diff --git a/doc/tutorial.qbk b/doc/tutorial.qbk index 7f45066..8f85732 100644 --- a/doc/tutorial.qbk +++ b/doc/tutorial.qbk @@ -76,6 +76,26 @@ Plugin Name: aggregator [endsect] +[section Searching for a symbol in multiple plugins] + +Consider the situation: we have multiple plugins, but only some of them have symbols that we need. +Let's write a function that search list of plugins and attempts to find `"create_plugin"` method. + +[import ../example/tutorial3/tutorial3.cpp] +[callplugcpp_tutorial3] + +If we call that method for all our plugins we'll get the following output: + +[pre +Loading plugin: "/test/libmy_plugin_aggregator.so" +Matching plugin name: aggregator +Loading plugin: "/test/libmy_plugin_sum.so.1.56" +Constructing my_plugin_sum +Destructing my_plugin_sum ;o) +] + +[endsect] + [endsect] diff --git a/example/tutorial3/tutorial3.cpp b/example/tutorial3/tutorial3.cpp new file mode 100644 index 0000000..df559e6 --- /dev/null +++ b/example/tutorial3/tutorial3.cpp @@ -0,0 +1,61 @@ +// 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_tutorial3 + +#include +#include + +#include + +#include "../tutorial_common/plugin_api.hpp" + +namespace pl = boost::plugin; + +std::size_t search_for_symbols(const std::vector& plugins) { + std::size_t plugins_found = 0; + boost::shared_ptr lib = boost::make_shared(); + + for (std::size_t i = 0; i < plugins.size(); ++i) { + std::cout << "Loading plugin: " << plugins[i] << '\n'; + lib->load(plugins[i]); + if (!lib->search_symbol("create_plugin")) { + // no such symbol + continue; + } + + // library has symbol, importing... + typedef boost::shared_ptr (pluginapi_create_t)(); + boost::function creator + = pl::shared_function_alias(lib, "create_plugin"); + + std::cout << "Matching plugin name: " << creator()->name() << std::endl; + ++ plugins_found; + } + + return plugins_found; +} + +//] + +int main(int argc, char* argv[]) { + // argv[1] contains path to our plugin library + BOOST_ASSERT(argc >= 2); + std::vector plugins; + plugins.push_back(shared_lib_path(argv[1], L"my_plugin_aggregator")); + plugins.push_back(shared_lib_path(argv[1], L"my_plugin_sum")); + + const std::size_t res = search_for_symbols(plugins); + BOOST_ASSERT(res == 1); +} diff --git a/example/tutorial3/load_self.cpp b/example/tutorial4/load_self.cpp similarity index 100% rename from example/tutorial3/load_self.cpp rename to example/tutorial4/load_self.cpp diff --git a/example/tutorial3/static_plugin.cpp b/example/tutorial4/static_plugin.cpp similarity index 100% rename from example/tutorial3/static_plugin.cpp rename to example/tutorial4/static_plugin.cpp diff --git a/example/tutorial3/static_plugin.hpp b/example/tutorial4/static_plugin.hpp similarity index 100% rename from example/tutorial3/static_plugin.hpp rename to example/tutorial4/static_plugin.hpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 2f7b389..0b08fd9 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -14,7 +14,7 @@ import path ; path-constant TEST_DIR : $(BOOST_ROOT)/bin.v2/libs/plugin/test ; # Static library that is not linked with any of the boost libs -lib static_plugin : ../example/tutorial3/static_plugin.cpp : static : : linux:"-rdynamic" ; +lib static_plugin : ../example/tutorial4/static_plugin.cpp : static : : linux:"-rdynamic" ; project : source-location . @@ -47,6 +47,7 @@ project [ run ../example/getting_started.cpp : $(TEST_DIR) ] [ run ../example/tutorial1/tutorial1.cpp : $(TEST_DIR) ] [ run ../example/tutorial2/tutorial2.cpp : $(TEST_DIR) ] - [ run ../example/tutorial3/load_self.cpp : : : static_plugin linux:"-rdynamic" ] + [ run ../example/tutorial3/tutorial3.cpp : $(TEST_DIR) ] + [ run ../example/tutorial4/load_self.cpp : : : static_plugin linux:"-rdynamic" ] ; }