smart_library.hpp 16 KB

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