OpenMD  2.5
Molecular Dynamics in the Open
OpenMD::GenericFactory< Object, IdentType, Creator > Class Template Reference

GenericFactory is a template based Object Factory Factory pattern is used to define an interface for creating an object. More...

#include "utils/GenericFactory.hpp"

Public Types

typedef GenericFactory< Object, IdentType, Creator > FactoryType
 
typedef std::map< IdentType, Creator > CreatorMapType
 

Public Member Functions

bool registerCreator (const IdentType &id, Creator creator)
 
bool unregisterCreator (const IdentType &id)
 
Object * createObject (const IdentType &id)
 
std::vector< IdentType > getIdents ()
 

Static Public Member Functions

static FactoryTypegetInstance ()
 

Public Attributes

CreatorMapType creatorMap_
 

Static Public Attributes

static FactoryTypeinstance_
 

Detailed Description

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
class OpenMD::GenericFactory< Object, IdentType, Creator >

GenericFactory is a template based Object Factory Factory pattern is used to define an interface for creating an object.

Parameters
Objectthe base class of the hierarchy for which you provide the object factory.
IdentTypethe object that identifies the type of the concrete object. Default type is std::string *
Creatorthe callable entity that creates objects. This type must support operator(), taking no parameters and returning a pointer to Object. Default type is function pointer.

Usage:

//Shape class
class Shape {
...
};
//instantiating a new object factory
typedef GenericFactory<Shape> ShapeFactory;
//Line class
class Line : public Shape{
...
};
//declare function to create Line
Shape* createLine() {
return new Line;
}
//register createLine
//note: must put ShapeFactory::getInstance()->registerCreator("Line", createLine) on the right
//hand side, otherwise the compiler will consider it as a function declaration
const bool registeredLine = ShapeFactory::getInstance()->registerCreator("Line", createLine);
//Circle class
class Circle : public Shape{
...
};
//declare function to create Circle
Shape* createCircle() {
return new Circle;
}
//register createCircle
const bool registeredCircle = ShapeFactory::getInstance()->registerCreator("Circle", createCircle);
//create object by ident
Line* line = ShapeFactory::getInstance()->createObject("Line");
Circle* circle = ShapeFactory::getInstance()->createObject("Circle");

Or the user can use predefined macro DECLARE_CREATOR and REGISTER_CREATOR

//Shape class
class Shape {
...
};
//instantiating a new object factory
typedef GenericFactory<Shape> ShapeFactory;
//Line class
class Line : public Shape{
...
};
//declare function using macro
DECLARE_CREATOR(Shape, Line)
//register using macro
REGISTER_CREATOR(ShapeFactory, "Line", Line);
//Circle class
class Circle : public Shape{
...
};
//declare function using macro
DECLARE_CREATOR(Shape, Circle)
//register using macro
REGISTER_CREATOR(ShapeFactory, "Circle", Circle);

Definition at line 144 of file GenericFactory.hpp.

Member Typedef Documentation

◆ CreatorMapType

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
typedef std::map<IdentType, Creator> OpenMD::GenericFactory< Object, IdentType, Creator >::CreatorMapType

Definition at line 147 of file GenericFactory.hpp.

◆ FactoryType

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
typedef GenericFactory<Object, IdentType, Creator> OpenMD::GenericFactory< Object, IdentType, Creator >::FactoryType

Definition at line 146 of file GenericFactory.hpp.

Member Function Documentation

◆ createObject()

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
Object* OpenMD::GenericFactory< Object, IdentType, Creator >::createObject ( const IdentType &  id)
inline

Looks up the type identifier in the internal map. If it is found, it invokes the corresponding creator for the type identifier and returns its result.

Returns
a pointer of the concrete object, return NULL if no creator is registed for creating this concrete object
Parameters
idthe identification of the concrete object

Definition at line 188 of file GenericFactory.hpp.

References OpenMD::GenericFactory< Object, IdentType, Creator >::creatorMap_.

◆ getIdents()

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
std::vector<IdentType> OpenMD::GenericFactory< Object, IdentType, Creator >::getIdents ( )
inline

Returns all of the registed type identifiers

Returns
all of the registed type identifiers

Definition at line 202 of file GenericFactory.hpp.

References OpenMD::GenericFactory< Object, IdentType, Creator >::creatorMap_.

◆ getInstance()

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
static FactoryType* OpenMD::GenericFactory< Object, IdentType, Creator >::getInstance ( )
inlinestatic

Returns an instance of object factory

Returns
an instance of object factory

Definition at line 153 of file GenericFactory.hpp.

References OpenMD::GenericFactory< Object, IdentType, Creator >::instance_.

◆ registerCreator()

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
bool OpenMD::GenericFactory< Object, IdentType, Creator >::registerCreator ( const IdentType &  id,
Creator  creator 
)
inline

Registers a creator with a type identifier

Returns
true if registration is succeed, otherwise return false
Parameters
idthe identification of the concrete object
creatorthe object responsible to create the concrete object

Definition at line 165 of file GenericFactory.hpp.

References OpenMD::GenericFactory< Object, IdentType, Creator >::creatorMap_.

◆ unregisterCreator()

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
bool OpenMD::GenericFactory< Object, IdentType, Creator >::unregisterCreator ( const IdentType &  id)
inline

Unregisters the creator for the given type identifier. If the type identifier was previously registered, the function returns true.

Returns
truethe type identifier was previously registered and the creator is removed, otherwise return false
Parameters
idthe identification of the concrete object

Definition at line 177 of file GenericFactory.hpp.

References OpenMD::GenericFactory< Object, IdentType, Creator >::creatorMap_.

Member Data Documentation

◆ creatorMap_

◆ instance_

template<class Object, typename IdentType = std::string, typename Creator = Object* (*)()>
GenericFactory< Object, IdentType, Creator > * OpenMD::GenericFactory< Object, IdentType, Creator >::instance_
static

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