smart_library.hpp 16 KB

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