Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:30

0001 // Copyright 2015-2018 Klemens D. Morgenstern
0002 // Copyright Antony Polukhin, 2019-2023
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE_1_0.txt
0006 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 
0009 #ifndef BOOST_DLL_IMPORT_MANGLED_HPP_
0010 #define BOOST_DLL_IMPORT_MANGLED_HPP_
0011 
0012 /// \file boost/dll/import_mangled.hpp
0013 /// \warning Extremely experimental! Requires C++11! Will change in next version of Boost! boost/dll/import_mangled.hpp is not included in boost/dll.hpp
0014 /// \brief Contains the boost::dll::experimental::import_mangled function for importing mangled symbols.
0015 
0016 #include <boost/dll/config.hpp>
0017 #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
0018 #  error This file requires C++11 at least!
0019 #endif
0020 
0021 #include <boost/make_shared.hpp>
0022 #include <boost/move/move.hpp>
0023 #include <boost/dll/smart_library.hpp>
0024 #include <boost/dll/detail/import_mangled_helpers.hpp>
0025 #include <boost/core/addressof.hpp>
0026 #include <boost/core/enable_if.hpp>
0027 #include <boost/type_traits/conditional.hpp>
0028 #include <boost/type_traits/is_object.hpp>
0029 
0030 
0031 #ifdef BOOST_HAS_PRAGMA_ONCE
0032 # pragma once
0033 #endif
0034 
0035 namespace boost { namespace dll { namespace experimental {
0036 
0037 namespace detail
0038 {
0039 
0040 template <class ... Ts>
0041 class mangled_library_function {
0042     // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
0043     boost::shared_ptr<shared_library> lib_;
0044     function_tuple<Ts...>   f_;
0045 public:
0046     constexpr mangled_library_function(const boost::shared_ptr<shared_library>& lib, Ts*... func_ptr) BOOST_NOEXCEPT
0047         : lib_(lib)
0048         , f_(func_ptr...)
0049     {}
0050 
0051 
0052     // Compilation error at this point means that imported function
0053     // was called with unmatching parameters.
0054     //
0055     // Example:
0056     // auto f = dll::import_mangled<void(int), void(double)>("function", "lib.so");
0057     // f("Hello");  // error: invalid conversion from 'const char*' to 'int'
0058     // f(1, 2);     // error: too many arguments to function
0059     // f();         // error: too few arguments to function
0060     template <class... Args>
0061     auto operator()(Args&&... args) const
0062         -> decltype( f_(static_cast<Args&&>(args)...) )
0063     {
0064         return f_(static_cast<Args&&>(args)...);
0065     }
0066 };
0067 
0068 
0069 template<class Class, class Sequence>
0070 class mangled_library_mem_fn;
0071 
0072 template <class Class, class ... Ts>
0073 class mangled_library_mem_fn<Class, sequence<Ts...>> {
0074     // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
0075     typedef mem_fn_tuple<Ts...> call_tuple_t;
0076     boost::shared_ptr<shared_library>   lib_;
0077     call_tuple_t f_;
0078 
0079 public:
0080     constexpr mangled_library_mem_fn(const boost::shared_ptr<shared_library>& lib, typename Ts::mem_fn... func_ptr) BOOST_NOEXCEPT
0081         : lib_(lib)
0082         , f_(func_ptr...)
0083     {}
0084 
0085     template <class ClassIn, class... Args>
0086     auto operator()(ClassIn *cl, Args&&... args) const
0087         -> decltype( f_(cl, static_cast<Args&&>(args)...) )
0088     {
0089         return f_(cl, static_cast<Args&&>(args)...);
0090     }
0091 };
0092 
0093 
0094 
0095 
0096 // simple enough to be here
0097 template<class Seq>  struct is_variable : boost::false_type {};
0098 template<typename T> struct is_variable<sequence<T>> : boost::is_object<T> {};
0099 
0100 template <class Sequence,
0101           bool isFunction = is_function_seq<Sequence>::value,
0102           bool isMemFn    = is_mem_fn_seq  <Sequence>::value,
0103           bool isVariable = is_variable    <Sequence>::value>
0104 struct mangled_import_type;
0105 
0106 template <class ...Args>
0107 struct mangled_import_type<sequence<Args...>, true,false,false> //is function
0108 {
0109     typedef boost::dll::experimental::detail::mangled_library_function<Args...> type;
0110     static type make(
0111            const boost::dll::experimental::smart_library& p,
0112            const std::string& name)
0113     {
0114         return type(
0115                 boost::make_shared<shared_library>(p.shared_lib()),
0116                 boost::addressof(p.get_function<Args>(name))...);
0117     }
0118 };
0119 
0120 template <class Class, class ...Args>
0121 struct mangled_import_type<sequence<Class, Args...>, false, true, false> //is member-function
0122 {
0123     typedef typename boost::dll::experimental::detail::make_mem_fn_seq<Class, Args...>::type actual_sequence;
0124     typedef typename boost::dll::experimental::detail::mangled_library_mem_fn<Class, actual_sequence> type;
0125 
0126 
0127     template<class ... ArgsIn>
0128     static type make_impl(
0129             const boost::dll::experimental::smart_library& p,
0130             const std::string & name,
0131             sequence<ArgsIn...> * )
0132     {
0133         return type(boost::make_shared<shared_library>(p.shared_lib()),
0134                     p.get_mem_fn<typename ArgsIn::class_type, typename ArgsIn::func_type>(name)...);
0135     }
0136 
0137     static type make(
0138            const boost::dll::experimental::smart_library& p,
0139            const std::string& name)
0140     {
0141         return make_impl(p, name, static_cast<actual_sequence*>(nullptr));
0142     }
0143 
0144 };
0145 
0146 template <class T>
0147 struct mangled_import_type<sequence<T>, false, false, true> //is variable
0148 {
0149     typedef boost::shared_ptr<T> type;
0150 
0151     static type make(
0152            const boost::dll::experimental::smart_library& p,
0153            const std::string& name)
0154     {
0155         return type(
0156                 boost::make_shared<shared_library>(p.shared_lib()),
0157                 boost::addressof(p.get_variable<T>(name)));
0158     }
0159 
0160 };
0161 
0162 
0163 } // namespace detail
0164 
0165 
0166 #ifndef BOOST_DLL_DOXYGEN
0167 #   define BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE inline typename \
0168     boost::dll::experimental::detail::mangled_import_type<boost::dll::experimental::detail::sequence<Args...>>::type
0169 #endif
0170 
0171 /*
0172  * Variants:
0173  * import_mangled<int>("Stuff");
0174  * import_mangled<thingy(xyz)>("Function");
0175  * import mangled<thingy, void(int)>("Function");
0176  */
0177 
0178 /*!
0179 * Returns callable object or boost::shared_ptr<T> that holds the symbol imported
0180 * from the loaded library. Returned value refcounts usage
0181 * of the loaded shared library, so that it won't get unload until all copies of return value
0182 * are not destroyed.
0183 *
0184 * For importing symbols by \b alias names use \forcedlink{import_alias} method.
0185 *
0186 * \b Examples:
0187 *
0188 * \code
0189 * boost::function<int(int)> f = import_mangled<int(int)>("test_lib.so", "integer_func_name");
0190 *
0191 * auto f_cpp11 = import_mangled<int(int)>("test_lib.so", "integer_func_name");
0192 * \endcode
0193 *
0194 * \code
0195 * boost::shared_ptr<int> i = import_mangled<int>("test_lib.so", "integer_name");
0196 * \endcode
0197 *
0198 * Additionally you can also import overloaded symbols, including member-functions.
0199 *
0200 * \code
0201 * auto fp = import_mangled<void(int), void(double)>("test_lib.so", "func");
0202 * \endcode
0203 *
0204 * \code
0205 * auto fp = import_mangled<my_class, void(int), void(double)>("test_lib.so", "func");
0206 * \endcode
0207 *
0208 * If qualified member-functions are needed, this can be set by repeating the class name with const or volatile.
0209 * All following signatures after the redifintion will use this, i.e. the latest.
0210 *
0211 * * * \code
0212 * auto fp = import_mangled<my_class, void(int), void(double),
0213 *                          const my_class, void(int), void(double)>("test_lib.so", "func");
0214 * \endcode
0215 *
0216 * \b Template \b parameter \b T:    Type of the symbol that we are going to import. Must be explicitly specified.
0217 *
0218 * \param lib Path to shared library or shared library to load function from.
0219 * \param name Null-terminated C or C++ mangled name of the function to import. Can handle std::string, char*, const char*.
0220 * \param mode An mode that will be used on library load.
0221 *
0222 * \return callable object if T is a function type, or boost::shared_ptr<T> if T is an object type.
0223 *
0224 * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
0225 *       Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
0226 */
0227 
0228 
0229 template <class ...Args>
0230 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const char* name,
0231     load_mode::type mode = load_mode::default_mode)
0232 {
0233     typedef typename boost::dll::experimental::detail::mangled_import_type<
0234                      boost::dll::experimental::detail::sequence<Args...>> type;
0235 
0236     boost::dll::experimental::smart_library p(lib, mode);
0237     //the load
0238     return type::make(p, name);
0239 }
0240 
0241 
0242 
0243 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0244 template <class ...Args>
0245 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const std::string& name,
0246     load_mode::type mode = load_mode::default_mode)
0247 {
0248     return import_mangled<Args...>(lib, name.c_str(), mode);
0249 }
0250 
0251 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0252 template <class ...Args>
0253 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const char* name) {
0254     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0255 
0256     return type::make(lib, name);
0257 }
0258 
0259 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0260 template <class ...Args>
0261 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const std::string& name) {
0262     return import_mangled<Args...>(lib, name.c_str());
0263 }
0264 
0265 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0266 template <class ...Args>
0267 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(smart_library) lib, const char* name) {
0268     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0269 
0270     return type::make(lib, name);
0271 }
0272 
0273 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0274 template <class ...Args>
0275 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(smart_library) lib, const std::string& name) {
0276     return import_mangled<Args...>(boost::move(lib), name.c_str());
0277 }
0278 
0279 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0280 template <class ...Args>
0281 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const char* name) {
0282     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0283 
0284     boost::shared_ptr<boost::dll::experimental::smart_library> p = boost::make_shared<boost::dll::experimental::smart_library>(lib);
0285     return type::make(p, name);
0286 }
0287 
0288 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0289 template <class ...Args>
0290 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const std::string& name) {
0291     return import_mangled<Args...>(lib, name.c_str());
0292 }
0293 
0294 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0295 template <class ...Args>
0296 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(shared_library) lib, const char* name) {
0297     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0298 
0299     boost::dll::experimental::smart_library p(boost::move(lib));
0300 
0301     return type::make(p, name);
0302 }
0303 
0304 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0305 template <class ...Args>
0306 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(shared_library) lib, const std::string& name) {
0307     return import_mangled<Args...>(boost::move(lib), name.c_str());
0308 }
0309 
0310 #undef BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE
0311 
0312 }}}
0313 
0314 
0315 #endif /* BOOST_DLL_IMPORT_MANGLED_HPP_ */