diff --git a/doc/getting_started.qbk b/doc/getting_started.qbk index 47e4774..5e9c43b 100644 --- a/doc/getting_started.qbk +++ b/doc/getting_started.qbk @@ -13,17 +13,17 @@ To start with the library you only need to include `` header. [import ../example/getting_started_library.cpp] [table:starting - [[ In plugin (DLL/DSL) sources: ] [ In code that uses plugin: ]] - [[ [getting_started_exports_c_function] ] [ [getting_started_imports_c_function] ]] - [[ [getting_started_exports_c_variable] ] [ [getting_started_imports_c_variable] ]] - [[ [getting_started_exports_cpp_function] ] [ [getting_started_imports_cpp_function] ]] - [[ [getting_started_exports_cpp_variable] ] [ [getting_started_imports_cpp_variable] ]] +[[ In plugin (DLL/DSL) sources: ] [ In code that uses plugin: ] [ Function description: ]] +[[ [getting_started_exports_c_function] ] [ [getting_started_imports_c_function] ] [ [funcref boost::plugin::shared_function shared_function ] ]] +[[ [getting_started_exports_c_variable] ] [ [getting_started_imports_c_variable] ] [ [funcref boost::plugin::shared_variable shared_variable ] ]] +[[ [getting_started_exports_cpp_function] ] [ [getting_started_imports_cpp_function] ] [ [funcref boost::plugin::shared_function_alias shared_function_alias] ]] +[[ [getting_started_exports_cpp_variable] ] [ [getting_started_imports_cpp_variable] ] [ [funcref boost::plugin::shared_variable_alias shared_variable_alias] ]] ] In all the cases above it is safe to use imported variable or function because the `boost::function` or `boost::shared_ptr` internaly hold a reference to shread library. -If you need a low level api `boost::plugin::shared_library` and `boost::plugin::alias` will suit you. If you want to load a library, +If you need a low level api [classref boost::plugin::shared_library] and [funcref boost::plugin::alias] will suit you. If you want to load a library, just cunstruct that class with a path to the library as a parameter: ``` boost::plugin::shared_library lib("/test/boost/application/libtest_library.so"); diff --git a/doc/plugin.qbk b/doc/plugin.qbk index f18ee90..880c5b3 100644 --- a/doc/plugin.qbk +++ b/doc/plugin.qbk @@ -6,7 +6,7 @@ [library Boost.Plugin [quickbook 1.5] - [authors [Forti, Renato, Antony Polukhin]] + [authors [Forti, Renato][Polukhin, Antony]] [copyright 2014 Renato Tegon Forti, Antony Polukhin] [id plugin] [dirname plugin] diff --git a/doc/tutorial.qbk b/doc/tutorial.qbk index 220d1d4..7f45066 100644 --- a/doc/tutorial.qbk +++ b/doc/tutorial.qbk @@ -6,29 +6,76 @@ [section Tutorial] -tutorial is provided to give you a idea, of how to use shared library class. +Tutorial is provided to give you an idea of how to create and use plugins. -The first thing to do is define the plugin interface, this will be a abstract class that will be our plugin API, and we will have a cpp file that will hold a implementation of our plugin. -[import ../example/plugin_api.hpp] + +[section Plugin basics] + +The first thing to do when creating your own plugins is define the plugin interface. There is an example +of an abstract class that will be our plugin API: + +[import ../example/tutorial_common/plugin_api.hpp] [plugapi] -[import ../example/plugin_library.cpp] -[plugcpp] +Now let's make a DLL/DSO library that will holds implementation of plugin interface and exports it using the +[macroref BOOST_PLUGIN_ALIAS]: -Now our application that will use our plugin. +[import ../example/tutorial1/my_plugin_sum.cpp] +[plugcpp_my_plugin_sum] -[import ../example/shared_library_load_plugin.cpp] -[callplugcpp] +Simple application that loads plugin using the [funcref boost::plugin::shared_variable_alias]: -The output: +[import ../example/tutorial1/tutorial1.cpp] +[callplugcpp_tutorial1] + +That application will output: [pre +Application started +Loading the plugin +Constructing my_plugin_sum Plugin Version: 1 Plugin Method: 3 -;o) +Destructing my_plugin_sum ;o) ] [endsect] + +[section Factory method in plugin] + +In previous example we were importing from a plugin a single variable. Let's make a class +that uses our plugin API plugin and holds some state: + +[import ../example/tutorial2/my_plugin_aggregator.cpp] +[plugcpp_my_plugin_aggregator] + +As you may see, `my_namespace::create_plugin` is a factory method, that creates +instances of `my_namespace::my_plugin_aggregator`. We export that method with the name "create_plugin" +using [macroref BOOST_PLUGIN_ALIAS]. + +[import ../example/tutorial2/tutorial2.cpp] +[callplugcpp_tutorial2] + +In that application we have imported the factory method using [funcref boost::plugin::shared_function_alias]. + +[caution Be careful: `creator` variable holds a reference to the loaded shared library. If this +variable goes out of scope or will be reset, then the *DLL/DSO will be unloaded* and any attempt to +dereference the `plugin` variable will lead to *undefined behavior*. ] + +Output of the application will be the following: + +[pre +Plugin Version: 1 +Plugin Method: 3 +Plugin Method second call: 6 +Plugin Name: aggregator +] + +[endsect] + +[endsect] + + diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 index 4166842..0adc5a4 100644 --- a/example/Jamfile.v2 +++ b/example/Jamfile.v2 @@ -20,10 +20,11 @@ project ; # our example plugin lib - lib plugin_library : plugin_library.cpp ; + lib my_plugin_sum : tutorial1/my_plugin_sum.cpp ; + lib my_plugin_aggregator : tutorial2/my_plugin_aggregator.cpp ; lib getting_started_library : getting_started_library.cpp ; install install-bin - : plugin_library getting_started_library : + : my_plugin_sum my_plugin_aggregator getting_started_library : windows:"C:/test/boost/application" linux:/test/boost/application ; diff --git a/example/Jamfile.v2.orig b/example/Jamfile.v2.orig new file mode 100644 index 0000000..4166842 --- /dev/null +++ b/example/Jamfile.v2.orig @@ -0,0 +1,38 @@ +# +# Copyright Renato Tegon Forti 2011 - 2013. +# 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) +# + +project + : source-location . + : requirements + + # linux + linux:"-ldl" + + # others + shared:BOOST_ALL_DLL + /boost/system//boost_system + /boost/filesystem//boost_filesystem + multi + ; + + # our example plugin lib + lib plugin_library : plugin_library.cpp ; + lib getting_started_library : getting_started_library.cpp ; + install install-bin + : plugin_library getting_started_library : + windows:"C:/test/boost/application" + linux:/test/boost/application ; + + +# shared library sub-system +exe shared_library_load_plugin + : shared_library_load_plugin.cpp + ; + +exe getting_started + : getting_started.cpp + ; diff --git a/example/getting_started.cpp b/example/getting_started.cpp index 276955b..88b03ce 100644 --- a/example/getting_started.cpp +++ b/example/getting_started.cpp @@ -11,34 +11,20 @@ #include #include -#include -namespace fs = ::boost::filesystem; -fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) { - fs::directory_iterator it(root); - fs::directory_iterator endit; - - while (it != endit) { - if (fs::is_regular_file(*it) && it->path().filename().wstring().find(filename_part) != std::wstring::npos) { - return *it; - } - ++it; - } - - throw std::runtime_error("Failed to find library"); -} +#include "shared_lib_path.hpp" // Unit Tests int test_main(int argc, char* argv[]) { using namespace boost::plugin; BOOST_CHECK(argc >= 2); - - boost::filesystem::path path_to_shared_library = get_shared_lib(argv[1], L"getting_started_library"); + boost::filesystem::path path_to_shared_library = shared_lib_path(argv[1], L"getting_started_library"); //[getting_started_imports_c_function - boost::function c_func = boost::plugin::shared_function( - path_to_shared_library, "c_func_name" - ); + boost::function c_func + = boost::plugin::shared_function( + path_to_shared_library, "c_func_name" + ); //] int c_func_res = c_func(1); // calling the function @@ -46,9 +32,10 @@ int test_main(int argc, char* argv[]) { //[getting_started_imports_c_variable - boost::shared_ptr c_var = boost::plugin::shared_variable( - path_to_shared_library, "c_variable_name" - ); + boost::shared_ptr c_var + = boost::plugin::shared_variable( + path_to_shared_library, "c_variable_name" + ); //] int c_var_old_contents = *c_var; // using the variable @@ -58,18 +45,22 @@ int test_main(int argc, char* argv[]) { //[getting_started_imports_cpp_function typedef std::string(cpp_func_type)(const std::string&); - boost::function cpp_func = boost::plugin::shared_function_alias( - path_to_shared_library, "cpp_function_alias_name" - ); + + boost::function cpp_func + = boost::plugin::shared_function_alias( + path_to_shared_library, "cpp_function_alias_name" + ); //] - std::string cpp_func_res = cpp_func(std::string("In importer.")); // calling the function + // calling the function + std::string cpp_func_res = cpp_func(std::string("In importer.")); BOOST_CHECK(cpp_func_res == "In importer. Hello from lib!"); //[getting_started_imports_cpp_variable - boost::shared_ptr cpp_var = boost::plugin::shared_variable_alias( - path_to_shared_library, "cpp_variable_alias_name" - ); + boost::shared_ptr cpp_var + = boost::plugin::shared_variable_alias( + path_to_shared_library, "cpp_variable_alias_name" + ); //] std::string cpp_var_old_contents = *cpp_var; // using the variable diff --git a/example/getting_started_library.cpp b/example/getting_started_library.cpp index c39abdc..64f80a7 100644 --- a/example/getting_started_library.cpp +++ b/example/getting_started_library.cpp @@ -25,7 +25,9 @@ namespace my_namespace { std::string cpp_function_name(const std::string& param); } -BOOST_PLUGIN_ALIAS(my_namespace::cpp_function_name, cpp_function_alias_name) +BOOST_PLUGIN_ALIAS( + my_namespace::cpp_function_name, cpp_function_alias_name +) //] //[getting_started_exports_cpp_variable @@ -34,7 +36,9 @@ namespace my_namespace { std::string cpp_variable_name = "some value"; } -BOOST_PLUGIN_ALIAS(my_namespace::cpp_variable_name, cpp_variable_alias_name) +BOOST_PLUGIN_ALIAS( + my_namespace::cpp_variable_name, cpp_variable_alias_name +) //] diff --git a/example/plugin_library.cpp.rej b/example/plugin_library.cpp.rej new file mode 100644 index 0000000..899fd30 --- /dev/null +++ b/example/plugin_library.cpp.rej @@ -0,0 +1,95 @@ +--- example/plugin_library.cpp ++++ /dev/null +@@ -1,92 +0,0 @@ +-// 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) +- +-// For more information, see http://www.boost.org +- +-#include +-#include +- +-//[plugcpp +- +-#include "plugin_api.hpp" +-#include +- +-namespace my_namespace { +- +-class my_plugin_sum : public my_plugin_api { +-public: +- +- float version() { +- return 1.0; +- } +- +- float calculate(float x, float y) { +- return x + y; +- } +- +- ~my_plugin_sum() { +- std::cout << ";o)" << std::endl; +- } +-}; +- +-boost::shared_ptr create_my_plugin() { +- return boost::make_shared(); +-} +- +-} // namespace my_namespace +- +-BOOST_PLUGIN_ALIAS(my_namespace::create_my_plugin, create_plugin) +-//] +- +- +-// platform dependent initialization sample +-// +- +-#if defined( BOOST_WINDOWS_API ) +- +-// return TRUE on success and FALSE if an error occurs. returning +-// FALSE will cause the library to be unloaded. +-BOOL WINAPI DllMain +-( +- HINSTANCE hinstDLL, +- DWORD fdwReason, +- LPVOID lpReserved +-) +-{ +- switch (fdwReason) +- { +- case DLL_PROCESS_ATTACH: +- // add initialization code... +- break; +- case DLL_PROCESS_DETACH: +- // add clean-up code... +- break; +- } +- +- return (TRUE); +-} +- +-#elif defined( BOOST_POSIX_API ) +- +-// GCC +-void __attribute__ ((constructor)) my_load(void); +-void __attribute__ ((destructor)) my_unload(void); +- +-// called when the library is loaded and before dlopen() returns +-void my_load(void) +-{ +- // Add initialization code +-} +- +-// called when the library is unloaded and before dlclose() returns +-void my_unload(void) +-{ +- // Add clean-up code +-} +- +-#endif +- +- +- diff --git a/example/shared_lib_path.hpp b/example/shared_lib_path.hpp new file mode 100644 index 0000000..79f3871 --- /dev/null +++ b/example/shared_lib_path.hpp @@ -0,0 +1,27 @@ +// Copyright 2011-2013 Renato Tegon Forti. +// Copyright 2014 Renato Tegon Forti, 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) + +// For more information, see http://www.boost.org + +#ifndef BOOST_PLUGIN_EXAMPLE_COMMON_SHARED_LIB_PATH_HPP +#define BOOST_PLUGIN_EXAMPLE_COMMON_SHARED_LIB_PATH_HPP + +#include +boost::filesystem::path shared_lib_path(const boost::filesystem::path& root, const std::wstring& filename_part) { + namespace fs = ::boost::filesystem; + fs::directory_iterator endit; + + for (fs::directory_iterator it(root); it != endit; ++it) { + if (fs::is_regular_file(*it) && it->path().filename().wstring().find(filename_part) != std::wstring::npos) { + return *it; + } + } + + throw std::runtime_error("Failed to find library"); +} + +#endif // BOOST_PLUGIN_EXAMPLE_COMMON_SHARED_LIB_PATH_HPP + diff --git a/example/tutorial1/my_plugin_sum.cpp b/example/tutorial1/my_plugin_sum.cpp new file mode 100644 index 0000000..af8fda7 --- /dev/null +++ b/example/tutorial1/my_plugin_sum.cpp @@ -0,0 +1,98 @@ +// Copyright 2011-2013 Renato Tegon Forti. +// Copyright 2014 Renato Tegon Forti, 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) + +// For more information, see http://www.boost.org + +#include +#include + +//[plugcpp_my_plugin_sum + +#include "../tutorial_common/plugin_api.hpp" +#include + +namespace my_namespace { + +class my_plugin_sum : public my_plugin_api { +public: + my_plugin_sum() { + std::cout << "Constructing my_plugin_sum" << std::endl; + } + + float version() const { + return 1.0; + } + + std::string name() const { + return "sum"; + } + + float calculate(float x, float y) { + return x + y; + } + + ~my_plugin_sum() { + std::cout << "Destructing my_plugin_sum ;o)" << std::endl; + } +}; + +my_plugin_sum plugin; + +} // namespace my_namespace + +BOOST_PLUGIN_ALIAS(my_namespace::plugin, plugin) +//] + + +// platform dependent initialization sample +// + +#if defined( BOOST_WINDOWS_API ) + +// return TRUE on success and FALSE if an error occurs. returning +// FALSE will cause the library to be unloaded. +BOOL WINAPI DllMain +( + HINSTANCE hinstDLL, + DWORD fdwReason, + LPVOID lpReserved +) +{ + switch (fdwReason) + { + case DLL_PROCESS_ATTACH: + // add initialization code... + break; + case DLL_PROCESS_DETACH: + // add clean-up code... + break; + } + + return (TRUE); +} + +#elif defined( BOOST_POSIX_API ) + +// GCC +void __attribute__ ((constructor)) my_load(void); +void __attribute__ ((destructor)) my_unload(void); + +// called when the library is loaded and before dlopen() returns +void my_load(void) +{ + // Add initialization code +} + +// called when the library is unloaded and before dlclose() returns +void my_unload(void) +{ + // Add clean-up code +} + +#endif + + + diff --git a/example/tutorial1/tutorial1.cpp b/example/tutorial1/tutorial1.cpp new file mode 100644 index 0000000..a2b20f9 --- /dev/null +++ b/example/tutorial1/tutorial1.cpp @@ -0,0 +1,36 @@ +// Copyright 2011-2012 Renato Tegon Forti. +// Copyright 2014 Renato Tegon Forti, 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) + +// For more information, see http://www.boost.org + +// ------------------------------------------------------------------------------------- +// This example shows how to shared_variable_alias function to load a plugin of a DSO. +// ------------------------------------------------------------------------------------- + +#include "../shared_lib_path.hpp" + +//[callplugcpp_tutorial1 +#include +#include +#include "../tutorial_common/plugin_api.hpp" + +int main(int argc, char* argv[]) { + std::cout << "Application started" << std::endl; + + // argv[1] contains path to our plugin library + BOOST_ASSERT(argc >= 2); + boost::filesystem::path shared_library_path = shared_lib_path(argv[1], L"my_plugin_sum"); + + std::cout << "Loading the plugin" << std::endl; + boost::shared_ptr plugin + = boost::plugin::shared_variable_alias( + shared_library_path, "plugin" + ); + + std::cout << "Plugin Version: " << plugin->version() << std::endl; + std::cout << "Plugin Method: " << plugin->calculate(1.5, 1.5) << std::endl; +} +//] diff --git a/example/tutorial2/my_plugin_aggregator.cpp b/example/tutorial2/my_plugin_aggregator.cpp new file mode 100644 index 0000000..8a809f2 --- /dev/null +++ b/example/tutorial2/my_plugin_aggregator.cpp @@ -0,0 +1,48 @@ +// Copyright 2011-2013 Renato Tegon Forti. +// Copyright 2014 Renato Tegon Forti, 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) + +// For more information, see http://www.boost.org + +#include +#include + +//[plugcpp_my_plugin_aggregator + +#include "../tutorial_common/plugin_api.hpp" +#include + +namespace my_namespace { + +class my_plugin_aggregator : public my_plugin_api { + float aggr_; +public: + my_plugin_aggregator() : aggr_(0) {} + + float version() const { + return 1.0; + } + + std::string name() const { + return "aggregator"; + } + + float calculate(float x, float y) { + aggr_ += x + y; + return aggr_; + } +}; + +boost::shared_ptr create_plugin() { + return boost::make_shared(); +} + +} // namespace my_namespace + +BOOST_PLUGIN_ALIAS(my_namespace::create_plugin, create_plugin) +//] + + + diff --git a/example/shared_library_load_plugin.cpp b/example/tutorial2/tutorial2.cpp similarity index 53% rename from example/shared_library_load_plugin.cpp rename to example/tutorial2/tutorial2.cpp index 4c33680..5de4dc1 100644 --- a/example/shared_library_load_plugin.cpp +++ b/example/tutorial2/tutorial2.cpp @@ -1,43 +1,26 @@ -// Copyright 2011-2012 Renato Tegon Forti +// Copyright 2011-2012 Renato Tegon Forti. +// Copyright 2014 Renato Tegon Forti, 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) // For more information, see http://www.boost.org -// ----------------------------------------------------------------------------- -// This example shows how to use shared_library class to load a plugin of a DSO. -// ----------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------- +// This example shows how to shared_variable_alias function to load a plugin of a DSO. +// ------------------------------------------------------------------------------------- -#define BOOST_ALL_DYN_LINK -#define BOOST_LIB_DIAGNOSTIC +#include "../shared_lib_path.hpp" -#include -#include -namespace fs = ::boost::filesystem; -fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) { - fs::directory_iterator it(root); - fs::directory_iterator endit; - - while (it != endit) { - if (fs::is_regular_file(*it) && it->path().filename().wstring().find(filename_part) != std::wstring::npos) { - return *it; - } - ++it; - } - - throw std::runtime_error("Failed to find library"); -} - -//[callplugcpp +//[callplugcpp_tutorial2 #include -#include "plugin_api.hpp" - +#include +#include "../tutorial_common/plugin_api.hpp" int main(int argc, char* argv[]) { // argv[1] contains path to our plugin library BOOST_ASSERT(argc >= 2); - boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"plugin_library"); + boost::filesystem::path shared_library_path = shared_lib_path(argv[1], L"my_plugin_aggregator"); typedef boost::shared_ptr (pluginapi_create_t)(); boost::function creator @@ -47,5 +30,7 @@ int main(int argc, char* argv[]) { std::cout << "Plugin Version: " << plugin->version() << std::endl; std::cout << "Plugin Method: " << plugin->calculate(1.5, 1.5) << std::endl; + std::cout << "Plugin Method second call: " << plugin->calculate(1.5, 1.5) << std::endl; + std::cout << "Plugin Name: " << plugin->name() << std::endl; } //] diff --git a/example/plugin_api.hpp b/example/tutorial_common/plugin_api.hpp similarity index 83% rename from example/plugin_api.hpp rename to example/tutorial_common/plugin_api.hpp index 7a3c7f2..d523d94 100644 --- a/example/plugin_api.hpp +++ b/example/tutorial_common/plugin_api.hpp @@ -9,10 +9,13 @@ #define BOOST_PLUGIN_MY_PLUGIN_API_HPP //[plugapi +#include + class my_plugin_api { public: virtual ~my_plugin_api() {}; - virtual float version() = 0; + virtual float version() const = 0; + virtual std::string name() const = 0; virtual float calculate(float x, float y) = 0; }; //] diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 76e6784..1d76863 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -32,8 +32,9 @@ project # our test lib for shared library tests lib test_library : test_library.cpp ; lib getting_started_library : ../example/getting_started_library.cpp ; - lib plugin_library : ../example/plugin_library.cpp ; - install install-bin : test_library plugin_library getting_started_library : $(TEST_DIR) ; + lib my_plugin_sum : ../example/tutorial1/my_plugin_sum.cpp ; + lib my_plugin_aggregator : ../example/tutorial2/my_plugin_aggregator.cpp ; + install install-bin : test_library my_plugin_sum my_plugin_aggregator getting_started_library : $(TEST_DIR) ; test-suite application : @@ -41,7 +42,8 @@ project [ run shared_library_search_symbol_test.cpp : $(TEST_DIR) ] [ run shared_library_get_symbol_test.cpp : $(TEST_DIR) ] [ run shared_library_errors.cpp : $(TEST_DIR) : : always_show_run_output ] - [ run ../example/shared_library_load_plugin.cpp : $(TEST_DIR) ] [ run ../example/getting_started.cpp : $(TEST_DIR) ] + [ run ../example/tutorial1/tutorial1.cpp : $(TEST_DIR) ] + [ run ../example/tutorial2/tutorial2.cpp : $(TEST_DIR) ] ; } diff --git a/test/Jamfile.v2.orig b/test/Jamfile.v2.orig new file mode 100644 index 0000000..76e6784 --- /dev/null +++ b/test/Jamfile.v2.orig @@ -0,0 +1,47 @@ +# +# Copyright Renato Tegon Forti 2011 - 2013. +# 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) +# + +# For more information, see http://www.boost.org + +# bring in rules for testing +import testing ; +import path ; + +path-constant TEST_DIR : $(BOOST_ROOT)/bin.v2/libs/application/test ; + +project + : source-location . + : requirements + + # linux + linux:"-ldl" + + # others + shared:BOOST_ALL_DLL + /boost/system//boost_system + /boost/filesystem//boost_filesystem + multi + + ; +{ + + # our test lib for shared library tests + lib test_library : test_library.cpp ; + lib getting_started_library : ../example/getting_started_library.cpp ; + lib plugin_library : ../example/plugin_library.cpp ; + install install-bin : test_library plugin_library getting_started_library : $(TEST_DIR) ; + + test-suite application + : + [ run shared_library_load_test.cpp : $(TEST_DIR) ] + [ run shared_library_search_symbol_test.cpp : $(TEST_DIR) ] + [ run shared_library_get_symbol_test.cpp : $(TEST_DIR) ] + [ run shared_library_errors.cpp : $(TEST_DIR) : : always_show_run_output ] + [ run ../example/shared_library_load_plugin.cpp : $(TEST_DIR) ] + [ run ../example/getting_started.cpp : $(TEST_DIR) ] + ; +} diff --git a/test/shared_library_errors.cpp b/test/shared_library_errors.cpp index 1dbb57f..1a64de8 100644 --- a/test/shared_library_errors.cpp +++ b/test/shared_library_errors.cpp @@ -11,21 +11,7 @@ #include #include -#include -namespace fs = ::boost::filesystem; -fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) { - fs::directory_iterator it(root); - fs::directory_iterator endit; - - while (it != endit) { - if (fs::is_regular_file(*it) && it->path().filename().wstring().find(filename_part) != std::wstring::npos) { - return *it; - } - ++it; - } - - throw std::runtime_error("Failed to find library"); -} +#include "../example/shared_lib_path.hpp" // Unit Tests @@ -33,7 +19,7 @@ int test_main(int argc, char* argv[]) { using namespace boost::plugin; BOOST_CHECK(argc >= 2); - boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"test_library"); + boost::filesystem::path shared_library_path = shared_lib_path(argv[1], L"test_library"); boost::filesystem::path bad_path = shared_library_path / "directory_that_does_not_exist"; try { diff --git a/test/shared_library_get_symbol_test.cpp b/test/shared_library_get_symbol_test.cpp index ff3791d..d060063 100644 --- a/test/shared_library_get_symbol_test.cpp +++ b/test/shared_library_get_symbol_test.cpp @@ -14,21 +14,7 @@ #include #include -#include -namespace fs = ::boost::filesystem; -fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) { - fs::directory_iterator it(root); - fs::directory_iterator endit; - - while (it != endit) { - if (fs::is_regular_file(*it) && it->path().filename().wstring().find(filename_part) != std::wstring::npos) { - return *it; - } - ++it; - } - - throw std::runtime_error("Failed to find library"); -} +#include "../example/shared_lib_path.hpp" // lib functions @@ -124,7 +110,7 @@ int test_main(int argc, char* argv[]) { using namespace boost::plugin; BOOST_CHECK(argc >= 2); - boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"test_library"); + boost::filesystem::path shared_library_path = shared_lib_path(argv[1], L"test_library"); std::cout << "Library: " << shared_library_path; diff --git a/test/shared_library_load_test.cpp b/test/shared_library_load_test.cpp index b9aaa08..9d38c22 100644 --- a/test/shared_library_load_test.cpp +++ b/test/shared_library_load_test.cpp @@ -7,21 +7,8 @@ #include #include -#include -namespace fs = ::boost::filesystem; -fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) { - fs::directory_iterator it(root); - fs::directory_iterator endit; - while (it != endit) { - if (fs::is_regular_file(*it) && it->path().filename().wstring().find(filename_part) != std::wstring::npos) { - return *it; - } - ++it; - } - - throw std::runtime_error("Failed to find library"); -} +#include "../example/shared_lib_path.hpp" // Unit Tests int test_main(int argc, char* argv[]) @@ -29,7 +16,7 @@ int test_main(int argc, char* argv[]) using namespace boost::plugin; BOOST_CHECK(argc >= 2); - boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"test_library"); + boost::filesystem::path shared_library_path = shared_lib_path(argv[1], L"test_library"); std::cout << "Library: " << shared_library_path; { diff --git a/test/shared_library_search_symbol_test.cpp b/test/shared_library_search_symbol_test.cpp index bdcfc74..f325fb4 100644 --- a/test/shared_library_search_symbol_test.cpp +++ b/test/shared_library_search_symbol_test.cpp @@ -9,21 +9,8 @@ #include #include -#include -namespace fs = ::boost::filesystem; -fs::path get_shared_lib(const fs::path& root, const std::wstring& filename_part) { - fs::directory_iterator it(root); - fs::directory_iterator endit; - while (it != endit) { - if (fs::is_regular_file(*it) && it->path().filename().wstring().find(filename_part) != std::wstring::npos) { - return *it; - } - ++it; - } - - throw std::runtime_error("Failed to find library"); -} +#include "../example/shared_lib_path.hpp" // Unit Tests int test_main(int argc, char* argv[]) @@ -31,7 +18,7 @@ int test_main(int argc, char* argv[]) using namespace boost::plugin; BOOST_CHECK(argc >= 2); - boost::filesystem::path shared_library_path = get_shared_lib(argv[1], L"test_library"); + boost::filesystem::path shared_library_path = shared_lib_path(argv[1], L"test_library"); std::cout << "Library: " << shared_library_path; {