CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT > Class Template Reference

#include <plugin.h>

Inheritance diagram for PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >:

Public Member Functions

virtual ~PolymorphicConstructionImpl ()
 

Static Public Member Functions

static AbstractProductT * Construct (const ParameterT &param)
 

Protected Types

typedef AbstractFactory
< AbstractProductT, ParameterT,
InfoT > 
Factory
 
typedef std::vector< Factory * > RegisteredPlugins
 

Protected Member Functions

virtual bool Initialize ()
 

Static Protected Member Functions

static void LazilyRegisterPlugins ()
 
template<class ConcreteProductT >
static void RegisterPlugin ()
 

Static Protected Attributes

static RegisteredPlugins registered_plugins_
 

Static Private Member Functions

static void UnregisterAllPlugins ()
 

Static Private Attributes

static atomic_int32 needs_init_
 
static pthread_mutex_t init_mutex_
 

Friends

class PolymorphicConstructionUnittestAdapter
 

Detailed Description

template<class AbstractProductT, typename ParameterT, typename InfoT>
class PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >

Template to simplify the polymorphic creation of a number of concrete classes that share the common base class AbstractProductT. Use this to create flexible class hierarchies.

The template assumes a number of things from the user classes:

  1. AbstractProductT must implement static void RegisterPlugins() which will register all available derived classes by calling RegisterPlugin<DerivedClass>() for each implemented sub-class.
  2. Each derived class of AbstractProductT must implement static bool WillHandle(const ParameterT &param) that figures out if the concrete class can cope with the given parameter
  3. Each derived class must have at least the following constructor: DerivedClass(const ParameterT &param) which is used to instantiate the concrete class in case it returned true in WillHandle()
  4. (OPTIONAL) Both AbstractProductT and ConcreteProductTs can override the virtual method bool Initialize() which will be called directly after creation of a ConcreteProductT. If it returns false, the constructed in- stance is deleted and the list of plugins is traversed further.
  5. (OPTIONAL) The ConcreteProductTs can implement a static InfoT GetInfo() that can be used for run-time introspection of registered plugins using PolymorphicConstruction<AbstractProductT, ParameterT, InfoT>::Introspect()

A possible class hierarchy could look like this:

PolymorphicConstruction<AbstractNumberCruncher, Parameter> | +–> AbstractNumberCruncher | +–> ConcreteMulticoreNumberCruncher | +–> ConcreteGpuNumberCruncher | +–> ConcreteClusterNumberCruncher

In this example AbstractNumberCruncher::RegisterPlugins() will register all three concrete number cruncher classes. Using the whole thing would look like so:

Parameter param = Parameter(typicalGpuProblem); AbstractNumberCruncher *polymorphicCruncher = AbstractNumberCruncher::Construct(param); polymorphicCruncher->Crunch();

polymorphicCruncher now points to an instance of ConcreteGpuNumberCruncher and can be used as any other polymorphic class with the interface defined in AbstractNumberCruncher.

Note: PolymorphicCreation goes through the list of registered plugins in the order they have been registered and instantiates the first class that claims responsibility for the given parameter.

Parameters
AbstractProductTthe common base class of all classes that should be polymorphically created. In most cases this will be the class that directly inherits from Polymorphic- Construction.
ParameterTthe type of the parameter that is used to poly- morphically instantiate one of the subclasses of AbstractProductT
InfoT(optional) wrapper type for introspection data of registered plugins. InfoT AbstractProductT::GetInfo() needs to be implemented for each plugin

Definition at line 180 of file plugin.h.

Member Typedef Documentation

template<class AbstractProductT, typename ParameterT, typename InfoT>
typedef AbstractFactory<AbstractProductT, ParameterT, InfoT> PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::Factory
protected

Definition at line 182 of file plugin.h.

template<class AbstractProductT, typename ParameterT, typename InfoT>
typedef std::vector<Factory*> PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::RegisteredPlugins
protected

Definition at line 183 of file plugin.h.

Constructor & Destructor Documentation

template<class AbstractProductT, typename ParameterT, typename InfoT>
virtual PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::~PolymorphicConstructionImpl ( )
inlinevirtual

Definition at line 186 of file plugin.h.

Member Function Documentation

template<class AbstractProductT, typename ParameterT, typename InfoT>
static AbstractProductT* PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::Construct ( const ParameterT &  param)
inlinestatic

Definition at line 188 of file plugin.h.

template<class AbstractProductT, typename ParameterT, typename InfoT>
virtual bool PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::Initialize ( )
inlineprotectedvirtual

Reimplemented in upload::AbstractUploader, and upload::GatewayUploader.

Definition at line 256 of file plugin.h.

template<class AbstractProductT, typename ParameterT, typename InfoT>
static void PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::LazilyRegisterPlugins ( )
inlinestaticprotected

Definition at line 212 of file plugin.h.

template<class AbstractProductT, typename ParameterT, typename InfoT>
template<class ConcreteProductT >
static void PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::RegisterPlugin ( )
inlinestaticprotected

Registers a plugin that is polymorphically constructable afterwards. Warning: Multiple registrations of the same ConcreteProductT might lead to undefined behaviour!

Parameters
ConcreteProductTthe concrete implementation of AbstractProductT that should be registered as constructable.

Note: You shall not need to use this method anywhere in your code except in AbstractProductT::RegisterPlugins().

Definition at line 248 of file plugin.h.

template<class AbstractProductT, typename ParameterT, typename InfoT>
static void PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::UnregisterAllPlugins ( )
inlinestaticprivate

This method clears the list of registered plugins. Note: A user of PolymorphicConstruction is not supposed to use this! The method is meant to be used solely for testing purposes! In particular a unit test registering a mocked plugin is supposed to clear up after each unit test! see: gtest: SetUp() / TearDown() and PolymorphicConstructionUnittestAdapter

DO NOT USE THIS OUTSIDE UNIT TESTS!! -> Global state is nasty!

Definition at line 270 of file plugin.h.

Friends And Related Function Documentation

template<class AbstractProductT, typename ParameterT, typename InfoT>
friend class PolymorphicConstructionUnittestAdapter
friend

Friend class for testability (see test/common/testutil.h)

Definition at line 234 of file plugin.h.

Member Data Documentation

template<class AbstractProductT, typename ParameterT, typename InfoT>
pthread_mutex_t PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::init_mutex_
staticprivate
Initial value:
=
PTHREAD_MUTEX_INITIALIZER

Definition at line 280 of file plugin.h.

template<class AbstractProductT, typename ParameterT, typename InfoT>
atomic_int32 PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::needs_init_
staticprivate

Definition at line 279 of file plugin.h.

template<class AbstractProductT, typename ParameterT, typename InfoT>
RegisteredPlugins PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >::registered_plugins_
staticprotected

Definition at line 276 of file plugin.h.


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