import_mangled.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright 2015-2018 Klemens D. Morgenstern
  2. // Copyright Antony Polukhin, 2019-2024
  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 Extremely experimental! Requires C++11! Will change in next version of Boost! boost/dll/import_mangled.hpp is not included in boost/dll.hpp
  11. /// \brief Contains the boost::dll::experimental::import_mangled function for importing mangled symbols.
  12. #include <boost/dll/config.hpp>
  13. #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
  14. # error This file requires C++11 at least!
  15. #endif
  16. #include <boost/make_shared.hpp>
  17. #include <boost/move/move.hpp>
  18. #include <boost/dll/smart_library.hpp>
  19. #include <boost/dll/detail/import_mangled_helpers.hpp>
  20. #include <boost/core/addressof.hpp>
  21. #include <boost/core/enable_if.hpp>
  22. #include <boost/type_traits/conditional.hpp>
  23. #include <boost/type_traits/is_object.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. # pragma once
  26. #endif
  27. namespace boost { namespace dll { namespace experimental {
  28. namespace detail
  29. {
  30. template <class ... Ts>
  31. class mangled_library_function {
  32. // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
  33. boost::shared_ptr<shared_library> lib_;
  34. function_tuple<Ts...> f_;
  35. public:
  36. constexpr mangled_library_function(const boost::shared_ptr<shared_library>& lib, Ts*... func_ptr) BOOST_NOEXCEPT
  37. : lib_(lib)
  38. , f_(func_ptr...)
  39. {}
  40. // Compilation error at this point means that imported function
  41. // was called with unmatching parameters.
  42. //
  43. // Example:
  44. // auto f = dll::import_mangled<void(int), void(double)>("function", "lib.so");
  45. // f("Hello"); // error: invalid conversion from 'const char*' to 'int'
  46. // f(1, 2); // error: too many arguments to function
  47. // f(); // error: too few arguments to function
  48. template <class... Args>
  49. auto operator()(Args&&... args) const
  50. -> decltype( f_(static_cast<Args&&>(args)...) )
  51. {
  52. return f_(static_cast<Args&&>(args)...);
  53. }
  54. };
  55. template<class Class, class Sequence>
  56. class mangled_library_mem_fn;
  57. template <class Class, class ... Ts>
  58. class mangled_library_mem_fn<Class, sequence<Ts...>> {
  59. // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
  60. typedef mem_fn_tuple<Ts...> call_tuple_t;
  61. boost::shared_ptr<shared_library> lib_;
  62. call_tuple_t f_;
  63. public:
  64. constexpr mangled_library_mem_fn(const boost::shared_ptr<shared_library>& lib, typename Ts::mem_fn... func_ptr) BOOST_NOEXCEPT
  65. : lib_(lib)
  66. , f_(func_ptr...)
  67. {}
  68. template <class ClassIn, class... Args>
  69. auto operator()(ClassIn *cl, Args&&... args) const
  70. -> decltype( f_(cl, static_cast<Args&&>(args)...) )
  71. {
  72. return f_(cl, static_cast<Args&&>(args)...);
  73. }
  74. };
  75. // simple enough to be here
  76. template<class Seq> struct is_variable : boost::false_type {};
  77. template<typename T> struct is_variable<sequence<T>> : boost::is_object<T> {};
  78. template <class Sequence,
  79. bool isFunction = is_function_seq<Sequence>::value,
  80. bool isMemFn = is_mem_fn_seq <Sequence>::value,
  81. bool isVariable = is_variable <Sequence>::value>
  82. struct mangled_import_type;
  83. template <class ...Args>
  84. struct mangled_import_type<sequence<Args...>, true,false,false> //is function
  85. {
  86. typedef boost::dll::experimental::detail::mangled_library_function<Args...> type;
  87. static type make(
  88. const boost::dll::experimental::smart_library& p,
  89. const std::string& name)
  90. {
  91. return type(
  92. boost::make_shared<shared_library>(p.shared_lib()),
  93. boost::addressof(p.get_function<Args>(name))...);
  94. }
  95. };
  96. template <class Class, class ...Args>
  97. struct mangled_import_type<sequence<Class, Args...>, false, true, false> //is member-function
  98. {
  99. typedef typename boost::dll::experimental::detail::make_mem_fn_seq<Class, Args...>::type actual_sequence;
  100. typedef typename boost::dll::experimental::detail::mangled_library_mem_fn<Class, actual_sequence> type;
  101. template<class ... ArgsIn>
  102. static type make_impl(
  103. const boost::dll::experimental::smart_library& p,
  104. const std::string & name,
  105. sequence<ArgsIn...> * )
  106. {
  107. return type(boost::make_shared<shared_library>(p.shared_lib()),
  108. p.get_mem_fn<typename ArgsIn::class_type, typename ArgsIn::func_type>(name)...);
  109. }
  110. static type make(
  111. const boost::dll::experimental::smart_library& p,
  112. const std::string& name)
  113. {
  114. return make_impl(p, name, static_cast<actual_sequence*>(nullptr));
  115. }
  116. };
  117. template <class T>
  118. struct mangled_import_type<sequence<T>, false, false, true> //is variable
  119. {
  120. typedef boost::shared_ptr<T> type;
  121. static type make(
  122. const boost::dll::experimental::smart_library& p,
  123. const std::string& name)
  124. {
  125. return type(
  126. boost::make_shared<shared_library>(p.shared_lib()),
  127. boost::addressof(p.get_variable<T>(name)));
  128. }
  129. };
  130. } // namespace detail
  131. #ifndef BOOST_DLL_DOXYGEN
  132. # define BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE inline typename \
  133. boost::dll::experimental::detail::mangled_import_type<boost::dll::experimental::detail::sequence<Args...>>::type
  134. #endif
  135. /*
  136. * Variants:
  137. * import_mangled<int>("Stuff");
  138. * import_mangled<thingy(xyz)>("Function");
  139. * import mangled<thingy, void(int)>("Function");
  140. */
  141. /*!
  142. * Returns callable object or boost::shared_ptr<T> 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. * boost::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. * boost::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 boost::shared_ptr<T> if T is an object type.
  186. *
  187. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  188. * Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
  189. */
  190. template <class ...Args>
  191. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const char* name,
  192. load_mode::type mode = load_mode::default_mode)
  193. {
  194. typedef typename boost::dll::experimental::detail::mangled_import_type<
  195. boost::dll::experimental::detail::sequence<Args...>> type;
  196. boost::dll::experimental::smart_library p(lib, mode);
  197. //the load
  198. return type::make(p, name);
  199. }
  200. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  201. template <class ...Args>
  202. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::path& lib, const std::string& name,
  203. load_mode::type mode = load_mode::default_mode)
  204. {
  205. return import_mangled<Args...>(lib, name.c_str(), mode);
  206. }
  207. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  208. template <class ...Args>
  209. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const char* name) {
  210. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  211. return type::make(lib, name);
  212. }
  213. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  214. template <class ...Args>
  215. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const std::string& name) {
  216. return import_mangled<Args...>(lib, name.c_str());
  217. }
  218. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  219. template <class ...Args>
  220. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(smart_library) lib, const char* name) {
  221. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  222. return type::make(lib, name);
  223. }
  224. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  225. template <class ...Args>
  226. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(smart_library) lib, const std::string& name) {
  227. return import_mangled<Args...>(boost::move(lib), name.c_str());
  228. }
  229. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  230. template <class ...Args>
  231. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const char* name) {
  232. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  233. boost::shared_ptr<boost::dll::experimental::smart_library> p = boost::make_shared<boost::dll::experimental::smart_library>(lib);
  234. return type::make(p, name);
  235. }
  236. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  237. template <class ...Args>
  238. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const std::string& name) {
  239. return import_mangled<Args...>(lib, name.c_str());
  240. }
  241. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  242. template <class ...Args>
  243. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(shared_library) lib, const char* name) {
  244. typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type;
  245. boost::dll::experimental::smart_library p(boost::move(lib));
  246. return type::make(p, name);
  247. }
  248. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  249. template <class ...Args>
  250. BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(shared_library) lib, const std::string& name) {
  251. return import_mangled<Args...>(boost::move(lib), name.c_str());
  252. }
  253. #undef BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE
  254. }}}
  255. #endif /* BOOST_DLL_IMPORT_MANGLED_HPP_ */