// Copyright 2014 Renato Tegon Forti, Antony Polukhin. // Copyright Antony Polukhin, 2015-2025. // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_DLL_IMPORT_HPP #define BOOST_DLL_IMPORT_HPP #include #include #include // std::addressof #include #ifdef BOOST_HAS_PRAGMA_ONCE # pragma once #endif /// \file boost/dll/import.hpp /// \brief Contains all the boost::dll::import* reference counting /// functions that hold a shared pointer to the instance of /// boost::dll::shared_library. namespace boost { namespace dll { namespace detail { template class library_function { // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster. boost::dll::detail::shared_ptr f_; public: inline library_function(const boost::dll::detail::shared_ptr& lib, T* func_ptr) noexcept : f_(lib, func_ptr) {} // Compilation error at this point means that imported function // was called with unmatching parameters. // // Example: // auto f = dll::import_symbol("function", "lib.so"); // f("Hello"); // error: invalid conversion from 'const char*' to 'int' // f(1, 2); // error: too many arguments to function // f(); // error: too few arguments to function template inline auto operator()(Args&&... args) const -> decltype( (*f_)(static_cast(args)...) ) { return (*f_)(static_cast(args)...); } }; template using import_type = typename std::conditional< std::is_object::value, boost::dll::detail::shared_ptr, boost::dll::detail::library_function >::type; } // namespace detail #ifndef BOOST_DLL_DOXYGEN # define BOOST_DLL_IMPORT_RESULT_TYPE inline boost::dll::detail::import_type #endif /*! * Returns callable object or std::shared_ptr (boost::shared_ptr if * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) that holds the symbol imported * from the loaded library. Returned value refcounts usage * of the loaded shared library, so that it won't get unload until all copies of return value * are not destroyed. * * This call will succeed if call to \forcedlink{shared_library}`::has(const char* )` * function with the same symbol name returned `true`. * * For importing symbols by \b alias names use \forcedlink{import_alias} method. * * \b Examples: * * \code * std::function f = import_symbol("test_lib.so", "integer_func_name"); * * auto f_cpp11 = import_symbol("test_lib.so", "integer_func_name"); * \endcode * * \code * std::shared_ptr i = import_symbol("test_lib.so", "integer_name"); * \endcode * * \b Template \b parameter \b T: Type of the symbol that we are going to import. Must be explicitly specified. * * \param lib Path to shared library or shared library to load function from. * \param name Null-terminated C or C++ mangled name of the function to import. Can handle std::string, char*, const char*. * \param mode An mode that will be used on library load. * * \return callable object if T is a function type, or std::shared_ptr (boost::shared_ptr if * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) if T is an object type. * * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded. * Overload that accepts path also throws std::bad_alloc in case of insufficient memory. */ template BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(const boost::dll::fs::path& lib, const char* name, load_mode::type mode = load_mode::default_mode) { using type = boost::dll::detail::import_type; auto p = boost::dll::detail::make_shared(lib, mode); auto* addr = std::addressof(p->get(name)); return type(std::move(p), addr); } //! \overload boost::dll::import_symbol(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(const boost::dll::fs::path& lib, const std::string& name, load_mode::type mode = load_mode::default_mode) { return dll::import_symbol(lib, name.c_str(), mode); } //! \overload boost::dll::import_symbol(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(const shared_library& lib, const char* name) { using type = boost::dll::detail::import_type; auto p = boost::dll::detail::make_shared(lib); return type(p, std::addressof(p->get(name))); } //! \overload boost::dll::import_symbol(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(const shared_library& lib, const std::string& name) { return dll::import_symbol(lib, name.c_str()); } //! \overload boost::dll::import_symbol(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(shared_library&& lib, const char* name) { using type = boost::dll::detail::import_type; auto p = boost::dll::detail::make_shared( std::move(lib) ); auto* addr = std::addressof(p->get(name)); return type(std::move(p), addr); } //! \overload boost::dll::import_symbol(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(shared_library&& lib, const std::string& name) { return dll::import_symbol(std::move(lib), name.c_str()); } /*! * Returns callable object or std::shared_ptr (boost::shared_ptr if * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) that holds the symbol imported * from the loaded library. Returned value refcounts usage * of the loaded shared library, so that it won't get unload until all copies of return value * are not destroyed. * * This call will succeed if call to \forcedlink{shared_library}`::has(const char* )` * function with the same symbol name returned `true`. * * For importing symbols by \b non \b alias names use \forcedlink{import} method. * * \b Examples: * * \code * std::function f = import_alias("test_lib.so", "integer_func_alias_name"); * * auto f_cpp11 = import_alias("test_lib.so", "integer_func_alias_name"); * \endcode * * \code * std::shared_ptr i = import_alias("test_lib.so", "integer_alias_name"); * \endcode * * \code * \endcode * * \b Template \b parameter \b T: Type of the symbol alias that we are going to import. Must be explicitly specified. * * \param lib Path to shared library or shared library to load function from. * \param name Null-terminated C or C++ mangled name of the function or variable to import. Can handle std::string, char*, const char*. * \param mode An mode that will be used on library load. * * \return callable object if T is a function type, or std::shared_ptr (boost::shared_ptr if * BOOST_DLL_USE_BOOST_SHARED_PTR is defined) if T is an object type. * * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded. * Overload that accepts path also throws std::bad_alloc in case of insufficient memory. */ template BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode = load_mode::default_mode) { using type = boost::dll::detail::import_type; auto p = boost::dll::detail::make_shared(lib, mode); auto* addr = p->get(name); return type(std::move(p), addr); } //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::dll::fs::path& lib, const std::string& name, load_mode::type mode = load_mode::default_mode) { return dll::import_alias(lib, name.c_str(), mode); } //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const shared_library& lib, const char* name) { using type = boost::dll::detail::import_type; auto p = boost::dll::detail::make_shared(lib); auto* addr = p->get(name); return type(std::move(p), addr); } //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const shared_library& lib, const std::string& name) { return dll::import_alias(lib, name.c_str()); } //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_alias(shared_library&& lib, const char* name) { using type = boost::dll::detail::import_type; auto p = boost::dll::detail::make_shared( std::move(lib) ); auto* addr = p->get(name); return type(std::move(p), addr); } //! \overload boost::dll::import_alias(const boost::dll::fs::path& lib, const char* name, load_mode::type mode) template BOOST_DLL_IMPORT_RESULT_TYPE import_alias(shared_library&& lib, const std::string& name) { return dll::import_alias(std::move(lib), name.c_str()); } #undef BOOST_DLL_IMPORT_RESULT_TYPE }} // boost::dll #endif // BOOST_DLL_IMPORT_HPP