Lunchbox  1.14.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::PluginFactory< T > Class Template Reference

Factory for Plugin classes. More...

#include <pluginFactory.h>

+ Collaboration diagram for lunchbox::PluginFactory< T >:

Public Types

typedef Plugin< T > PluginT
 
typedef std::vector< PluginTPlugins
 

Public Member Functions

T * create (const typename T::InitDataT &initData)
 Create a plugin instance. More...
 
void register_ (const PluginT &plugin)
 Register a plugin type. More...
 
bool deregister (const PluginT &plugin)
 Deregister a plugin type. More...
 
void deregisterAll ()
 Unregister all plugin types. More...
 
Automatic loading of plugin DSOs.
void load (const int version, const std::string &path, const std::string &pattern)
 Load all compatible plugin libraries from a directory matching a pattern. More...
 
void load (const int version, const Strings &paths, const std::string &pattern)
 

Static Public Member Functions

static PluginFactorygetInstance ()
 Get the single class instance. More...
 

Detailed Description

template<class T>
class lunchbox::PluginFactory< T >

Factory for Plugin classes.

The PluginFactory selects the a plugin for a given T::InitDataT, based on a plugin's handles() function. In case a InitDataT can be handled by multiple plugins, which plugin is chosen is undefined.

This class has been designed as a singleton to allow for link time plugin registration, but nothing prevents an application from registering new types at run time.

To do the registration of a plugin during the static initialization phase use the PluginRegisterer.

Example:

/* Copyright (c) 2013-2016, EPFL/Blue Brain Project
* Raphael Dumusc <raphael.dumusc@epfl.ch>
*
* This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define BOOST_TEST_MODULE PluginFactory
#include <lunchbox/types.h>
struct InitData;
namespace std { string to_string( const InitData& ); }
#include <lunchbox/pluginFactory.h>
#include <lunchbox/pluginRegisterer.h>
#include <servus/uri.h>
#include <boost/test/unit_test.hpp>
#define VALID_VALUE 10
#define INVALID_VALUE 0
struct InitData
{
InitData() : uri( "test://uri" ) {}
servus::URI uri;
};
namespace std
{
inline std::string to_string( const InitData& data )
{
return std::to_string( data.uri );
}
}
class PluginInterface
{
public:
typedef InitData InitDataT;
typedef PluginInterface InterfaceT;
virtual ~PluginInterface() {}
virtual int getValue() = 0;
};
class Plugin : public PluginInterface
{
public:
explicit Plugin( const InitData& ) {}
static bool handles( const InitData& ) { return true; }
int getValue() final { return VALID_VALUE; }
};
class FalsePlugin : public PluginInterface
{
public:
explicit FalsePlugin( const InitData& ) {}
static bool handles( const InitData& ) { return false; }
int getValue() final { return INVALID_VALUE; }
};
typedef std::shared_ptr< PluginInterface > PluginInterfacePtr;
PluginInterfacePtr createPlugin()
{
return PluginInterfacePtr(
}
BOOST_AUTO_TEST_CASE( throwNoneRegistered )
{
BOOST_CHECK_THROW( createPlugin(), std::runtime_error );
}
BOOST_AUTO_TEST_CASE( creation )
{
PluginInterfacePtr plugin = createPlugin();
BOOST_CHECK( plugin );
BOOST_CHECK_EQUAL( plugin->getValue(), VALID_VALUE );
}
BOOST_AUTO_TEST_CASE( throwHandlesFailure )
{
BOOST_CHECK_THROW( createPlugin(), std::runtime_error );
}
BOOST_AUTO_TEST_CASE( createCorrectVariant )
{
PluginInterfacePtr plugin = createPlugin();
BOOST_CHECK( plugin );
BOOST_CHECK_EQUAL( plugin->getValue(), VALID_VALUE );
}
Version
1.11.0

Definition at line 55 of file pluginFactory.h.

Member Function Documentation

template<typename T >
T * lunchbox::PluginFactory< T >::create ( const typename T::InitDataT &  initData)

Create a plugin instance.

Parameters
initDataThe initData passed to the plugin constructor.
Returns
A new PluginT instance. The user is responsible for deleting the returned object.
Exceptions
std::runtime_errorif no plugin can handle the initData.
Version
1.11.0

Definition at line 40 of file pluginFactory.ipp.

References LBTHROW.

template<typename T >
bool lunchbox::PluginFactory< T >::deregister ( const PluginT plugin)

Deregister a plugin type.

Version
1.11.0

Definition at line 57 of file pluginFactory.ipp.

template<typename T >
void lunchbox::PluginFactory< T >::deregisterAll ( )

Unregister all plugin types.

Version
1.11.0

Definition at line 68 of file pluginFactory.ipp.

References lunchbox::PluginFactory< T >::load(), and lunchbox::usort().

+ Here is the call graph for this function:

template<typename T >
PluginFactory< T > & lunchbox::PluginFactory< T >::getInstance ( )
static

Get the single class instance.

Version
1.11.0

Definition at line 24 of file pluginFactory.ipp.

Referenced by lunchbox::PluginRegisterer< T >::PluginRegisterer().

+ Here is the caller graph for this function:

template<typename T >
void lunchbox::PluginFactory< T >::load ( const int  version,
const std::string &  path,
const std::string &  pattern 
)

Load all compatible plugin libraries from a directory matching a pattern.

The pattern is the core name of the library, and is extended by the system-specific shared library suffix and postfix. The plugin has to implement the C functions 'int LunchboxPluginGetVersion()' and 'bool LunchboxPluginRegister()'. Only plugins with the same ABI version as the given one are registered.

Parameters
versionthe current ABI version of the application loading the plugins.
paththe directory to search for plugins.
patternthe core pattern of plugin names.
Version
1.11.0
See also
getLibraryPath()

Definition at line 88 of file pluginFactory.ipp.

References lunchbox::DSO::getFunctionPointer(), lunchbox::DSO::isOpen(), LBERROR, LBINFO, and lunchbox::searchDirectory().

Referenced by lunchbox::PluginFactory< T >::deregisterAll().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T >
void lunchbox::PluginFactory< T >::register_ ( const PluginT plugin)

Register a plugin type.

Version
1.11.0

Definition at line 51 of file pluginFactory.ipp.

Referenced by lunchbox::PluginRegisterer< T >::PluginRegisterer().

+ Here is the caller graph for this function:


The documentation for this class was generated from the following files: