smart_library.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright 2016 Klemens Morgenstern
  2. // Copyright Antony Polukhin, 2019-2025
  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 Experimental feature that relies on an incomplete implementation of platform specific C++
  11. /// mangling. In case of an issue provide a PR with a fix and tests to https://github.com/boostorg/dll .
  12. /// boost/dll/smart_library.hpp is not included in boost/dll.hpp
  13. /// \brief Contains the boost::dll::experimental::smart_library class for loading mangled symbols.
  14. #include <boost/dll/config.hpp>
  15. #if defined(_MSC_VER) // MSVC, Clang-cl, and ICC on Windows
  16. # include <boost/dll/detail/demangling/msvc.hpp>
  17. #else
  18. # include <boost/dll/detail/demangling/itanium.hpp>
  19. #endif
  20. #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
  21. # error This file requires C++11 at least!
  22. #endif
  23. #include <boost/dll/shared_library.hpp>
  24. #include <boost/dll/detail/get_mem_fn_type.hpp>
  25. #include <boost/dll/detail/ctor_dtor.hpp>
  26. #include <boost/dll/detail/type_info.hpp>
  27. #include <type_traits>
  28. #include <utility> // std::move
  29. namespace boost {
  30. namespace dll {
  31. namespace experimental {
  32. using boost::dll::detail::constructor;
  33. using boost::dll::detail::destructor;
  34. /*!
  35. * \brief This class is an extension of \ref shared_library, which allows to load C++ symbols.
  36. *
  37. * This class allows type safe loading of overloaded functions, member-functions, constructors and variables.
  38. * It also allows to overwrite classes so they can be loaded, while being declared with different names.
  39. *
  40. * \warning Experimental feature that relies on an incomplete implementation of platform specific C++
  41. * mangling. In case of an issue provide a PR with a fix and tests to https://github.com/boostorg/dll
  42. *
  43. * Currently known limitations:
  44. *
  45. * Member functions must be defined outside of the class to be exported. That is:
  46. * \code
  47. * //not exported:
  48. * struct BOOST_SYMBOL_EXPORT my_class { void func() {} };
  49. * //exported
  50. * struct BOOST_SYMBOL_EXPORT my_class { void func(); };
  51. * void my_class::func() {}
  52. * \endcode
  53. *
  54. * With the current analysis, the first version does get exported in MSVC.
  55. * MinGW also does export it, BOOST_SYMBOL_EXPORT is written before it. To allow this on windows one can use
  56. * BOOST_DLL_MEMBER_EXPORT for this, so that MinGW and MSVC can provide those functions. This does however not work with gcc on linux.
  57. *
  58. * Direct initialization of members.
  59. * On linux the following member variable i will not be initialized when using the allocating constructor:
  60. * \code
  61. * struct BOOST_SYMBOL_EXPORT my_class { int i; my_class() : i(42) {} };
  62. * \endcode
  63. *
  64. * This does however not happen when the value is set inside the constructor function.
  65. */
  66. class smart_library {
  67. shared_library lib_;
  68. detail::mangled_storage_impl storage_;
  69. public:
  70. /*!
  71. * Get the underlying shared_library
  72. */
  73. const shared_library &shared_lib() const noexcept { return lib_;}
  74. using mangled_storage = detail::mangled_storage_impl;
  75. /*!
  76. * Access to the mangled storage, which is created on construction.
  77. *
  78. * \throw Nothing.
  79. */
  80. const mangled_storage &symbol_storage() const noexcept { return storage_; }
  81. ///Overload, for current development.
  82. mangled_storage &symbol_storage() noexcept { return storage_; }
  83. //! \copydoc shared_library::shared_library()
  84. smart_library() = default;
  85. //! \copydoc shared_library::shared_library(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode)
  86. smart_library(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode) {
  87. lib_.load(lib_path, mode);
  88. storage_.load(lib_path);
  89. }
  90. //! \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)
  91. smart_library(const boost::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode) {
  92. load(lib_path, mode, ec);
  93. }
  94. //! \copydoc shared_library::shared_library(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec)
  95. smart_library(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec) {
  96. load(lib_path, mode, ec);
  97. }
  98. /*!
  99. * copy a smart_library object.
  100. *
  101. * \param lib A smart_library to move from.
  102. *
  103. * \throw Nothing.
  104. */
  105. smart_library(const smart_library & lib) = default;
  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(smart_library&& lib) = default;
  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) 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(shared_library&& lib) noexcept
  134. : lib_(std::move(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() = default;
  147. //! \copydoc shared_library::load(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode)
  148. void load(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode) {
  149. boost::dll::fs::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::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode)
  157. void load(const boost::dll::fs::path& lib_path, boost::dll::fs::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::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec)
  163. void load(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::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 \forcedlinkfs{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 \forcedlinkfs{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 \forcedlinkfs{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 \forcedlinkfs{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 whose destructor shall be loaded
  272. * \return A destructor object.
  273. *
  274. * \throw \forcedlinkfs{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 whose typeinfo shall be loaded
  293. * \return A reference to a type_info object.
  294. *
  295. * \throw \forcedlinkfs{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() noexcept {
  329. storage_.clear();
  330. lib_.unload();
  331. }
  332. //! \copydoc shared_library::is_loaded() const
  333. bool is_loaded() const noexcept {
  334. return lib_.is_loaded();
  335. }
  336. //! \copydoc shared_library::operator bool() const
  337. explicit operator bool() const noexcept {
  338. return is_loaded();
  339. }
  340. //! \copydoc shared_library::has(const char* symbol_name) const
  341. bool has(const char* symbol_name) const 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 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) 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) 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) 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) 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) 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. typename std::enable_if<std::is_object<T>::value, T&>::type get(const smart_library& sm, const std::string &name)
  396. {
  397. return sm.get_variable<T>(name);
  398. }
  399. template<class T>
  400. typename std::enable_if<std::is_function<T>::value, T&>::type get(const smart_library& sm, const std::string &name)
  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_ */