OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
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

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

Public Member Functions

bool registerCreator (const IdentType &id, Creator creator)
 Registers a creator with a type identifier.
 
bool unregisterCreator (const IdentType &id)
 Unregisters the creator for the given type identifier.
 
Object * createObject (const IdentType &id)
 Looks up the type identifier in the internal map.
 
std::vector< IdentType > getIdents ()
 Returns all of the registed type identifiers.
 

Static Public Member Functions

static FactoryTypegetInstance ()
 Returns an instance of object factory.
 

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
using ShapeFactory = GenericFactory<Shape>;
//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");
GenericFactory is a template based Object Factory Factory pattern is used to define an interface for ...

Or the user can use predefined macro DECLARE_CREATOR and REGISTER_CREATOR

//Shape class
class Shape {
...
};
//instantiating a new object factory
using ShapeFactory = GenericFactory<Shape>;
//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 157 of file GenericFactory.hpp.

Member Typedef Documentation

◆ CreatorMapType

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

Definition at line 160 of file GenericFactory.hpp.

◆ FactoryType

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

Definition at line 159 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 200 of file GenericFactory.hpp.

◆ 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 214 of file GenericFactory.hpp.

Referenced by OpenMD::operator<<().

◆ 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 166 of file GenericFactory.hpp.

◆ 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 177 of file GenericFactory.hpp.

◆ 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 188 of file GenericFactory.hpp.

Member Data Documentation

◆ creatorMap_

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

Definition at line 227 of file GenericFactory.hpp.

◆ instance_

template<class Object , typename IdentType , typename Creator >
GenericFactory< Object, IdentType, Creator > * OpenMD::GenericFactory< Object, IdentType, Creator >::instance_
static

Definition at line 226 of file GenericFactory.hpp.


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