From 65a239dbe59dab3034df30c9789727f034cfea03 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Tue, 28 Jan 2025 19:12:07 +0300 Subject: [PATCH] Switch to std::shared_ptr from boost::shared_ptr and provide a BOOST_DLL_USE_BOOST_SHARED_PTR compatibility macro to restore the old behavior --- CMakeLists.txt | 1 - build.jam | 1 - example/getting_started.cpp | 4 +-- include/boost/dll/config.hpp | 28 +++++++++++++++++++ include/boost/dll/import.hpp | 36 +++++++++++++------------ include/boost/dll/import_mangled.hpp | 26 +++++++++--------- test/Jamfile.v2 | 2 ++ test/shared_library_get_symbol_test.cpp | 17 ++++++------ test/test_library.cpp | 7 +++-- 9 files changed, 76 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6853a650..cb91c0b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,6 @@ target_link_libraries(boost_dll Boost::core Boost::filesystem Boost::predef - Boost::smart_ptr Boost::system Boost::throw_exception Boost::type_index diff --git a/build.jam b/build.jam index f5d93e2b..a3508fc9 100644 --- a/build.jam +++ b/build.jam @@ -11,7 +11,6 @@ constant boost_dependencies : /boost/core//boost_core /boost/filesystem//boost_filesystem /boost/predef//boost_predef - /boost/smart_ptr//boost_smart_ptr /boost/system//boost_system /boost/throw_exception//boost_throw_exception /boost/type_index//boost_type_index diff --git a/example/getting_started.cpp b/example/getting_started.cpp index 25fb3fca..a43c7f30 100644 --- a/example/getting_started.cpp +++ b/example/getting_started.cpp @@ -30,7 +30,7 @@ int main(int argc, char* argv[]) { //[getting_started_imports_c_variable // Importing pure C variable - shared_ptr c_var = dll::import_symbol( + std::shared_ptr c_var = dll::import_symbol( path_to_shared_library, "c_variable_name" ); //] @@ -64,7 +64,7 @@ int main(int argc, char* argv[]) { //[getting_started_imports_cpp_variable // Importing variable. - shared_ptr cpp_var = dll::import_symbol( + std::shared_ptr cpp_var = dll::import_symbol( path_to_shared_library, "cpp_variable_name" ); //] diff --git a/include/boost/dll/config.hpp b/include/boost/dll/config.hpp index df79ab6f..da170128 100644 --- a/include/boost/dll/config.hpp +++ b/include/boost/dll/config.hpp @@ -19,6 +19,10 @@ /// Define this macro to make Boost.DLL use C++17's std::filesystem::path and std::system_error. #define BOOST_DLL_USE_STD_FS BOOST_DLL_USE_STD_FS +/// Define this macro to make Boost.DLL use boost::shared_ptr instead of std::shared_ptr. This macro will be removed +/// after a few releases, consider migrating to std::shared_ptr. +#define BOOST_DLL_USE_BOOST_SHARED_PTR BOOST_DLL_USE_BOOST_SHARED_PTR + /// This namespace contains aliases to the Boost or C++17 classes. Aliases are configured using BOOST_DLL_USE_STD_FS macro. namespace boost { namespace dll { namespace fs { @@ -69,5 +73,29 @@ using boost::system::system_error; #endif // BOOST_DLL_USE_STD_FS + +#ifdef BOOST_DLL_USE_BOOST_SHARED_PTR + +#include + +namespace boost { namespace dll { namespace detail { + template + using shared_ptr = boost::shared_ptr; + using boost::make_shared; + +}}} + +#else // BOOST_DLL_USE_STD_FS + +#include + +namespace boost { namespace dll { namespace detail { + template + using shared_ptr = std::shared_ptr; + using std::make_shared; +}}} + +#endif // BOOST_DLL_USE_STD_FS + #endif // BOOST_DLL_DETAIL_PUSH_OPTIONS_HPP diff --git a/include/boost/dll/import.hpp b/include/boost/dll/import.hpp index ea31bbd2..f97e7d5d 100644 --- a/include/boost/dll/import.hpp +++ b/include/boost/dll/import.hpp @@ -9,7 +9,6 @@ #define BOOST_DLL_IMPORT_HPP #include -#include #include #include // std::addressof @@ -32,10 +31,10 @@ 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::shared_ptr f_; + boost::dll::detail::shared_ptr f_; public: - inline library_function(const boost::shared_ptr& lib, T* func_ptr) noexcept + inline library_function(const boost::dll::detail::shared_ptr& lib, T* func_ptr) noexcept : f_(lib, func_ptr) {} @@ -55,11 +54,10 @@ namespace detail { } }; - template using import_type = typename std::conditional< std::is_object::value, - boost::shared_ptr, + boost::dll::detail::shared_ptr, boost::dll::detail::library_function >::type; } // namespace detail @@ -71,7 +69,8 @@ namespace detail { /*! -* Returns callable object or boost::shared_ptr that holds the symbol imported +* 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. @@ -90,7 +89,7 @@ namespace detail { * \endcode * * \code -* boost::shared_ptr i = import_symbol("test_lib.so", "integer_name"); +* 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. @@ -99,7 +98,8 @@ namespace detail { * \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 boost::shared_ptr if T is an object type. +* \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. @@ -110,7 +110,7 @@ BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(const boost::dll::fs::path& lib, cons { using type = boost::dll::detail::import_type; - auto p = boost::make_shared(lib, mode); + auto p = boost::dll::detail::make_shared(lib, mode); return type(p, std::addressof(p->get(name))); } @@ -127,7 +127,7 @@ 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::make_shared(lib); + auto p = boost::dll::detail::make_shared(lib); return type(p, std::addressof(p->get(name))); } @@ -142,7 +142,7 @@ template BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(shared_library&& lib, const char* name) { using type = boost::dll::detail::import_type; - auto p = boost::make_shared( + auto p = boost::dll::detail::make_shared( std::move(lib) ); return type(p, std::addressof(p->get(name))); @@ -158,7 +158,8 @@ BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(shared_library&& lib, const std::stri /*! -* Returns callable object or boost::shared_ptr that holds the symbol imported +* 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. @@ -177,7 +178,7 @@ BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(shared_library&& lib, const std::stri * \endcode * * \code -* boost::shared_ptr i = import_alias("test_lib.so", "integer_alias_name"); +* std::shared_ptr i = import_alias("test_lib.so", "integer_alias_name"); * \endcode * * \code @@ -189,7 +190,8 @@ BOOST_DLL_IMPORT_RESULT_TYPE import_symbol(shared_library&& lib, const std::stri * \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 boost::shared_ptr if T is an object type. +* \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. @@ -200,7 +202,7 @@ BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::dll::fs::path& lib, const { using type = boost::dll::detail::import_type; - auto p = boost::make_shared(lib, mode); + auto p = boost::dll::detail::make_shared(lib, mode); return type(p, p->get(name)); } @@ -217,7 +219,7 @@ 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::make_shared(lib); + auto p = boost::dll::detail::make_shared(lib); return type(p, p->get(name)); } @@ -232,7 +234,7 @@ template BOOST_DLL_IMPORT_RESULT_TYPE import_alias(shared_library&& lib, const char* name) { using type = boost::dll::detail::import_type; - auto p = boost::make_shared( + auto p = boost::dll::detail::make_shared( std::move(lib) ); return type(p, p->get(name)); diff --git a/include/boost/dll/import_mangled.hpp b/include/boost/dll/import_mangled.hpp index 65a1ba5b..c1552083 100644 --- a/include/boost/dll/import_mangled.hpp +++ b/include/boost/dll/import_mangled.hpp @@ -19,7 +19,7 @@ # error This file requires C++11 at least! #endif -#include +#include #include #include @@ -39,10 +39,10 @@ namespace detail template class mangled_library_function { // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster. - boost::shared_ptr lib_; + boost::dll::detail::shared_ptr lib_; function_tuple f_; public: - constexpr mangled_library_function(const boost::shared_ptr& lib, Ts*... func_ptr) noexcept + constexpr mangled_library_function(const boost::dll::detail::shared_ptr& lib, Ts*... func_ptr) noexcept : lib_(lib) , f_(func_ptr...) {} @@ -72,11 +72,11 @@ template class mangled_library_mem_fn> { // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster. typedef mem_fn_tuple call_tuple_t; - boost::shared_ptr lib_; + boost::dll::detail::shared_ptr lib_; call_tuple_t f_; public: - constexpr mangled_library_mem_fn(const boost::shared_ptr& lib, typename Ts::mem_fn... func_ptr) noexcept + constexpr mangled_library_mem_fn(const boost::dll::detail::shared_ptr& lib, typename Ts::mem_fn... func_ptr) noexcept : lib_(lib) , f_(func_ptr...) {} @@ -111,7 +111,7 @@ struct mangled_import_type, true,false,false> //is function const std::string& name) { return type( - boost::make_shared(p.shared_lib()), + boost::dll::detail::make_shared(p.shared_lib()), std::addressof(p.get_function(name))...); } }; @@ -129,7 +129,7 @@ struct mangled_import_type, false, true, false> //is me const std::string & name, sequence * ) { - return type(boost::make_shared(p.shared_lib()), + return type(boost::dll::detail::make_shared(p.shared_lib()), p.get_mem_fn(name)...); } @@ -145,14 +145,14 @@ struct mangled_import_type, false, true, false> //is me template struct mangled_import_type, false, false, true> //is variable { - typedef boost::shared_ptr type; + typedef boost::dll::detail::shared_ptr type; static type make( const boost::dll::experimental::smart_library& p, const std::string& name) { return type( - boost::make_shared(p.shared_lib()), + boost::dll::detail::make_shared(p.shared_lib()), std::addressof(p.get_variable(name))); } @@ -175,7 +175,7 @@ struct mangled_import_type, false, false, true> //is variable */ /*! -* Returns callable object or boost::shared_ptr that holds the symbol imported +* Returns callable object or boost::dll::detail::shared_ptr 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. @@ -191,7 +191,7 @@ struct mangled_import_type, false, false, true> //is variable * \endcode * * \code -* boost::shared_ptr i = import_mangled("test_lib.so", "integer_name"); +* boost::dll::detail::shared_ptr i = import_mangled("test_lib.so", "integer_name"); * \endcode * * Additionally you can also import overloaded symbols, including member-functions. @@ -218,7 +218,7 @@ struct mangled_import_type, false, false, true> //is variable * \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 boost::shared_ptr if T is an object type. +* \return callable object if T is a function type, or boost::dll::detail::shared_ptr 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. @@ -280,7 +280,7 @@ template BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const char* name) { typedef typename boost::dll::experimental::detail::mangled_import_type> type; - boost::shared_ptr p = boost::make_shared(lib); + boost::dll::detail::shared_ptr p = boost::dll::detail::make_shared(lib); return type::boostmake(p, name); } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d2a52c11..ebde992d 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -42,6 +42,7 @@ project hidden /boost/filesystem//boost_filesystem /boost/system//boost_system + /boost/smart_ptr//boost_smart_ptr multi BOOST_SYSTEM_NO_DEPRECATED ; @@ -80,6 +81,7 @@ project [ run empty_library_info_test.cpp : : empty_library : always_show_run_output shared ] [ run ../example/getting_started.cpp : : getting_started_library : shared ] [ run ../example/tutorial1/tutorial1.cpp : : my_plugin_sum : shared ] + [ run ../example/tutorial1/tutorial1.cpp : : my_plugin_sum : shared BOOST_DLL_USE_BOOST_SHARED_PTR : tutorial1_boost_shared_ptr ] [ run ../example/tutorial2/tutorial2.cpp : : my_plugin_aggregator : shared ] [ run ../example/tutorial3/tutorial3.cpp : : my_plugin_aggregator my_plugin_sum : shared ] [ run ../example/tutorial4/load_self.cpp ../example/tutorial4/static_plugin.cpp diff --git a/test/shared_library_get_symbol_test.cpp b/test/shared_library_get_symbol_test.cpp index 5a6ff40c..acdf5e25 100644 --- a/test/shared_library_get_symbol_test.cpp +++ b/test/shared_library_get_symbol_test.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include // lib functions @@ -20,7 +21,7 @@ typedef void (say_hello_func) (); typedef int (increment) (int); typedef boost::fusion::vector, std::vector, std::vector, const std::vector*, std::vector* > do_share_res_t; -typedef boost::shared_ptr (do_share_t)( +typedef std::shared_ptr (do_share_t)( std::vector v1, std::vector& v2, const std::vector& v3, @@ -67,7 +68,7 @@ void refcountable_test(boost::dll::fs::path shared_library_path) { } std::vector v1(1, 1), v2(2, 2), v3(3, 3), v4(4, 4), v5(1000, 5); - boost::shared_ptr res = f(v1, v2, v3, &v4, &v5); + auto res = f(v1, v2, v3, &v4, &v5); BOOST_TEST(at_c<0>(*res).size() == 1); BOOST_TEST(at_c<0>(*res).front() == 1); BOOST_TEST(at_c<1>(*res).size() == 2); BOOST_TEST(at_c<1>(*res).front() == 2); @@ -82,10 +83,10 @@ void refcountable_test(boost::dll::fs::path shared_library_path) { } { - boost::shared_ptr i = import_symbol(shared_library_path, "integer_g"); + auto i = import_symbol(shared_library_path, "integer_g"); BOOST_TEST(*i == 100); - boost::shared_ptr i2; + decltype(i) i2; i.swap(i2); BOOST_TEST(*i2 == 100); } @@ -105,19 +106,19 @@ void refcountable_test(boost::dll::fs::path shared_library_path) { } { - boost::shared_ptr i = import_symbol(shared_library_path, "const_integer_g"); + auto i = import_symbol(shared_library_path, "const_integer_g"); BOOST_TEST(*i == 777); - boost::shared_ptr i2 = i; + auto i2 = i; i.reset(); BOOST_TEST(*i2 == 777); } { - boost::shared_ptr s = import_alias(shared_library_path, "info"); + auto s = import_alias(shared_library_path, "info"); BOOST_TEST(*s == "I am a std::string from the test_library (Think of me as of 'Hello world'. Long 'Hello world')."); - boost::shared_ptr s2; + decltype(s) s2; s.swap(s2); BOOST_TEST(*s2 == "I am a std::string from the test_library (Think of me as of 'Hello world'. Long 'Hello world')."); } diff --git a/test/test_library.cpp b/test/test_library.cpp index c472d85a..bdb5512e 100644 --- a/test/test_library.cpp +++ b/test/test_library.cpp @@ -13,11 +13,10 @@ #include #include +#include #include #include -#include -#include #include #define LIBRARY_API BOOST_SYMBOL_EXPORT @@ -49,7 +48,7 @@ namespace namespace1 { namespace namespace2 { namespace namespace3 { boost::fusion::vector, std::vector, std::vector, const std::vector*, std::vector* > do_share_res_t; - boost::shared_ptr do_share( + std::shared_ptr do_share( std::vector v1, std::vector& v2, const std::vector& v3, @@ -59,7 +58,7 @@ namespace namespace1 { namespace namespace2 { namespace namespace3 { { v2.back() = 777; v5->back() = 9990; - return boost::make_shared(v1, v2, v3, v4, v5); + return std::make_shared(v1, v2, v3, v4, v5); } std::string info("I am a std::string from the test_library (Think of me as of 'Hello world'. Long 'Hello world').");