Hi Stefan,
I did try with the factory approach but I was only able to
gain access to one of the plugin.
I have build a single shared library called MultiplePlugins
that contains two plugins SourcePlugin and SinkPlugin
but I have not found a way to iterate the individual
factory method
8<------8<------8<------8<------8<------8<------8<------8<------
================
AbstractPlugin.h
================
#pragma once
#include <boost/format.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
class AbstractPlugin {
public:
AbstractPlugin() {};
virtual ~AbstractPlugin() {};
virtual void compute() = 0;
protected:
std::string _name;
};
typedef boost::shared_ptr<AbstractPlugin> PluginSharedPtr;
============
SinkPlugin.h
============
#include "AbstractPlugin.h"
class SinkPlugin : public AbstractPlugin
{
public:
SinkPlugin();
virtual ~SinkPlugin();
virtual void compute();
// Factory method
static PluginSharedPtr create();
};
==============
SourcePlugin.h
==============
#include "AbstractPlugin.h"
class SourcePlugin : public AbstractPlugin
{
public:
SourcePlugin();
virtual ~SourcePlugin();
virtual void compute();
// Factory method
static PluginSharedPtr create();
};
==============
SinkPlugin.cpp
==============
#include "SinkPlugin.h"
#include "boost/dll/alias.hpp"
SinkPlugin::SinkPlugin()
{
_name = "SinkPlugin";
}
SinkPlugin::~SinkPlugin()
{
}
void SinkPlugin::compute()
{
std::cout << boost::format("Plugin name is '%1%'") % _name << std::endl;
}
PluginSharedPtr SinkPlugin::create()
{
return boost::shared_ptr<SinkPlugin>(new SinkPlugin());
}
BOOST_DLL_ALIAS(SinkPlugin::create, create_plugin)
================
SourcePlugin.cpp
================
#include "SourcePlugin.h"
#include "boost/dll/alias.hpp"
SourcePlugin::SourcePlugin()
{
_name = "SourcePlugin";
}
SourcePlugin::~SourcePlugin()
{
}
void SourcePlugin::compute()
{
std::cout << boost::format("Plugin name is '%1%'") % _name << std::endl;
}
// Factory method
PluginSharedPtr SourcePlugin::create()
{
return boost::shared_ptr<SourcePlugin>(new SourcePlugin());
}
BOOST_DLL_ALIAS(SourcePlugin::create, create_plugin)
========
main.cpp
========
#include "SinkPlugin.h"
#include "SourcePlugin.h"
#include <boost/filesystem.hpp>
#include <boost/function.hpp>
#include <boost/dll/import.hpp>
int main()
{
std::cout << "Boost DLL testing ..." << std::endl;
/* Load the plugin from current working path
*/
boost::filesystem::path pluginPath = boost::filesystem::current_path() / boost::filesystem::path("MultiplePlugins");
std::cout << "Load Plugin from " << pluginPath << std::endl;
typedef PluginSharedPtr(PluginCreate)();
boost::function <PluginCreate> pluginCreator;
try
{
pluginCreator = boost::dll::import_alias<PluginCreate>(pluginPath,
"create_plugin", boost::dll::load_mode::append_decorations);
}
catch (const boost::system::system_error &err)
{
std::cerr << "Cannot load Plugin from " << pluginPath << std::endl;
std::cerr << err.what() << std::endl;
return -1;
}
/* create the plugin */
auto plugin = pluginCreator();
plugin->compute();
return 0;
}
==============
CMakeLists.txt
==============
add_library ( MultiplePlugins SHARED
SinkPlugin.cpp
SourcePlugin.cpp
)
add_executable ( load_plugins
main.cpp
)
target_link_libraries ( load_plugins
${Boost_LIBRARIES}
${CMAKE_DL_LIBS}
)