smart_library.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 INCLUDE_BOOST_DLL_SMART_LIBRARY_HPP_
  7. #define INCLUDE_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/shared_library.hpp>
  12. #include <boost/dll/detail/get_mem_fn_type.hpp>
  13. #include <boost/dll/detail/ctor_dtor.hpp>
  14. #include <boost/predef/compiler.h>
  15. #if BOOST_COMP_GNUC || BOOST_COMP_CLANG || BOOST_COMP_HPACC || BOOST_COMP_IBM
  16. #include <boost/dll/detail/demangling/itanium.hpp>
  17. #elif BOOST_COMP_MSVC
  18. #include <boost/dll/detail/demangling/msvc.hpp>
  19. #else
  20. #error "Compiler not supported"
  21. #endif
  22. namespace boost {
  23. namespace dll {
  24. namespace experimental {
  25. using boost::dll::detail::constructor;
  26. using boost::dll::detail::destructor;
  27. /*!
  28. * \brief This class is an extension of \ref shared_library, which allows to load C++ symbols.
  29. *
  30. * This class allows type safe loading of overloaded functions, member-functions, constructors and variables.
  31. * It also allows to overwrite classes so they can be loaded, while being declared with different names.
  32. *
  33. * \warning Is still very experimental.
  34. *
  35. * Currently known limitations:
  36. *
  37. * Member functions must be defined outside of the class to be exported. That is:
  38. * \code
  39. * //not exported:
  40. * struct BOOST_SYMBOL_EXPORT my_class { void func() {}};
  41. * //exported
  42. * struct BOOST_SYMBOL_EXPORT my_class { void func();};
  43. * void my_class::func() {};
  44. * \endcode
  45. *
  46. * With the current analysis, the first version does get exported in MSVC.
  47. * MinGW also does export it, BOOST_SYMBOL_EXPORT is written before it. To allow this on windows one can use
  48. * BOOST_DLL_MEMBER_EXPORT for this, so that MinGW and MSVC can provide those functions. This does however not work with gcc on linux.
  49. *
  50. * Direct initialization of members.
  51. * On linux the following member variable i will not be initialized when using the allocating contructor:
  52. * \code
  53. * struct BOOST_SYMBOL_EXPORT my_class { int i; my_class() : i(42) {} };
  54. * \endcode
  55. *
  56. * This does however not happen when the value is set inside the constructor function.
  57. */
  58. class smart_library {
  59. shared_library _lib;
  60. detail::mangled_storage_impl _storage;
  61. public:
  62. /*!
  63. * Get the underlying shared_library
  64. */
  65. const shared_library &shared_lib() const {return _lib;}
  66. using mangled_storage = detail::mangled_storage_impl;
  67. /*!
  68. * Acces to the mangled storage, which is created on construction.
  69. *
  70. * \throw Nothing.
  71. */
  72. const mangled_storage &symbol_storage() const {return _storage;}
  73. //! \copydoc shared_library::shared_library()
  74. smart_library() BOOST_NOEXCEPT {};
  75. //! \copydoc shared_library::shared_library(const boost::filesystem::path& lib_path, load_mode::type mode = load_mode::default_mode)
  76. smart_library(const boost::filesystem::path& lib_path, load_mode::type mode = load_mode::default_mode) {
  77. _lib.load(lib_path, mode);
  78. _storage.load(lib_path, mode);
  79. }
  80. //! \copydoc shared_library::shared_library(const boost::filesystem::path& lib_path, boost::system::error_code& ec, load_mode::type mode = load_mode::default_mode)
  81. smart_library(const boost::filesystem::path& lib_path, boost::system::error_code& ec, load_mode::type mode = load_mode::default_mode) {
  82. load(lib_path, mode, ec);
  83. }
  84. //! \copydoc shared_library::shared_library(const boost::filesystem::path& lib_path, load_mode::type mode, boost::system::error_code& ec)
  85. smart_library(const boost::filesystem::path& lib_path, load_mode::type mode, boost::system::error_code& ec) {
  86. load(lib_path, mode, ec);
  87. }
  88. //! \copydoc shared_library::shared_library(BOOST_RV_REF(smart_library) lib)
  89. smart_library(BOOST_RV_REF(smart_library) lib) BOOST_NOEXCEPT // Move ctor
  90. : _lib(boost::move(static_cast<shared_library&>(lib._lib))), _storage(boost::move(lib._storage))
  91. {}
  92. //! \copydoc shared_library::~shared_library()
  93. ~smart_library() BOOST_NOEXCEPT {};
  94. //! \copydoc shared_library::load(const boost::filesystem::path& lib_path, load_mode::type mode = load_mode::default_mode)
  95. void load(const boost::filesystem::path& lib_path, load_mode::type mode = load_mode::default_mode) {
  96. boost::system::error_code ec;
  97. _storage.load(lib_path);
  98. _lib.load(lib_path, mode, ec);
  99. if (ec) {
  100. boost::dll::detail::report_error(ec, "load() failed");
  101. }
  102. }
  103. //! \copydoc shared_library::load(const boost::filesystem::path& lib_path, boost::system::error_code& ec, load_mode::type mode = load_mode::default_mode)
  104. void load(const boost::filesystem::path& lib_path, boost::system::error_code& ec, load_mode::type mode = load_mode::default_mode) {
  105. ec.clear();
  106. _storage.load(lib_path);
  107. _lib.load(lib_path, mode, ec);
  108. }
  109. //! \copydoc shared_library::load(const boost::filesystem::path& lib_path, load_mode::type mode, boost::system::error_code& ec)
  110. void load(const boost::filesystem::path& lib_path, load_mode::type mode, boost::system::error_code& ec) {
  111. ec.clear();
  112. _storage.load(lib_path);
  113. _lib.load(lib_path, mode, ec);
  114. }
  115. /*!
  116. * Load a variable from the referenced library.
  117. *
  118. * Unlinke shared_library::get this function will also load scoped variables, which also includes static class members.
  119. *
  120. * \note When mangled, MSVC will also check the type.
  121. *
  122. * \param name Name of the variable
  123. * \tparam T Type of the variable
  124. * \return A reference to the variable of type T.
  125. *
  126. * \throw boost::system::system_error if symbol does not exist or if the DLL/DSO was not loaded.
  127. */
  128. template<typename T>
  129. T& get_variable(const std::string &name) {
  130. return _lib.get<T>(_storage.get_variable<T>(name));
  131. }
  132. /*!
  133. * Load a function from the referenced library.
  134. *
  135. * \b Example:
  136. *
  137. * \code
  138. * smart_library lib("test_lib.so");
  139. * typedef int (&add_ints)(int, int);
  140. * typedef double (&add_doubles)(double, double);
  141. * add_ints f1 = lib.get_function<int(int, int)> ("func_name");
  142. * add_doubles f2 = lib.get_function<double(double, double)>("func_name");
  143. * \endcode
  144. *
  145. * \note When mangled, MSVC will also check the return type.
  146. *
  147. * \param name Name of the function.
  148. * \tparam Func Type of the function, required for determining the overload
  149. * \return A reference to the function of type F.
  150. *
  151. * \throw boost::system::system_error if symbol does not exist or if the DLL/DSO was not loaded.
  152. */
  153. template<typename Func>
  154. Func& get_function(const std::string &name) {
  155. return _lib.get<Func>(_storage.get_function<Func>(name));
  156. }
  157. /*!
  158. * Load a member-function from the referenced library.
  159. *
  160. * \b Example (import class is MyClass, which is available inside the library and the host):
  161. *
  162. * \code
  163. * smart_library lib("test_lib.so");
  164. *
  165. * typedef int MyClass(*func)(int);
  166. * typedef int MyClass(*func_const)(int) const;
  167. *
  168. * add_ints f1 = lib.get_mem_fn<MyClass, int(int)> ("MyClass::function");
  169. * add_doubles f2 = lib.get_mem_fn<const MyClass, double(double)>("MyClass::function");
  170. * \endcode
  171. *
  172. * \note When mangled, MSVC will also check the return type.
  173. *
  174. * \param name Name of the function.
  175. * \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.
  176. * \tparam Func Signature of the function, required for determining the overload
  177. * \return A pointer to the member-function with the signature provided
  178. *
  179. * \throw boost::system::system_error if symbol does not exist or if the DLL/DSO was not loaded.
  180. */
  181. template<typename Class, typename Func>
  182. typename detail::get_mem_fn_type<Class, Func>::mem_fn get_mem_fn(const std::string& name) {
  183. return _lib.get<typename detail::get_mem_fn_type<Class, Func>::mem_fn>(
  184. _storage.get_mem_fn<Class, Func>(name)
  185. );
  186. }
  187. /*!
  188. * Load a constructor from the referenced library.
  189. *
  190. * \b Example (import class is MyClass, which is available inside the library and the host):
  191. *
  192. * \code
  193. * smart_library lib("test_lib.so");
  194. *
  195. * constructor<MyClass(int) f1 = lib.get_mem_fn<MyClass(int)>();
  196. * \endcode
  197. *
  198. * \tparam Signature Signature of the function, required for determining the overload. The return type is the class which this is the constructor of.
  199. * \return A constructor object.
  200. *
  201. * \throw boost::system::system_error if symbol does not exist or if the DLL/DSO was not loaded.
  202. */
  203. template<typename Signature>
  204. constructor<Signature> get_constructor() {
  205. return detail::load_ctor<Signature>(_lib, _storage.get_constructor<Signature>());
  206. }
  207. /*!
  208. * Load a destructor from the referenced library.
  209. *
  210. * \b Example (import class is MyClass, which is available inside the library and the host):
  211. *
  212. * \code
  213. * smart_library lib("test_lib.so");
  214. *
  215. * destructor<MyClass> f1 = lib.get_mem_fn<MyClass>();
  216. * \endcode
  217. *
  218. * \tparam Class The class whichs destructor shall be loaded
  219. * \return A destructor object.
  220. *
  221. * \throw boost::system::system_error if symbol does not exist or if the DLL/DSO was not loaded.
  222. *
  223. */
  224. template<typename Class>
  225. destructor<Class> get_destructor() {
  226. return detail::load_dtor<Class>(_lib, _storage.get_destructor<Class>());
  227. }
  228. /**
  229. * This function can be used to add a type alias.
  230. *
  231. * This is to be used, when a class shall be imported, which is not declared on the host side.
  232. *
  233. * Example:
  234. * \code
  235. * smart_library lib("test_lib.so");
  236. *
  237. * lib.add_type_alias<MyAlias>("MyClass"); //when using MyAlias, the library will look for MyClass
  238. *
  239. * //get the destructor of MyClass
  240. * destructor<MyAlias> dtor = lib.get_destructor<MyAlias>();
  241. * \endcode
  242. *
  243. *
  244. * \param name Name of the class the alias is for.
  245. *
  246. * \attention If the alias-type is not large enough for the imported class, it will result in undefined behaviour.
  247. * \warning The alias will only be applied for the type signature, it will not replace the token in the scoped name.
  248. */
  249. template<typename Alias> void add_type_alias(const std::string& name) {
  250. this->_storage.add_alias<Alias>(name);
  251. }
  252. //! \copydoc shared_library::unload()
  253. void unload() BOOST_NOEXCEPT {
  254. _storage.clear();
  255. _lib.unload();
  256. }
  257. //! \copydoc shared_library::is_loaded() const
  258. bool is_loaded() const BOOST_NOEXCEPT {
  259. return _lib.is_loaded();
  260. }
  261. //! \copydoc shared_library::operator!() const
  262. bool operator!() const BOOST_NOEXCEPT {
  263. return !is_loaded();
  264. }
  265. //! \copydoc shared_library::operator bool() const
  266. BOOST_EXPLICIT_OPERATOR_BOOL()
  267. //! \copydoc shared_library::has(const char* symbol_name) const
  268. bool has(const char* symbol_name) const BOOST_NOEXCEPT {
  269. return _lib.has(symbol_name);
  270. }
  271. //! \copydoc shared_library::has(const std::string& symbol_name) const
  272. bool has(const std::string& symbol_name) const BOOST_NOEXCEPT {
  273. return _lib.has(symbol_name);
  274. }
  275. //! \copydoc shared_library::assign(const shared_library& lib)
  276. smart_library& assign(const smart_library& lib) {
  277. _lib.assign(lib._lib);
  278. _storage.assign(lib._storage);
  279. return *this;
  280. }
  281. //! \copydoc shared_library::swap(shared_library& rhs)
  282. void swap(smart_library& rhs) BOOST_NOEXCEPT {
  283. _lib.swap(rhs._lib);
  284. _storage.swap(rhs._storage);
  285. }
  286. };
  287. /// Very fast equality check that compares the actual DLL/DSO objects. Throws nothing.
  288. inline bool operator==(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
  289. return lhs.shared_lib().native() == rhs.shared_lib().native();
  290. }
  291. /// Very fast inequality check that compares the actual DLL/DSO objects. Throws nothing.
  292. inline bool operator!=(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
  293. return lhs.shared_lib().native() != rhs.shared_lib().native();
  294. }
  295. /// Compare the actual DLL/DSO objects without any guarantee to be stable between runs. Throws nothing.
  296. inline bool operator<(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
  297. return lhs.shared_lib().native() < rhs.shared_lib().native();
  298. }
  299. /// Swaps two shared libraries. Does not invalidate symbols and functions loaded from libraries. Throws nothing.
  300. inline void swap(smart_library& lhs, smart_library& rhs) BOOST_NOEXCEPT {
  301. lhs.swap(rhs);
  302. }
  303. } /* namespace experimental */
  304. } /* namespace dll */
  305. } /* namespace boost */
  306. #endif /* INCLUDE_BOOST_DLL_SMART_LIBRARY_HPP_ */