import_mangled.hpp 11 KB

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