LBIBCell
 All Classes Functions Variables Friends Pages
UtilLib::Factory< AbstractProduct, IdentifierType, FactoryErrorPolicy, ProductCreatorSignature > Class Template Reference

#include <Factory.hpp>

Inheritance diagram for UtilLib::Factory< AbstractProduct, IdentifierType, FactoryErrorPolicy, ProductCreatorSignature >:

Public Member Functions

bool registerType (const IdentifierType &id, ProductCreatorSignature creator)
 
bool unregisterType (const IdentifierType &id)
 
AbstractProduct * createObject (const IdentifierType &id) const
 

Detailed Description

template<class AbstractProduct, typename IdentifierType, template< typename, class > class FactoryErrorPolicy = ErrorPolicyThrowException, typename ProductCreatorSignature = AbstractProduct* (*)()>
class UtilLib::Factory< AbstractProduct, IdentifierType, FactoryErrorPolicy, ProductCreatorSignature >

Factory implementation, following [Alexandescu, 2002].

To generate a factory do a typedef like that:

This code generates a factory of CDESolvers as an Singleton.

To register your class in the Factory use this code in the header:

static CDESolver* create()
{
return new CDESolver;
}
static const bool registerLoader();
static const bool registered_;

And in the implementation do this:

const bool CDESolver::registerLoader()
{
return SolverFactory::instance().registerType(NAME, CDESolver::create);
}
const bool CDESolver::registered_ = CDESolver::registerLoader();

Where NAME is replaced by your string identifier for this class

Additional your class should protect the Constructor by making it either private or protected. Provide a static create method which returns a pointer to this class which is then registered in the factory.

To instantiate an object use this code:

SolverFactory::instance().createObject(cdeSolverName);

Definition at line 112 of file Factory.hpp.

Member Function Documentation

template<class AbstractProduct , typename IdentifierType , template< typename, class > class FactoryErrorPolicy = ErrorPolicyThrowException, typename ProductCreatorSignature = AbstractProduct* (*)()>
AbstractProduct* UtilLib::Factory< AbstractProduct, IdentifierType, FactoryErrorPolicy, ProductCreatorSignature >::createObject ( const IdentifierType &  id) const
inline

Create an instance of the specified type.

Parameters
idThe type identifier.
Returns
A pointer to an new instance of the requested type.
Note
The behavior upon requesting a non-existent type id defined by the FactoryErrorPolicy policy.

Definition at line 143 of file Factory.hpp.

template<class AbstractProduct , typename IdentifierType , template< typename, class > class FactoryErrorPolicy = ErrorPolicyThrowException, typename ProductCreatorSignature = AbstractProduct* (*)()>
bool UtilLib::Factory< AbstractProduct, IdentifierType, FactoryErrorPolicy, ProductCreatorSignature >::registerType ( const IdentifierType &  id,
ProductCreatorSignature  creator 
)
inline

Registers a type with the factory.

Parameters
idThe identifier of the type.
creatorFunction pointer to the type creation method.
Returns
A boolean, true on success.

Definition at line 122 of file Factory.hpp.

template<class AbstractProduct , typename IdentifierType , template< typename, class > class FactoryErrorPolicy = ErrorPolicyThrowException, typename ProductCreatorSignature = AbstractProduct* (*)()>
bool UtilLib::Factory< AbstractProduct, IdentifierType, FactoryErrorPolicy, ProductCreatorSignature >::unregisterType ( const IdentifierType &  id)
inline

Unregisters a type.

Parameters
idThe type identifier.
Returns
A boolean, true on success.

Definition at line 133 of file Factory.hpp.


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