import.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2019 Antony Polukhin.
  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_IMPORT_HPP
  8. #define BOOST_DLL_IMPORT_HPP
  9. #include <boost/dll/config.hpp>
  10. #include <boost/core/addressof.hpp>
  11. #include <boost/core/enable_if.hpp>
  12. #include <boost/type_traits/is_object.hpp>
  13. #include <boost/make_shared.hpp>
  14. #include <boost/dll/shared_library.hpp>
  15. #include <boost/move/move.hpp>
  16. #if defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) || defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  17. # include <boost/function.hpp>
  18. #endif
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. # pragma once
  21. #endif
  22. /// \file boost/dll/import.hpp
  23. /// \brief Contains all the boost::dll::import* reference counting
  24. /// functions that hold a shared pointer to the instance of
  25. /// boost::dll::shared_library.
  26. namespace boost { namespace dll {
  27. namespace detail {
  28. template <class T>
  29. class library_function {
  30. // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster.
  31. boost::shared_ptr<T> f_;
  32. public:
  33. inline library_function(const boost::shared_ptr<shared_library>& lib, T* func_ptr) BOOST_NOEXCEPT
  34. : f_(lib, func_ptr)
  35. {}
  36. #if defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) || defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  37. operator T*() const BOOST_NOEXCEPT {
  38. return f_.get();
  39. }
  40. #else
  41. // Compilation error at this point means that imported function
  42. // was called with unmatching parameters.
  43. //
  44. // Example:
  45. // auto f = dll::import<void(int)>("function", "lib.so");
  46. // f("Hello"); // error: invalid conversion from 'const char*' to 'int'
  47. // f(1, 2); // error: too many arguments to function
  48. // f(); // error: too few arguments to function
  49. template <class... Args>
  50. inline auto operator()(Args&&... args) const
  51. -> decltype( (*f_)(static_cast<Args&&>(args)...) )
  52. {
  53. return (*f_)(static_cast<Args&&>(args)...);
  54. }
  55. #endif
  56. };
  57. template <class T, class = void>
  58. struct import_type;
  59. template <class T>
  60. struct import_type<T, typename boost::disable_if<boost::is_object<T> >::type> {
  61. typedef boost::dll::detail::library_function<T> base_type;
  62. #if defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) || defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  63. typedef boost::function<T> type;
  64. #else
  65. typedef boost::dll::detail::library_function<T> type;
  66. #endif
  67. };
  68. template <class T>
  69. struct import_type<T, typename boost::enable_if<boost::is_object<T> >::type> {
  70. typedef boost::shared_ptr<T> base_type;
  71. typedef boost::shared_ptr<T> type;
  72. };
  73. } // namespace detail
  74. #ifndef BOOST_DLL_DOXYGEN
  75. # define BOOST_DLL_IMPORT_RESULT_TYPE inline typename boost::dll::detail::import_type<T>::type
  76. #endif
  77. /*!
  78. * Returns callable object or boost::shared_ptr<T> that holds the symbol imported
  79. * from the loaded library. Returned value refcounts usage
  80. * of the loaded shared library, so that it won't get unload until all copies of return value
  81. * are not destroyed.
  82. *
  83. * This call will succeed if call to \forcedlink{shared_library}`::has(const char* )`
  84. * function with the same symbol name returned `true`.
  85. *
  86. * For importing symbols by \b alias names use \forcedlink{import_alias} method.
  87. *
  88. * \b Examples:
  89. *
  90. * \code
  91. * boost::function<int(int)> f = import<int(int)>("test_lib.so", "integer_func_name");
  92. *
  93. * auto f_cpp11 = import<int(int)>("test_lib.so", "integer_func_name");
  94. * \endcode
  95. *
  96. * \code
  97. * boost::shared_ptr<int> i = import<int>("test_lib.so", "integer_name");
  98. * \endcode
  99. *
  100. * \b Template \b parameter \b T: Type of the symbol that we are going to import. Must be explicitly specified.
  101. *
  102. * \param lib Path to shared library or shared library to load function from.
  103. * \param name Null-terminated C or C++ mangled name of the function to import. Can handle std::string, char*, const char*.
  104. * \param mode An mode that will be used on library load.
  105. *
  106. * \return callable object if T is a function type, or boost::shared_ptr<T> if T is an object type.
  107. *
  108. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  109. * Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
  110. */
  111. template <class T>
  112. BOOST_DLL_IMPORT_RESULT_TYPE import(const boost::dll::fs::path& lib, const char* name,
  113. load_mode::type mode = load_mode::default_mode)
  114. {
  115. typedef typename boost::dll::detail::import_type<T>::base_type type;
  116. boost::shared_ptr<boost::dll::shared_library> p = boost::make_shared<boost::dll::shared_library>(lib, mode);
  117. return type(p, boost::addressof(p->get<T>(name)));
  118. }
  119. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  120. template <class T>
  121. BOOST_DLL_IMPORT_RESULT_TYPE import(const boost::dll::fs::path& lib, const std::string& name,
  122. load_mode::type mode = load_mode::default_mode)
  123. {
  124. return import<T>(lib, name.c_str(), mode);
  125. }
  126. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  127. template <class T>
  128. BOOST_DLL_IMPORT_RESULT_TYPE import(const shared_library& lib, const char* name) {
  129. typedef typename boost::dll::detail::import_type<T>::base_type type;
  130. boost::shared_ptr<boost::dll::shared_library> p = boost::make_shared<boost::dll::shared_library>(lib);
  131. return type(p, boost::addressof(p->get<T>(name)));
  132. }
  133. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  134. template <class T>
  135. BOOST_DLL_IMPORT_RESULT_TYPE import(const shared_library& lib, const std::string& name) {
  136. return import<T>(lib, name.c_str());
  137. }
  138. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  139. template <class T>
  140. BOOST_DLL_IMPORT_RESULT_TYPE import(BOOST_RV_REF(shared_library) lib, const char* name) {
  141. typedef typename boost::dll::detail::import_type<T>::base_type type;
  142. boost::shared_ptr<boost::dll::shared_library> p = boost::make_shared<boost::dll::shared_library>(
  143. boost::move(lib)
  144. );
  145. return type(p, boost::addressof(p->get<T>(name)));
  146. }
  147. //! \overload boost::dll::import(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  148. template <class T>
  149. BOOST_DLL_IMPORT_RESULT_TYPE import(BOOST_RV_REF(shared_library) lib, const std::string& name) {
  150. return import<T>(boost::move(lib), name.c_str());
  151. }
  152. /*!
  153. * Returns callable object or boost::shared_ptr<T> that holds the symbol imported
  154. * from the loaded library. Returned value refcounts usage
  155. * of the loaded shared library, so that it won't get unload until all copies of return value
  156. * are not destroyed.
  157. *
  158. * This call will succeed if call to \forcedlink{shared_library}`::has(const char* )`
  159. * function with the same symbol name returned `true`.
  160. *
  161. * For importing symbols by \b non \b alias names use \forcedlink{import} method.
  162. *
  163. * \b Examples:
  164. *
  165. * \code
  166. * boost::function<int(int)> f = import_alias<int(int)>("test_lib.so", "integer_func_alias_name");
  167. *
  168. * auto f_cpp11 = import_alias<int(int)>("test_lib.so", "integer_func_alias_name");
  169. * \endcode
  170. *
  171. * \code
  172. * boost::shared_ptr<int> i = import_alias<int>("test_lib.so", "integer_alias_name");
  173. * \endcode
  174. *
  175. * \code
  176. * \endcode
  177. *
  178. * \b Template \b parameter \b T: Type of the symbol alias that we are going to import. Must be explicitly specified.
  179. *
  180. * \param lib Path to shared library or shared library to load function from.
  181. * \param name Null-terminated C or C++ mangled name of the function or variable to import. Can handle std::string, char*, const char*.
  182. * \param mode An mode that will be used on library load.
  183. *
  184. * \return callable object if T is a function type, or boost::shared_ptr<T> if T is an object type.
  185. *
  186. * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
  187. * Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
  188. */
  189. template <class T>
  190. BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::dll::fs::path& lib, const char* name,
  191. load_mode::type mode = load_mode::default_mode)
  192. {
  193. typedef typename boost::dll::detail::import_type<T>::base_type type;
  194. boost::shared_ptr<boost::dll::shared_library> p = boost::make_shared<boost::dll::shared_library>(lib, mode);
  195. return type(p, p->get<T*>(name));
  196. }
  197. //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  198. template <class T>
  199. BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::dll::fs::path& lib, const std::string& name,
  200. load_mode::type mode = load_mode::default_mode)
  201. {
  202. return import_alias<T>(lib, name.c_str(), mode);
  203. }
  204. //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  205. template <class T>
  206. BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const shared_library& lib, const char* name) {
  207. typedef typename boost::dll::detail::import_type<T>::base_type type;
  208. boost::shared_ptr<boost::dll::shared_library> p = boost::make_shared<boost::dll::shared_library>(lib);
  209. return type(p, p->get<T*>(name));
  210. }
  211. //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  212. template <class T>
  213. BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const shared_library& lib, const std::string& name) {
  214. return import_alias<T>(lib, name.c_str());
  215. }
  216. //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  217. template <class T>
  218. BOOST_DLL_IMPORT_RESULT_TYPE import_alias(BOOST_RV_REF(shared_library) lib, const char* name) {
  219. typedef typename boost::dll::detail::import_type<T>::base_type type;
  220. boost::shared_ptr<boost::dll::shared_library> p = boost::make_shared<boost::dll::shared_library>(
  221. boost::move(lib)
  222. );
  223. return type(p, p->get<T*>(name));
  224. }
  225. //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode)
  226. template <class T>
  227. BOOST_DLL_IMPORT_RESULT_TYPE import_alias(BOOST_RV_REF(shared_library) lib, const std::string& name) {
  228. return import_alias<T>(boost::move(lib), name.c_str());
  229. }
  230. #undef BOOST_DLL_IMPORT_RESULT_TYPE
  231. }} // boost::dll
  232. #endif // BOOST_DLL_IMPORT_HPP