smart_library.hpp 16 KB

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