Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:31:23

0001 // Copyright 2015-2018 Klemens D. Morgenstern
0002 // Copyright Antony Polukhin, 2019-2025
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 #ifndef BOOST_DLL_IMPORT_MANGLED_HPP_
0009 #define BOOST_DLL_IMPORT_MANGLED_HPP_
0010 
0011 /// \file boost/dll/import_mangled.hpp
0012 /// \warning Experimental feature that relies on an incomplete implementation of platform specific C++
0013 ///          mangling. In case of an issue provide a PR with a fix and tests to https://github.com/boostorg/dll .
0014 ///          boost/dll/import_mangled.hpp is not included in boost/dll.hpp
0015 /// \brief Contains the boost::dll::experimental::import_mangled function for importing mangled symbols.
0016 
0017 #include <boost/dll/config.hpp>
0018 #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
0019 #  error This file requires C++11 at least!
0020 #endif
0021 
0022 #include <boost/dll/config.hpp>
0023 #include <boost/dll/smart_library.hpp>
0024 #include <boost/dll/detail/import_mangled_helpers.hpp>
0025 
0026 #include <memory>  // std::addressof
0027 #include <type_traits>
0028 
0029 
0030 #ifdef BOOST_HAS_PRAGMA_ONCE
0031 # pragma once
0032 #endif
0033 
0034 namespace boost { namespace dll { namespace experimental {
0035 
0036 namespace detail
0037 {
0038 
0039 template <class ... Ts>
0040 class mangled_library_function {
0041     // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
0042     boost::dll::detail::shared_ptr<shared_library> lib_;
0043     function_tuple<Ts...>   f_;
0044 public:
0045     constexpr mangled_library_function(const boost::dll::detail::shared_ptr<shared_library>& lib, Ts*... func_ptr) noexcept
0046         : lib_(lib)
0047         , f_(func_ptr...)
0048     {}
0049 
0050 
0051     // Compilation error at this point means that imported function
0052     // was called with unmatching parameters.
0053     //
0054     // Example:
0055     // auto f = dll::import_mangled<void(int), void(double)>("function", "lib.so");
0056     // f("Hello");  // error: invalid conversion from 'const char*' to 'int'
0057     // f(1, 2);     // error: too many arguments to function
0058     // f();         // error: too few arguments to function
0059     template <class... Args>
0060     auto operator()(Args&&... args) const
0061         -> decltype( f_(static_cast<Args&&>(args)...) )
0062     {
0063         return f_(static_cast<Args&&>(args)...);
0064     }
0065 };
0066 
0067 
0068 template<class Class, class Sequence>
0069 class mangled_library_mem_fn;
0070 
0071 template <class Class, class ... Ts>
0072 class mangled_library_mem_fn<Class, sequence<Ts...>> {
0073     // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
0074     typedef mem_fn_tuple<Ts...> call_tuple_t;
0075     boost::dll::detail::shared_ptr<shared_library>   lib_;
0076     call_tuple_t f_;
0077 
0078 public:
0079     constexpr mangled_library_mem_fn(const boost::dll::detail::shared_ptr<shared_library>& lib, typename Ts::mem_fn... func_ptr) noexcept
0080         : lib_(lib)
0081         , f_(func_ptr...)
0082     {}
0083 
0084     template <class ClassIn, class... Args>
0085     auto operator()(ClassIn *cl, Args&&... args) const
0086         -> decltype( f_(cl, static_cast<Args&&>(args)...) )
0087     {
0088         return f_(cl, static_cast<Args&&>(args)...);
0089     }
0090 };
0091 
0092 
0093 
0094 
0095 // simple enough to be here
0096 template<class Seq>  struct is_variable : std::false_type {};
0097 template<typename T> struct is_variable<sequence<T>> : std::is_object<T> {};
0098 
0099 template <class Sequence,
0100           bool isFunction = is_function_seq<Sequence>::value,
0101           bool isMemFn    = is_mem_fn_seq  <Sequence>::value,
0102           bool isVariable = is_variable    <Sequence>::value>
0103 struct mangled_import_type;
0104 
0105 template <class ...Args>
0106 struct mangled_import_type<sequence<Args...>, true,false,false> //is function
0107 {
0108     typedef boost::dll::experimental::detail::mangled_library_function<Args...> type;
0109     static type make(
0110            const boost::dll::experimental::smart_library& p,
0111            const std::string& name)
0112     {
0113         return type(
0114                 boost::dll::detail::make_shared<shared_library>(p.shared_lib()),
0115                 std::addressof(p.get_function<Args>(name))...);
0116     }
0117 };
0118 
0119 template <class Class, class ...Args>
0120 struct mangled_import_type<sequence<Class, Args...>, false, true, false> //is member-function
0121 {
0122     typedef typename boost::dll::experimental::detail::make_mem_fn_seq<Class, Args...>::type actual_sequence;
0123     typedef typename boost::dll::experimental::detail::mangled_library_mem_fn<Class, actual_sequence> type;
0124 
0125 
0126     template<class ... ArgsIn>
0127     static type make_impl(
0128             const boost::dll::experimental::smart_library& p,
0129             const std::string & name,
0130             sequence<ArgsIn...> * )
0131     {
0132         return type(boost::dll::detail::make_shared<shared_library>(p.shared_lib()),
0133                     p.get_mem_fn<typename ArgsIn::class_type, typename ArgsIn::func_type>(name)...);
0134     }
0135 
0136     static type make(
0137            const boost::dll::experimental::smart_library& p,
0138            const std::string& name)
0139     {
0140         return make_impl(p, name, static_cast<actual_sequence*>(nullptr));
0141     }
0142 
0143 };
0144 
0145 template <class T>
0146 struct mangled_import_type<sequence<T>, false, false, true> //is variable
0147 {
0148     typedef boost::dll::detail::shared_ptr<T> type;
0149 
0150     static type make(
0151            const boost::dll::experimental::smart_library& p,
0152            const std::string& name)
0153     {
0154         return type(
0155                 boost::dll::detail::make_shared<shared_library>(p.shared_lib()),
0156                 std::addressof(p.get_variable<T>(name)));
0157     }
0158 
0159 };
0160 
0161 
0162 } // namespace detail
0163 
0164 
0165 #ifndef BOOST_DLL_DOXYGEN
0166 #   define BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE inline typename \
0167     boost::dll::experimental::detail::mangled_import_type<boost::dll::experimental::detail::sequence<Args...>>::type
0168 #endif
0169 
0170 /*
0171  * Variants:
0172  * import_mangled<int>("Stuff");
0173  * import_mangled<thingy(xyz)>("Function");
0174  * import mangled<thingy, void(int)>("Function");
0175  */
0176 
0177 /*!
0178 * Returns callable object or std::shared_ptr<T> (boost::shared_ptr<T> if
0179 * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) 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 * std::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 * std::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 std::shared_ptr<T> (boost::shared_ptr<T> if
0223 * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) if T is an object type.
0224 *
0225 * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
0226 *       Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
0227 */
0228 
0229 
0230 template <class ...Args>
0231 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const char* name,
0232     load_mode::type mode = load_mode::default_mode)
0233 {
0234     typedef typename boost::dll::experimental::detail::mangled_import_type<
0235                      boost::dll::experimental::detail::sequence<Args...>> type;
0236 
0237     return type::make(boost::dll::experimental::smart_library{lib, mode}, name);
0238 }
0239 
0240 
0241 
0242 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0243 template <class ...Args>
0244 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const std::string& name,
0245     load_mode::type mode = load_mode::default_mode)
0246 {
0247     return boost::dll::experimental::import_mangled<Args...>(lib, name.c_str(), mode);
0248 }
0249 
0250 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0251 template <class ...Args>
0252 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const char* name) {
0253     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0254 
0255     return type::make(lib, name);
0256 }
0257 
0258 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0259 template <class ...Args>
0260 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const std::string& name) {
0261     return boost::dll::experimental::import_mangled<Args...>(lib, name.c_str());
0262 }
0263 
0264 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0265 template <class ...Args>
0266 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(smart_library&& lib, const char* name) {
0267     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0268     return type::make(std::move(lib), name);
0269 }
0270 
0271 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0272 template <class ...Args>
0273 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(smart_library&& lib, const std::string& name) {
0274     return boost::dll::experimental::import_mangled<Args...>(std::move(lib), name.c_str());
0275 }
0276 
0277 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0278 template <class ...Args>
0279 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const char* name) {
0280     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0281     return type::make(
0282         boost::dll::detail::make_shared<boost::dll::experimental::smart_library>(lib),
0283         name
0284     );
0285 }
0286 
0287 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0288 template <class ...Args>
0289 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const std::string& name) {
0290     return boost::dll::experimental::import_mangled<Args...>(lib, name.c_str());
0291 }
0292 
0293 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0294 template <class ...Args>
0295 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(shared_library&& lib, const char* name) {
0296     typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
0297     return type::make(
0298         boost::dll::experimental::smart_library{std::move(lib)},
0299         name
0300     );
0301 }
0302 
0303 //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
0304 template <class ...Args>
0305 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(shared_library&& lib, const std::string& name) {
0306     return boost::dll::experimental::import_mangled<Args...>(std::move(lib), name.c_str());
0307 }
0308 
0309 #undef BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE
0310 
0311 }}}
0312 
0313 
0314 #endif /* BOOST_DLL_IMPORT_MANGLED_HPP_ */