diff --git a/example/plugin_api.hpp b/example/plugin_api.hpp index ffd76f7..7736166 100644 --- a/example/plugin_api.hpp +++ b/example/plugin_api.hpp @@ -5,8 +5,8 @@ // For more information, see http://www.boost.org -#ifndef BOOST_APPLICATION_MY_PLUGIN_API_HPP -#define BOOST_APPLICATION_MY_PLUGIN_API_HPP +#ifndef BOOST_PLUGIN_MY_PLUGIN_API_HPP +#define BOOST_PLUGIN_MY_PLUGIN_API_HPP //[plugapi class my_plugin_api @@ -18,5 +18,5 @@ public: }; //] -#endif // BOOST_APPLICATION_MY_PLUGIN_API_HPP +#endif // BOOST_PLUGIN_MY_PLUGIN_API_HPP diff --git a/example/shared_library_load_plugin.cpp b/example/shared_library_load_plugin.cpp index fb7aa5f..574b3a5 100644 --- a/example/shared_library_load_plugin.cpp +++ b/example/shared_library_load_plugin.cpp @@ -44,7 +44,7 @@ int main(int argc, char* argv[]) BOOST_ASSERT(argc >= 2); boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"plugin_library"); - application::shared_library sl(shared_library_path); + plugin::shared_library sl(shared_library_path); if (!sl.search_symbol("create_my_plugin")) { return 2; diff --git a/include/boost/plugin/detail/posix/shared_library_impl.hpp b/include/boost/plugin/detail/posix/shared_library_impl.hpp index ce8e00c..3e0b5e6 100644 --- a/include/boost/plugin/detail/posix/shared_library_impl.hpp +++ b/include/boost/plugin/detail/posix/shared_library_impl.hpp @@ -15,12 +15,12 @@ // // ----------------------------------------------------------------------------- -#ifndef BOOST_APPLICATION_SHARED_LIBRARY_IMPL_HPP -#define BOOST_APPLICATION_SHARED_LIBRARY_IMPL_HPP +#ifndef BOOST_PLUGIN_SHARED_LIBRARY_IMPL_HPP +#define BOOST_PLUGIN_SHARED_LIBRARY_IMPL_HPP -#include -#include -#include +#include +#include +#include #include #include @@ -37,7 +37,7 @@ #include #include -namespace boost { namespace application { +namespace boost { namespace plugin { class shared_library_impl : noncopyable { public: @@ -58,7 +58,7 @@ public: handle_ = dlopen(sl.c_str(), static_cast(mode)); if (!handle_) { - ec = boost::application::last_error_code(); + ec = boost::plugin::detail::last_error_code(); } } @@ -79,25 +79,28 @@ public: boost::swap(handle_, rhs.handle_); } - static character_types::string_type suffix() { + static library_path suffix() { // https://sourceforge.net/p/predef/wiki/OperatingSystems/ #if defined(__APPLE__) - return character_types::string_type(".dylib"); + return ".dylib"; #else - return character_types::string_type(".so"); + return ".so"; #endif } void* symbol_addr(const symbol_type &sb, boost::system::error_code &ec) const BOOST_NOEXCEPT { - void* symbol = 0; - - if (handle_) { - // dlsym - obtain the address of a symbol from a dlopen object - symbol = dlsym(handle_, sb.data()); + if (!handle_) { + ec = boost::system::error_code( + boost::system::errc::bad_file_descriptor, + boost::system::generic_category() + ); + return NULL; } - + + // dlsym - obtain the address of a symbol from a dlopen object + void* symbol = dlsym(handle_, sb.data()); if (symbol == NULL) { - ec = boost::application::last_error_code(); + ec = boost::plugin::detail::last_error_code(); } // If handle does not refer to a valid object opened by dlopen(), @@ -112,7 +115,7 @@ private: void* handle_; }; -}} // boost::application +}} // boost::plugin -#endif // BOOST_APPLICATION_SHARED_LIBRARY_IMPL_HPP +#endif // BOOST_PLUGIN_SHARED_LIBRARY_IMPL_HPP diff --git a/include/boost/plugin/detail/system_error.hpp b/include/boost/plugin/detail/system_error.hpp new file mode 100644 index 0000000..b2bd164 --- /dev/null +++ b/include/boost/plugin/detail/system_error.hpp @@ -0,0 +1,65 @@ +// system_error.hpp ----------------------------------------------------------// +// ----------------------------------------------------------------------------- + +// Copyright 2011-2013 Renato Tegon Forti +// +// 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) + +// See http://www.boost.org/libs/application for documentation. + +// ----------------------------------------------------------------------------- + +// Revision History +// 18-10-2013 dd-mm-yyyy - Initial Release + +// ----------------------------------------------------------------------------- + +#ifndef BOOST_PLUGIN_SYSTEM_ERROR_HPP +#define BOOST_PLUGIN_SYSTEM_ERROR_HPP + +#include +#include +#include +#include +#include + +#if BOOST_OS_WINDOWS +# include +#else +# include +#endif + +namespace boost { namespace plugin { namespace detail { + + inline boost::system::error_code last_error_code() BOOST_NOEXCEPT { + // dl* functions report errors using NULL and dlerror() + return boost::system::error_code( +#if BOOST_OS_WINDOWS + boost::detail::winapi::GetLastError(), + boost::system::system_category() +#else + boost::system::errc::bad_file_descriptor, + boost::system::generic_category() +#endif + ); + } + + inline void report_error(const boost::system::error_code& ec, const char* message) { + // TODO: error category for DSO + boost::throw_exception( + boost::system::system_error( + ec, +#if BOOST_OS_WINDOWS + message +#else + message + std::string(". System message (dlerror): ") + dlerror() + std::string(". //") +#endif + ) + ); + } + +}}} // boost::plugin::detail + +#endif // BOOST_PLUGIN_SYSTEM_ERROR_HPP + diff --git a/include/boost/plugin/detail/windows/shared_library_impl.hpp b/include/boost/plugin/detail/windows/shared_library_impl.hpp index 4800842..e336b23 100644 --- a/include/boost/plugin/detail/windows/shared_library_impl.hpp +++ b/include/boost/plugin/detail/windows/shared_library_impl.hpp @@ -14,18 +14,18 @@ // ----------------------------------------------------------------------------- -#ifndef BOOST_APPLICATION_SHARED_LIBRARY_IMPL_HPP -#define BOOST_APPLICATION_SHARED_LIBRARY_IMPL_HPP +#ifndef BOOST_PLUGIN_SHARED_LIBRARY_IMPL_HPP +#define BOOST_PLUGIN_SHARED_LIBRARY_IMPL_HPP -#include -#include -#include +#include +#include +#include #include #include #include -namespace boost { namespace application { +namespace boost { namespace plugin { class shared_library_impl : noncopyable { public: @@ -47,7 +47,7 @@ public: DWORD flags = static_cast(mode); handle_ = LoadLibraryExW(sh.c_str(), 0, flags); if (!handle_) { - ec = boost::application::last_error_code(); + ec = boost::plugin::detail::last_error_code(); } } @@ -66,12 +66,8 @@ public: boost::swap(handle_, rhs.handle_); } - static character_types::string_type suffix() { -#if defined(BOOST_APPLICATION_STD_WSTRING) + static library_path suffix() { return character_types::string_type(L".dll"); -#else - return character_types::string_type(".dll"); -#endif } void* symbol_addr(const symbol_type &sb, boost::system::error_code &ec) const BOOST_NOEXCEPT { @@ -80,20 +76,25 @@ public: // There can be it and is correct, as in executed // units names of functions are stored in narrow characters. - if (handle_) { - return (void*) GetProcAddress(handle_, sb.data()); - } else { - ec = boost::application::last_error_code(); + if (!handle_) { + ec = bad_file_descriptor; + return NULL; + } + + // dlsym - obtain the address of a symbol from a dlopen object + void* symbol = dlsym(handle_, sb.data()); + if (symbol == NULL) { + ec = boost::plugin::detail::last_error_code(); } - return NULL; + return symbol; } private: HMODULE handle_; }; -}} // boost::application +}} // boost::plugin -#endif // BOOST_APPLICATION_SHARED_LIBRARY_IMPL_HPP +#endif // BOOST_PLUGIN_SHARED_LIBRARY_IMPL_HPP diff --git a/include/boost/plugin/shared_library.hpp b/include/boost/plugin/shared_library.hpp index aafdeb7..158080e 100644 --- a/include/boost/plugin/shared_library.hpp +++ b/include/boost/plugin/shared_library.hpp @@ -14,20 +14,23 @@ // ----------------------------------------------------------------------------- -#ifndef BOOST_APPLICATION_SHARED_LIBRARY_HPP -#define BOOST_APPLICATION_SHARED_LIBRARY_HPP +#ifndef BOOST_PLUGIN_SHARED_LIBRARY_HPP +#define BOOST_PLUGIN_SHARED_LIBRARY_HPP -#include -#include -#if defined( BOOST_WINDOWS_API ) -#include -#elif defined( BOOST_POSIX_API ) -#include +#include +#include + +#include +#include + +#if BOOST_OS_WINDOWS +# include #else -#error "Sorry, no boost application are available for this platform." +# include #endif -namespace boost { namespace application { + +namespace boost { namespace plugin { /*! * \brief This class can be used to load a @@ -35,7 +38,7 @@ namespace boost { namespace application { * as dynamic shared objects (DSO's) and invoke their exported * symbols. * -* Provides a means to extend your application using plugins way. +* Provides a means to extend your plugin using plugins way. * */ class shared_library: private shared_library_impl { @@ -158,8 +161,7 @@ public: if (ec) { path_.clear(); - BOOST_APPLICATION_THROW_LAST_SYSTEM_ERROR_USING_MY_EC( - "load() failed", ec); + boost::plugin::detail::report_error(ec, "load() failed"); } } @@ -212,8 +214,7 @@ public: if (ec) { path_.clear(); - BOOST_APPLICATION_THROW_LAST_SYSTEM_ERROR_USING_MY_EC( - "load() failed", ec); + boost::plugin::detail::report_error(ec, "load() failed"); } } @@ -305,8 +306,7 @@ public: void* ret = base_t::symbol_addr(sb, ec); if (ec || !ret) { - BOOST_APPLICATION_THROW_LAST_SYSTEM_ERROR_USING_MY_EC( - "get() failed", ec); + boost::plugin::detail::report_error(ec, "get() failed"); } return *reinterpret_cast(ret); @@ -334,7 +334,7 @@ public: * .dylib (mac) * */ - static character_types::string_type suffix() { + static boost::filesystem::path suffix() { return base_t::suffix(); } @@ -376,7 +376,7 @@ inline void swap(shared_library& lhs, shared_library& rhs) BOOST_NOEXCEPT { lhs.swap(rhs); } -}} // boost::application +}} // boost::plugin -#endif // BOOST_APPLICATION_SHARED_LIBRARY_HPP +#endif // BOOST_PLUGIN_SHARED_LIBRARY_HPP diff --git a/include/boost/plugin/shared_library_load_mode.hpp b/include/boost/plugin/shared_library_load_mode.hpp index 9a58be1..972bcbd 100644 --- a/include/boost/plugin/shared_library_load_mode.hpp +++ b/include/boost/plugin/shared_library_load_mode.hpp @@ -1,7 +1,8 @@ // shared_library_mode.hpp ----------------------------------------------------// // ----------------------------------------------------------------------------- -// Copyright 2011-2013 Renato Tegon Forti +// Copyright 2011-2013 Renato Tegon Forti. +// Copyright 2014 Renato Tegon Forti, Antony Polukhin. // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt @@ -13,19 +14,18 @@ // ----------------------------------------------------------------------------- -#ifndef BOOST_APPLICATION_SHARED_LIBRARY_MODE_HPP -#define BOOST_APPLICATION_SHARED_LIBRARY_MODE_HPP +#ifndef BOOST_PLUGIN_SHARED_LIBRARY_MODE_HPP +#define BOOST_PLUGIN_SHARED_LIBRARY_MODE_HPP -#include +#include +#include // MINGW NOTE // // in mingw we have some problems here, at this time we don't support shared_library for __MINGW32__ // you can download port yourself from: https://code.google.com/p/dlfcn-win32/downloads/list -#if defined( BOOST_POSIX_API ) -#include -#elif defined( BOOST_WINDOWS_API ) +#if BOOST_OS_WINDOWS // workaround [ # ifndef DONT_RESOLVE_DLL_REFERENCES @@ -50,8 +50,8 @@ // - -# ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR -# define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200 +# ifndef LOAD_LIBRARY_SEARCH_PLUGIN_DIR +# define LOAD_LIBRARY_SEARCH_PLUGIN_DIR 0x00000200 # endif # ifndef LOAD_LIBRARY_SEARCH_DEFAULT_DIRS @@ -70,9 +70,11 @@ # define LOAD_LIBRARY_SEARCH_USER_DIRS 0x00000400 # endif // ] end workaround +#else +# include #endif -namespace boost { namespace application { +namespace boost { namespace plugin { /*! \enum Modes of load library. * @@ -117,20 +119,20 @@ namespace boost { namespace application { * If forced integrity checking is desired for the loaded file then * LOAD_LIBRARY_AS_IMAGE is recommended instead. * - * LOAD_LIBRARY_SEARCH_APPLICATION_DIR - * If this value is used, the application's installation directory is searched + * LOAD_LIBRARY_SEARCH_PLUGIN_DIR + * If this value is used, the plugin's installation directory is searched * for the DLL and its dependencies. Directories in the standard search path * are not searched. * This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH. * * LOAD_LIBRARY_SEARCH_DEFAULT_DIRS - * This value is a combination of LOAD_LIBRARY_SEARCH_APPLICATION_DIR, + * This value is a combination of LOAD_LIBRARY_SEARCH_PLUGIN_DIR, * LOAD_LIBRARY_SEARCH_SYSTEM32, and LOAD_LIBRARY_SEARCH_USER_DIRS. * * Directories in the standard search path are not searched. * This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH. * This value represents the recommended maximum number of directories - * an application should include in its DLL search path. + * an plugin should include in its DLL search path. * * LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR * If this value is used, the directory that contains the DLL is temporarily @@ -178,7 +180,7 @@ namespace boost { namespace application { * All necessary relocations shall be performed when the object is first * loaded. This may waste some processing if relocations are performed for * functions that are never referenced. This behavior may be useful for - * applications that need to know as soon as an object is loaded that all + * plugins that need to know as soon as an object is loaded that all * symbols referenced during execution are available. * * Any object loaded by dlopen() that requires relocations against global @@ -206,7 +208,7 @@ namespace boost { namespace application { */ enum shared_library_load_mode { -#if defined( BOOST_WINDOWS_API ) +#if BOOST_OS_WINDOWS // windows load_library_default_mode = 0, dont_resolve_dll_references = DONT_RESOLVE_DLL_REFERENCES, // 0x00000001 @@ -215,7 +217,7 @@ namespace boost { namespace application { load_library_as_datafile_exclusive = LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE, // 0x00000040 load_library_as_image_resource = LOAD_LIBRARY_AS_IMAGE_RESOURCE, // 0x00000020 - // About LOAD_LIBRARY_SEARCH_APPLICATION_DIR, + // About LOAD_LIBRARY_SEARCH_PLUGIN_DIR, // LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, // LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR, // LOAD_LIBRARY_SEARCH_SYSTEM32 and @@ -229,7 +231,7 @@ namespace boost { namespace application { # ifndef _USING_V110_SDK71_ // when user uses: Visual Studio 2012 - Windows XP (v110_xp), we need hide following enums : - load_library_search_application_dir = LOAD_LIBRARY_SEARCH_APPLICATION_DIR, // 0x00000200 + load_library_search_plugin_dir = LOAD_LIBRARY_SEARCH_PLUGIN_DIR, // 0x00000200 load_library_search_default_dirs = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, // 0x00001000 load_library_search_dll_load_dir = LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR, // 0x00000100 load_library_search_system32 = LOAD_LIBRARY_SEARCH_SYSTEM32, // 0x00000800 @@ -239,7 +241,7 @@ namespace boost { namespace application { load_with_altered_search_path = LOAD_WITH_ALTERED_SEARCH_PATH // 0x00000008 -#elif defined( BOOST_POSIX_API ) +#else // posix rtld_lazy = RTLD_LAZY, // 1 rtld_now = RTLD_NOW, // 2 @@ -303,6 +305,6 @@ namespace boost { namespace application { return (left); } -}} // boost::application +}} // boost::plugin -#endif // BOOST_APPLICATION_SHARED_LIBRARY_MODE_HPP +#endif // BOOST_PLUGIN_SHARED_LIBRARY_MODE_HPP diff --git a/include/boost/plugin/shared_library_types.hpp b/include/boost/plugin/shared_library_types.hpp index f69c2cf..4eefe64 100644 --- a/include/boost/plugin/shared_library_types.hpp +++ b/include/boost/plugin/shared_library_types.hpp @@ -13,15 +13,13 @@ // ----------------------------------------------------------------------------- -#ifndef BOOST_APPLICATION_SHARED_LIBRARY_TYPES_HPP -#define BOOST_APPLICATION_SHARED_LIBRARY_TYPES_HPP +#ifndef BOOST_PLUGIN_SHARED_LIBRARY_TYPES_HPP +#define BOOST_PLUGIN_SHARED_LIBRARY_TYPES_HPP -#include -#include #include #include -namespace boost { namespace application { +namespace boost { namespace plugin { /*! * \brief This is a path. @@ -45,7 +43,7 @@ namespace boost { namespace application { */ typedef boost::string_ref symbol_type; -}} // boost::application +}} // boost::plugin -#endif // BOOST_APPLICATION_SHARED_LIBRARY_TYPES_HPP +#endif // BOOST_PLUGIN_SHARED_LIBRARY_TYPES_HPP diff --git a/test/shared_library_get_symbol_test.cpp b/test/shared_library_get_symbol_test.cpp index 38f3a7b..cb6d3f0 100644 --- a/test/shared_library_get_symbol_test.cpp +++ b/test/shared_library_get_symbol_test.cpp @@ -37,13 +37,13 @@ typedef int (increment) (int); // Unit Tests int test_main(int argc, char* argv[]) { - using namespace boost::application; + using namespace boost::plugin; BOOST_CHECK(argc >= 2); boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"test_library"); std::cout << "Library: " << shared_library_path; - shared_library sl(shared_library_path); + shared_library sl(shared_library_path/"qweqwe"); BOOST_CHECK(sl.get("integer_g") == 100); diff --git a/test/shared_library_load_test.cpp b/test/shared_library_load_test.cpp index f4e23a0..4d7b013 100644 --- a/test/shared_library_load_test.cpp +++ b/test/shared_library_load_test.cpp @@ -26,7 +26,7 @@ fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) int test_main(int argc, char* argv[]) { - using namespace boost::application; + using namespace boost::plugin; BOOST_CHECK(argc >= 2); boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"test_library"); diff --git a/test/shared_library_search_symbol_test.cpp b/test/shared_library_search_symbol_test.cpp index 2ed9737..bdcfc74 100644 --- a/test/shared_library_search_symbol_test.cpp +++ b/test/shared_library_search_symbol_test.cpp @@ -28,7 +28,7 @@ fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) int test_main(int argc, char* argv[]) { - using namespace boost::application; + using namespace boost::plugin; BOOST_CHECK(argc >= 2); boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"test_library");