import_mangled.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright 2015-2018 Klemens D. Morgenstern
  2. // Copyright Antony Polukhin, 2019-2025
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_IMPORT_MANGLED_HPP_
  8. #define BOOST_DLL_IMPORT_MANGLED_HPP_
  9. /// \file boost/dll/import_mangled.hpp
  10. /// \warning Experimental feature that relies on an incomplete implementation of platform specific C++
  11. /// mangling. In case of an issue provide a PR with a fix and tests to https://github.com/boostorg/dll .
  12. /// boost/dll/import_mangled.hpp is not included in boost/dll.hpp
  13. /// \brief Contains the boost::dll::experimental::import_mangled function for importing mangled symbols.
  14. #include <boost/dll/config.hpp>
  15. #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
  16. # error This file requires C++11 at least!
  17. #endif
  18. #include <boost/dll/config.hpp>
  19. #include <boost/dll/smart_library.hpp>
  20. #include <boost/dll/detail/import_mangled_helpers.hpp>
  21. #include <memory> // std::addressof
  22. #include <type_traits>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. # pragma once
  25. #endif
  26. namespace boost { namespace dll { namespace experimental {
  27. namespace detail
  28. {
  29. template <class ... Ts>
  30. class mangled_library_function {
  31. // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
  32. boost::dll::detail::shared_ptr<shared_library> lib_;
  33. function_tuple<Ts...> f_;
  34. public:
  35. constexpr mangled_library_function(const boost::dll::detail::shared_ptr<shared_library>& lib, Ts*... func_ptr) noexcept
  36. : lib_(lib)
  37. , f_(func_ptr...)
  38. {}
  39. // Compilation error at this point means that imported function
  40. // was called with unmatching parameters.
  41. //
  42. // Example:
  43. // auto f = dll::import_mangled<void(int), void(double)>("function", "lib.so");
  44. // f("Hello"); // error: invalid conversion from 'const char*' to 'int'
  45. // f(1, 2); // error: too many arguments to function
  46. // f(); // error: too few arguments to function
  47. template <class... Args>
  48. auto operator()(Args&&... args) const
  49. -> decltype( f_(static_cast<Args&&>(args)...) )
  50. {
  51. return f_(static_cast<Args&&>(args)...);
  52. }
  53. };
  54. template<class Class, class Sequence>
  55. class mangled_library_mem_fn;
  56. template <class Class, class ... Ts>
  57. class mangled_library_mem_fn<Class, sequence<Ts...>> {
  58. // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
  59. typedef mem_fn_tuple<Ts...> call_tuple_t;
  60. boost::dll::detail::shared_ptr<shared_library> lib_;
  61. call_tuple_t f_;
  62. public:
  63. constexpr mangled_library_mem_fn(const boost::dll::detail::shared_ptr<shared_library>& lib, typename Ts::mem_fn... func_ptr) noexcept
  64. : lib_(lib)
  65. , f_(func_ptr...)
  66. {}
  67. template <class ClassIn, class... Args>
  68. auto operator()(ClassIn *cl, Args&&... args) const
  69. -> decltype( f_(cl, static_cast<Args&&>(args)...) )
  70. {
  71. return f_(cl, static_cast<Args&&>(args)...);
  72. }
  73. };
  74. // simple enough to be here
  75. template<class Seq> struct is_variable : std::false_type {};
  76. template<typename T> struct is_variable<sequence<T>> : std::is_object<T> {};
  77. template <class Sequence,
  78. bool isFunction = is_function_seq<Sequence>::value,
  79. bool isMemFn = is_mem_fn_seq <Sequence>::value,
  80. bool isVariable = is_variable <Sequence>::value>
  81. struct mangled_import_type;
  82. template <class ...Args>
  83. struct mangled_import_type<sequence<Args...>, true,false,false> //is function
  84. {
  85. typedef boost::dll::experimental::detail::mangled_library_function<Args...> type;
  86. static type make(
  87. const boost::dll::experimental::smart_library& p,
  88. const std::string& name)
  89. {
  90. return type(
  91. boost::dll::detail::make_shared<shared_library>(p.shared_lib()),
  92. std::addressof(p.get_function<Args>(name))...);
  93. }
  94. };
  95. template <class Class, class ...Args>
  96. struct mangled_import_type<sequence<Class, Args...>, false, true, false> //is member-function
  97. {
  98. typedef typename boost::dll::experimental::detail::make_mem_fn_seq<Class, Args...>::type actual_sequence;
  99. typedef typename boost::dll::experimental::detail::mangled_library_mem_fn<Class, actual_sequence> type;
  100. template<class ... ArgsIn>
  101. static type make_impl(
  102. const boost::dll::experimental::smart_library& p,
  103. const std::string & name,
  104. sequence<ArgsIn...> * )
  105. {
  106. return type(boost::dll::detail::make_shared<shared_library>(p.shared_lib()),
  107. p.get_mem_fn<typename ArgsIn::class_type, typename ArgsIn::func_type>(name)...);
  108. }
  109. static type make(
  110. const boost::dll::experimental::smart_library& p,
  111. const std::string& name)
  112. {
  113. return make_impl(p, name, static_cast<actual_sequence*>(nullptr));
  114. }
  115. };
  116. template <class T>
  117. struct mangled_import_type<sequence<T>, false, false, true> //is variable
  118. {
  119. typedef boost::dll::detail::shared_ptr<T> type;
  120. static type make(
  121. const boost::dll::experimental::smart_library& p,
  122. const std::string& name)
  123. {
  124. return type(
  125. boost::dll::detail::make_shared<shared_library>(p.shared_lib()),
  126. std::addressof(p.get_variable<T>(name)));
  127. }
  128. };
  129. } // namespace detail
  130. #ifndef BOOST_DLL_DOXYGEN
  131. # define BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE inline typename \
  132. boost::dll::experimental::detail::mangled_import_type<boost::dll::experimental::detail::sequence<Args...>>::type
  133. #endif
  134. /*
  135. * Variants:
  136. * import_mangled<int>("Stuff");
  137. * import_mangled<thingy(xyz)>("Function");
  138. * import mangled<thingy, void(int)>("Function");
  139. */
  140. /*!
  141. * Returns callable object or std::shared_ptr<T> (boost::shared_ptr<T> if
  142. * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) that holds the symbol imported
  143. * from the loaded library. Returned value refcounts usage
  144. * of the loaded shared library, so that it won't get unload until all copies of return value
  145. * are not destroyed.
  146. *
  147. * For importing symbols by \b alias names use \forcedlink{import_alias} method.
  148. *
  149. * \b Examples:
  150. *
  151. * \code
  152. * std::function<int(int)> f = import_mangled<int(int)>("test_lib.so", "integer_func_name");
  153. *
  154. * auto f_cpp11 = import_mangled<int(int)>("test_lib.so", "integer_func_name");
  155. * \endcode
  156. *
  157. * \code
  158. * std::shared_ptr<int> i = import_mangled<int>("test_lib.so", "integer_name");
  159. * \endcode
  160. *
  161. * Additionally you can also import overloaded symbols, including member-functions.
  162. *
  163. * \code
  164. * auto fp = import_mangled<void(int), void(double)>("test_lib.so", "func");
  165. * \endcode
  166. *
  167. * \code
  168. * auto fp = import_mangled<my_class, void(int), void(double)>("test_lib.so", "func");
  169. * \endcode
  170. *
  171. * If qualified member-functions are needed, this can be set by repeating the class name with const or volatile.
  172. * All following signatures after the redifintion will use this, i.e. the latest.
  173. *
  174. * * * \code
  175. * auto fp = import_mangled<my_class, void(int), void(double),
  176. * const my_class, void(int), void(double)>("test_lib.so", "func");
  177. * \endcode
  178. *
  179. * \b Template \b parameter \b T: Type of the symbol that we are going to import. Must be explicitly specified.
  180. *
  181. * \param lib Path to shared library or shared library to load function from.
  182. * \param name Null-terminated C or C++ mangled name of the function to import. Can handle std::string, char*, const char*.
  183. * \param mode An mode that will be used on library load.
  184. *
  185. * \return callable object if T is a function type, or std::shared_ptr<T> (boost::shared_ptr<T> if
  186. * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) if T is an object type.
  187. *
  188. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  189. * Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
  190. */
  191. template <class ...Args>
  192. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const char* name,
  193. load_mode::type mode = load_mode::default_mode)
  194. {
  195. typedef typename boost::dll::experimental::detail::mangled_import_type<
  196. boost::dll::experimental::detail::sequence<Args...>> type;
  197. return type::make(boost::dll::experimental::smart_library{lib, mode}, name);
  198. }
  199. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  200. template <class ...Args>
  201. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const std::string& name,
  202. load_mode::type mode = load_mode::default_mode)
  203. {
  204. return boost::dll::experimental::import_mangled<Args...>(lib, name.c_str(), mode);
  205. }
  206. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  207. template <class ...Args>
  208. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const char* name) {
  209. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  210. return type::make(lib, name);
  211. }
  212. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  213. template <class ...Args>
  214. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const std::string& name) {
  215. return boost::dll::experimental::import_mangled<Args...>(lib, name.c_str());
  216. }
  217. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  218. template <class ...Args>
  219. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(smart_library&& lib, const char* name) {
  220. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  221. return type::make(std::move(lib), name);
  222. }
  223. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  224. template <class ...Args>
  225. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(smart_library&& lib, const std::string& name) {
  226. return boost::dll::experimental::import_mangled<Args...>(std::move(lib), name.c_str());
  227. }
  228. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  229. template <class ...Args>
  230. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const char* name) {
  231. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  232. return type::make(
  233. boost::dll::detail::make_shared<boost::dll::experimental::smart_library>(lib),
  234. name
  235. );
  236. }
  237. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  238. template <class ...Args>
  239. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const std::string& name) {
  240. return boost::dll::experimental::import_mangled<Args...>(lib, name.c_str());
  241. }
  242. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  243. template <class ...Args>
  244. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(shared_library&& lib, const char* name) {
  245. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  246. return type::make(
  247. boost::dll::experimental::smart_library{std::move(lib)},
  248. name
  249. );
  250. }
  251. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  252. template <class ...Args>
  253. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(shared_library&& lib, const std::string& name) {
  254. return boost::dll::experimental::import_mangled<Args...>(std::move(lib), name.c_str());
  255. }
  256. #undef BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE
  257. }}}
  258. #endif /* BOOST_DLL_IMPORT_MANGLED_HPP_ */