diff --git a/include/boost/dll/import.hpp b/include/boost/dll/import.hpp new file mode 100644 index 0000000000000000000000000000000000000000..74f23bfcefa19f3aa2a1f4d7e701df85edb4c2a8 --- /dev/null +++ b/include/boost/dll/import.hpp @@ -0,0 +1,227 @@ +// Copyright 2014 Renato Tegon Forti, Antony Polukhin. +// Copyright 2015 Antony Polukhin. +// +// 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 +#include +#include +#include +#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 refc_function { + boost::shared_ptr lib_; + T* func_ptr_; + + public: + refc_function(const boost::shared_ptr& lib, T* func_ptr) BOOST_NOEXCEPT + : lib_(lib) + , func_ptr_(func_ptr) + {} + + operator T*() const BOOST_NOEXCEPT { + return func_ptr_; + } + }; + + template + struct import_type; + + template + struct import_type >::type> { + typedef boost::dll::detail::refc_function base_type; + typedef boost::function< + typename boost::dll::detail::strip_calling_convention::type + > type; + }; + + template + struct import_type >::type> { + typedef boost::shared_ptr base_type; + typedef boost::shared_ptr type; + }; +} // namespace detail + + +#ifndef BOOST_DLL_DOXYGEN +# define BOOST_DLL_IMPORT_RESULT_TYPE inline typename boost::dll::detail::import_type::type +#endif + + +/*! +* Returns boost::function or boost::shared_ptr that holds an imported function or variable +* from the loaded library and 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 +* boost::function f = import( +* boost::make_shared("test_lib.so"), +* "integer_func_name" +* ); +* \endcode +* +* \code +* boost::function f = import("test_lib.so", "integer_func_name"); +* \endcode +* +* \code +* boost::shared_ptr i = import("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 or shared pointer to 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 boost::function if T is a function type, or boost::shared_ptr if T is an object type. +* +* \throw boost::system::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(const boost::filesystem::path& lib, const char* name, + load_mode::type mode = load_mode::default_mode); + +//! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) +template +BOOST_DLL_IMPORT_RESULT_TYPE import(const boost::filesystem::path& lib, const std::string& name, + load_mode::type mode = load_mode::default_mode) +{ + return boost::dll::import( + boost::make_shared(lib, mode), + name.c_str() + ); +} + +//! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) +template +BOOST_DLL_IMPORT_RESULT_TYPE import(const boost::shared_ptr& lib, const char* name) { + typedef typename boost::dll::detail::import_type::base_type type; + return type(lib, &lib->get(name)); +} + +//! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) +template +BOOST_DLL_IMPORT_RESULT_TYPE import(const boost::shared_ptr& lib, const std::string& name) { + return boost::dll::import(lib, name.c_str()); +} + +template +BOOST_DLL_IMPORT_RESULT_TYPE import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) { + return boost::dll::import( + boost::make_shared(lib, mode), + name + ); +} + + + + +/*! +* Returns boost::function or boost::shared_ptr that holds an imported function or variable +* from the loaded library and 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 +* boost::function f = import_alias( +* boost::make_shared("test_lib.so"), +* "integer_func_alias_name" +* ); +* \endcode +* +* \code +* boost::function f = import_alias("test_lib.so", "integer_func_alias_name"); +* \endcode +* +* \code +* boost::shared_ptr i = import_alias("test_lib.so", "integer_alias_name"); +* \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 or shared pointer to 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 boost::function if T is a function type, or boost::shared_ptr if T is an object type. +* +* \throw boost::system::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::filesystem::path& lib, const char* name, + load_mode::type mode = load_mode::default_mode); + +//! \overload boost::dll::import_alias(const boost::filesystem::path& lib, const char* name, load_mode::type mode) +template +BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::filesystem::path& lib, const std::string& name, + load_mode::type mode = load_mode::default_mode) +{ + return boost::dll::import_alias( + boost::make_shared(lib, mode), + name.c_str() + ); +} + +//! \overload boost::dll::import_alias(const boost::filesystem::path& lib, const char* name, load_mode::type mode) +template +BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::shared_ptr& lib, const std::string& name) { + return boost::dll::import_alias(lib, name.c_str()); +} + +//! \overload boost::dll::import_alias(const boost::filesystem::path& lib, const char* name, load_mode::type mode) +template +BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::shared_ptr& lib, const char* name) { + typedef typename boost::dll::detail::import_type::base_type type; + return type(lib, lib->get(name)); +} + +template +BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::filesystem::path& lib, const char* name, load_mode::type mode) { + return boost::dll::import_alias( + boost::make_shared(lib, mode), + name + ); +} +#undef BOOST_DLL_IMPORT_RESULT_TYPE + + +}} // boost::dll + +#endif // BOOST_DLL_IMPORT_HPP +