smart_library.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Copyright 2016 Klemens Morgenstern
  2. // Copyright 2019-2021 Antony Polukhin
  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_SMART_LIBRARY_HPP_
  8. #define BOOST_DLL_SMART_LIBRARY_HPP_
  9. /// \file boost/dll/smart_library.hpp
  10. /// \warning Extremely experimental! Requires C++11! Will change in next version of Boost! boost/dll/smart_library.hpp is not included in boost/dll.hpp
  11. /// \brief Contains the boost::dll::experimental::smart_library class for loading mangled symbols.
  12. #include <boost/dll/config.hpp>
  13. #if defined(_MSC_VER) // MSVC, Clang-cl, and ICC on Windows
  14. # include <boost/dll/detail/demangling/msvc.hpp>
  15. #else
  16. # include <boost/dll/detail/demangling/itanium.hpp>
  17. #endif
  18. #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
  19. # error This file requires C++11 at least!
  20. #endif
  21. #include <boost/dll/shared_library.hpp>
  22. #include <boost/dll/detail/get_mem_fn_type.hpp>
  23. #include <boost/dll/detail/ctor_dtor.hpp>
  24. #include <boost/dll/detail/type_info.hpp>
  25. #include <boost/type_traits/is_object.hpp>
  26. #include <boost/type_traits/is_void.hpp>
  27. #include <boost/type_traits/is_function.hpp>
  28. namespace boost {
  29. namespace dll {
  30. namespace experimental {
  31. using boost::dll::detail::constructor;
  32. using boost::dll::detail::destructor;
  33. /*!
  34. * \brief This class is an extension of \ref shared_library, which allows to load C++ symbols.
  35. *
  36. * This class allows type safe loading of overloaded functions, member-functions, constructors and variables.
  37. * It also allows to overwrite classes so they can be loaded, while being declared with different names.
  38. *
  39. * \warning Is still very experimental.
  40. *
  41. * Currently known limitations:
  42. *
  43. * Member functions must be defined outside of the class to be exported. That is:
  44. * \code
  45. * //not exported:
  46. * struct BOOST_SYMBOL_EXPORT my_class { void func() {}};
  47. * //exported
  48. * struct BOOST_SYMBOL_EXPORT my_class { void func();};
  49. * void my_class::func() {};
  50. * \endcode
  51. *
  52. * With the current analysis, the first version does get exported in MSVC.
  53. * MinGW also does export it, BOOST_SYMBOL_EXPORT is written before it. To allow this on windows one can use
  54. * BOOST_DLL_MEMBER_EXPORT for this, so that MinGW and MSVC can provide those functions. This does however not work with gcc on linux.
  55. *
  56. * Direct initialization of members.
  57. * On linux the following member variable i will not be initialized when using the allocating constructor:
  58. * \code
  59. * struct BOOST_SYMBOL_EXPORT my_class { int i; my_class() : i(42) {} };
  60. * \endcode
  61. *
  62. * This does however not happen when the value is set inside the constructor function.
  63. */
  64. class smart_library {
  65. shared_library _lib;
  66. detail::mangled_storage_impl _storage;
  67. public:
  68. /*!
  69. * Get the underlying shared_library
  70. */
  71. const shared_library &shared_lib() const {return _lib;}
  72. using mangled_storage = detail::mangled_storage_impl;
  73. /*!
  74. * Access to the mangled storage, which is created on construction.
  75. *
  76. * \throw Nothing.
  77. */
  78. const mangled_storage &symbol_storage() const {return _storage;}
  79. ///Overload, for current development.
  80. mangled_storage &symbol_storage() {return _storage;}
  81. //! \copydoc shared_library::shared_library()
  82. smart_library() BOOST_NOEXCEPT {};
  83. //! \copydoc shared_library::shared_library(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode)
  84. smart_library(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode) {
  85. _lib.load(lib_path, mode);
  86. _storage.load(lib_path);
  87. }
  88. //! \copydoc shared_library::shared_library(const boost::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode)
  89. smart_library(const boost::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode) {
  90. load(lib_path, mode, ec);
  91. }
  92. //! \copydoc shared_library::shared_library(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec)
  93. smart_library(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec) {
  94. load(lib_path, mode, ec);
  95. }
  96. /*!
  97. * copy a smart_library object.
  98. *
  99. * \param lib A smart_library to move from.
  100. *
  101. * \throw Nothing.
  102. */
  103. smart_library(const smart_library & lib) BOOST_NOEXCEPT
  104. : _lib(lib._lib), _storage(lib._storage)
  105. {}
  106. /*!
  107. * Move a smart_library object.
  108. *
  109. * \param lib A smart_library to move from.
  110. *
  111. * \throw Nothing.
  112. */
  113. smart_library(BOOST_RV_REF(smart_library) lib) BOOST_NOEXCEPT
  114. : _lib(boost::move(lib._lib)), _storage(boost::move(lib._storage))
  115. {}
  116. /*!
  117. * Construct from a shared_library object.
  118. *
  119. * \param lib A shared_library to move from.
  120. *
  121. * \throw Nothing.
  122. */
  123. explicit smart_library(const shared_library & lib) BOOST_NOEXCEPT
  124. : _lib(lib)
  125. {
  126. _storage.load(lib.location());
  127. }
  128. /*!
  129. * Construct from a shared_library object.
  130. *
  131. * \param lib A shared_library to move from.
  132. *
  133. * \throw Nothing.
  134. */
  135. explicit smart_library(BOOST_RV_REF(shared_library) lib) BOOST_NOEXCEPT
  136. : _lib(boost::move(static_cast<shared_library&>(lib)))
  137. {
  138. _storage.load(lib.location());
  139. }
  140. /*!
  141. * Destroys the smart_library.
  142. * `unload()` is called if the DLL/DSO was loaded. If library was loaded multiple times
  143. * by different instances of shared_library, the actual DLL/DSO won't be unloaded until
  144. * there is at least one instance of shared_library.
  145. *
  146. * \throw Nothing.
  147. */
  148. ~smart_library() BOOST_NOEXCEPT {};
  149. //! \copydoc shared_library::load(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode)
  150. void load(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode) {
  151. boost::dll::fs::error_code ec;
  152. _storage.load(lib_path);
  153. _lib.load(lib_path, mode, ec);
  154. if (ec) {
  155. boost::dll::detail::report_error(ec, "load() failed");
  156. }
  157. }
  158. //! \copydoc shared_library::load(const boost::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode)
  159. void load(const boost::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode) {
  160. ec.clear();
  161. _storage.load(lib_path);
  162. _lib.load(lib_path, mode, ec);
  163. }
  164. //! \copydoc shared_library::load(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec)
  165. void load(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec) {
  166. ec.clear();
  167. _storage.load(lib_path);
  168. _lib.load(lib_path, mode, ec);
  169. }
  170. /*!
  171. * Load a variable from the referenced library.
  172. *
  173. * Unlinke shared_library::get this function will also load scoped variables, which also includes static class members.
  174. *
  175. * \note When mangled, MSVC will also check the type.
  176. *
  177. * \param name Name of the variable
  178. * \tparam T Type of the variable
  179. * \return A reference to the variable of type T.
  180. *
  181. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  182. */
  183. template<typename T>
  184. T& get_variable(const std::string &name) const {
  185. return _lib.get<T>(_storage.get_variable<T>(name));
  186. }
  187. /*!
  188. * Load a function from the referenced library.
  189. *
  190. * \b Example:
  191. *
  192. * \code
  193. * smart_library lib("test_lib.so");
  194. * typedef int (&add_ints)(int, int);
  195. * typedef double (&add_doubles)(double, double);
  196. * add_ints f1 = lib.get_function<int(int, int)> ("func_name");
  197. * add_doubles f2 = lib.get_function<double(double, double)>("func_name");
  198. * \endcode
  199. *
  200. * \note When mangled, MSVC will also check the return type.
  201. *
  202. * \param name Name of the function.
  203. * \tparam Func Type of the function, required for determining the overload
  204. * \return A reference to the function of type F.
  205. *
  206. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  207. */
  208. template<typename Func>
  209. Func& get_function(const std::string &name) const {
  210. return _lib.get<Func>(_storage.get_function<Func>(name));
  211. }
  212. /*!
  213. * Load a member-function from the referenced library.
  214. *
  215. * \b Example (import class is MyClass, which is available inside the library and the host):
  216. *
  217. * \code
  218. * smart_library lib("test_lib.so");
  219. *
  220. * typedef int MyClass(*func)(int);
  221. * typedef int MyClass(*func_const)(int) const;
  222. *
  223. * add_ints f1 = lib.get_mem_fn<MyClass, int(int)> ("MyClass::function");
  224. * add_doubles f2 = lib.get_mem_fn<const MyClass, double(double)>("MyClass::function");
  225. * \endcode
  226. *
  227. * \note When mangled, MSVC will also check the return type.
  228. *
  229. * \param name Name of the function.
  230. * \tparam Class The class the function is a member of. If Class is const, the function will be assumed as taking a const this-pointer. The same applies for volatile.
  231. * \tparam Func Signature of the function, required for determining the overload
  232. * \return A pointer to the member-function with the signature provided
  233. *
  234. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  235. */
  236. template<typename Class, typename Func>
  237. typename boost::dll::detail::get_mem_fn_type<Class, Func>::mem_fn get_mem_fn(const std::string& name) const {
  238. return _lib.get<typename boost::dll::detail::get_mem_fn_type<Class, Func>::mem_fn>(
  239. _storage.get_mem_fn<Class, Func>(name)
  240. );
  241. }
  242. /*!
  243. * Load a constructor from the referenced library.
  244. *
  245. * \b Example (import class is MyClass, which is available inside the library and the host):
  246. *
  247. * \code
  248. * smart_library lib("test_lib.so");
  249. *
  250. * constructor<MyClass(int) f1 = lib.get_mem_fn<MyClass(int)>();
  251. * \endcode
  252. *
  253. * \tparam Signature Signature of the function, required for determining the overload. The return type is the class which this is the constructor of.
  254. * \return A constructor object.
  255. *
  256. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  257. */
  258. template<typename Signature>
  259. constructor<Signature> get_constructor() const {
  260. return boost::dll::detail::load_ctor<Signature>(_lib, _storage.get_constructor<Signature>());
  261. }
  262. /*!
  263. * Load a destructor from the referenced library.
  264. *
  265. * \b Example (import class is MyClass, which is available inside the library and the host):
  266. *
  267. * \code
  268. * smart_library lib("test_lib.so");
  269. *
  270. * destructor<MyClass> f1 = lib.get_mem_fn<MyClass>();
  271. * \endcode
  272. *
  273. * \tparam Class The class whose destructor shall be loaded
  274. * \return A destructor object.
  275. *
  276. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  277. *
  278. */
  279. template<typename Class>
  280. destructor<Class> get_destructor() const {
  281. return boost::dll::detail::load_dtor<Class>(_lib, _storage.get_destructor<Class>());
  282. }
  283. /*!
  284. * Load the typeinfo of the given type.
  285. *
  286. * \b Example (import class is MyClass, which is available inside the library and the host):
  287. *
  288. * \code
  289. * smart_library lib("test_lib.so");
  290. *
  291. * std::type_info &ti = lib.get_Type_info<MyClass>();
  292. * \endcode
  293. *
  294. * \tparam Class The class whose typeinfo shall be loaded
  295. * \return A reference to a type_info object.
  296. *
  297. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  298. *
  299. */
  300. template<typename Class>
  301. const std::type_info& get_type_info() const
  302. {
  303. return boost::dll::detail::load_type_info<Class>(_lib, _storage);
  304. }
  305. /**
  306. * This function can be used to add a type alias.
  307. *
  308. * This is to be used, when a class shall be imported, which is not declared on the host side.
  309. *
  310. * Example:
  311. * \code
  312. * smart_library lib("test_lib.so");
  313. *
  314. * lib.add_type_alias<MyAlias>("MyClass"); //when using MyAlias, the library will look for MyClass
  315. *
  316. * //get the destructor of MyClass
  317. * destructor<MyAlias> dtor = lib.get_destructor<MyAlias>();
  318. * \endcode
  319. *
  320. *
  321. * \param name Name of the class the alias is for.
  322. *
  323. * \attention If the alias-type is not large enough for the imported class, it will result in undefined behaviour.
  324. * \warning The alias will only be applied for the type signature, it will not replace the token in the scoped name.
  325. */
  326. template<typename Alias> void add_type_alias(const std::string& name) {
  327. this->_storage.add_alias<Alias>(name);
  328. }
  329. //! \copydoc shared_library::unload()
  330. void unload() BOOST_NOEXCEPT {
  331. _storage.clear();
  332. _lib.unload();
  333. }
  334. //! \copydoc shared_library::is_loaded() const
  335. bool is_loaded() const BOOST_NOEXCEPT {
  336. return _lib.is_loaded();
  337. }
  338. //! \copydoc shared_library::operator!() const
  339. bool operator!() const BOOST_NOEXCEPT {
  340. return !is_loaded();
  341. }
  342. //! \copydoc shared_library::operator bool() const
  343. BOOST_EXPLICIT_OPERATOR_BOOL()
  344. //! \copydoc shared_library::has(const char* symbol_name) const
  345. bool has(const char* symbol_name) const BOOST_NOEXCEPT {
  346. return _lib.has(symbol_name);
  347. }
  348. //! \copydoc shared_library::has(const std::string& symbol_name) const
  349. bool has(const std::string& symbol_name) const BOOST_NOEXCEPT {
  350. return _lib.has(symbol_name);
  351. }
  352. //! \copydoc shared_library::assign(const shared_library& lib)
  353. smart_library& assign(const smart_library& lib) {
  354. _lib.assign(lib._lib);
  355. _storage.assign(lib._storage);
  356. return *this;
  357. }
  358. //! \copydoc shared_library::swap(shared_library& rhs)
  359. void swap(smart_library& rhs) BOOST_NOEXCEPT {
  360. _lib.swap(rhs._lib);
  361. _storage.swap(rhs._storage);
  362. }
  363. };
  364. /// Very fast equality check that compares the actual DLL/DSO objects. Throws nothing.
  365. inline bool operator==(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
  366. return lhs.shared_lib().native() == rhs.shared_lib().native();
  367. }
  368. /// Very fast inequality check that compares the actual DLL/DSO objects. Throws nothing.
  369. inline bool operator!=(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
  370. return lhs.shared_lib().native() != rhs.shared_lib().native();
  371. }
  372. /// Compare the actual DLL/DSO objects without any guarantee to be stable between runs. Throws nothing.
  373. inline bool operator<(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
  374. return lhs.shared_lib().native() < rhs.shared_lib().native();
  375. }
  376. /// Swaps two shared libraries. Does not invalidate symbols and functions loaded from libraries. Throws nothing.
  377. inline void swap(smart_library& lhs, smart_library& rhs) BOOST_NOEXCEPT {
  378. lhs.swap(rhs);
  379. }
  380. #ifdef BOOST_DLL_DOXYGEN
  381. /** Helper functions for overloads.
  382. *
  383. * Gets either a variable, function or member-function, depending on the signature.
  384. *
  385. * @code
  386. * smart_library sm("lib.so");
  387. * get<int>(sm, "space::value"); //import a variable
  388. * get<void(int)>(sm, "space::func"); //import a function
  389. * get<some_class, void(int)>(sm, "space::class_::mem_fn"); //import a member function
  390. * @endcode
  391. *
  392. * @param sm A reference to the @ref smart_library
  393. * @param name The name of the entity to import
  394. */
  395. template<class T, class T2>
  396. void get(const smart_library& sm, const std::string &name);
  397. #endif
  398. template<class T>
  399. typename boost::enable_if<boost::is_object<T>, T&>::type get(const smart_library& sm, const std::string &name)
  400. {
  401. return sm.get_variable<T>(name);
  402. }
  403. template<class T>
  404. typename boost::enable_if<boost::is_function<T>, T&>::type get(const smart_library& sm, const std::string &name)
  405. {
  406. return sm.get_function<T>(name);
  407. }
  408. template<class Class, class Signature>
  409. auto get(const smart_library& sm, const std::string &name) -> typename detail::get_mem_fn_type<Class, Signature>::mem_fn
  410. {
  411. return sm.get_mem_fn<Class, Signature>(name);
  412. }
  413. } /* namespace experimental */
  414. } /* namespace dll */
  415. } /* namespace boost */
  416. #endif /* BOOST_DLL_SMART_LIBRARY_HPP_ */