import.hpp 9.6 KB

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