Provides a way to create Entity objects from their class name. More...
#include <factory.h>
Public Types | |
typedef Entity *(* | EntityConstructor_ptr) (const std::string &) |
Function pointer providing an entity instance from its name. More... | |
Public Member Functions | |
void | deregisterEntity (const std::string &entname) |
Delete an entity from the factory. More... | |
bool | existEntity (const std::string &name) const |
Check if an Entity associated with a particular name has already been registered. More... | |
void | listEntities (std::vector< std::string > &list) const |
List the available entities. More... | |
Entity * | newEntity (const std::string &classname, const std::string &objname) const |
Instantiate (and allocate) an entity. More... | |
void | registerEntity (const std::string &entname, EntityConstructor_ptr ent) |
Add a new entity to the factory. More... | |
~FactoryStorage () | |
Static Public Member Functions | |
static void | destroy () |
Destroy the unique instance of the class. More... | |
static FactoryStorage * | getInstance () |
Get pointer to unique object of the class. More... | |
Private Types | |
typedef std::map< std::string, EntityConstructor_ptr > | EntityMap |
Entity map type. More... | |
Private Member Functions | |
FactoryStorage () | |
Constructor the factory. More... | |
Private Attributes | |
EntityMap | entityMap |
The entity map storing information about how to instantiate an Entity. More... | |
Static Private Attributes | |
static FactoryStorage * | instance_ = NULL |
to the unique object of the class More... | |
Provides a way to create Entity objects from their class name.
The dynamic graph frameworks relies on entities (see Entity) which defines atomic processing units. This class provides a robust way to enumerate and instantiate these entities. Each entity has a name (its type name) and can be instantiated. Each instance also has a name.
For instance one can define a C++ class called MyEntity which inherits from dynamicgraph::Entity. This type can be registered into the factory to teach the framework that:
To achieve this, one must pass an entity name and a function pointer.
The entity name will identify the class at run-time (be careful: this may not be equivalent to the C++ class name even if it is recommended to do so).
The function pointer must point on a function taking a string as input and returning an instance of the Entity (the concrete subclass, not directly the upper Entity class).
The instances returned by this function must be dynamically allocated and the caller must get the ownership of the instance (i.e. it will free it when required).
To finish, please note that the instance name indicates to the entity how the instance itself is called at run-time. This name does not need to be unique and no check is done on it. It is the caller responsibility to make sure that the instance name is appropriate and to check for uniqueness if required.
This class is a singleton. The rationale is that each unique name must identify a unique Entity. The use of a single instance of this class enforces this behavior, instantiating one yourself would break this property.
Definition at line 81 of file include/dynamic-graph/factory.h.
typedef Entity*(* dynamicgraph::FactoryStorage::EntityConstructor_ptr) (const std::string &) |
Function pointer providing an entity instance from its name.
Definition at line 85 of file include/dynamic-graph/factory.h.
|
private |
Entity map type.
This maps entity names to functions pointers which can be used to instantiate an Entity.
Definition at line 165 of file include/dynamic-graph/factory.h.
dynamicgraph::FactoryStorage::~FactoryStorage | ( | ) |
Definition at line 29 of file src/dgraph/factory.cpp.
|
explicitprivate |
Constructor the factory.
After the initialization, no entities will be available. registerEntity has to be used to add new entities to the object.
Definition at line 27 of file src/dgraph/factory.cpp.
void dynamicgraph::FactoryStorage::deregisterEntity | ( | const std::string & | entname | ) |
Delete an entity from the factory.
If the provided entity name does not exist in the factory, an ExceptionFactory exception will be raised with the code OBJECT_CONFLICT.
entname | the entity name (as passed to registerEntity before) |
Definition at line 61 of file src/dgraph/factory.cpp.
|
static |
Destroy the unique instance of the class.
Definition at line 22 of file src/dgraph/factory.cpp.
bool dynamicgraph::FactoryStorage::existEntity | ( | const std::string & | name | ) | const |
Check if an Entity associated with a particular name has already been registered.
name | entity name |
Definition at line 95 of file src/dgraph/factory.cpp.
|
static |
Get pointer to unique object of the class.
Definition at line 15 of file src/dgraph/factory.cpp.
void dynamicgraph::FactoryStorage::listEntities | ( | std::vector< std::string > & | list | ) | const |
List the available entities.
Available entities are appended to the method argument.
list | Available entities will be appended to list. |
Definition at line 101 of file src/dgraph/factory.cpp.
Entity * dynamicgraph::FactoryStorage::newEntity | ( | const std::string & | classname, |
const std::string & | objname | ||
) | const |
Instantiate (and allocate) an entity.
An instance called objname of the entity which type is classname will be allocated by this method.
It is the caller responsibility to free the returned object.
If the class name does not exist, an ExceptionFactory exception will be raised with the code UNREFERED_OBJECT.
The instance name (objname) is passed to the Entity constructor and it is the caller responsibility to avoid instance name conflicts if necessary.
classname | the name of the Entity type |
objname | the instance name |
Definition at line 79 of file src/dgraph/factory.cpp.
void dynamicgraph::FactoryStorage::registerEntity | ( | const std::string & | entname, |
FactoryStorage::EntityConstructor_ptr | ent | ||
) |
Add a new entity to the factory.
It is not allowed to have several entities using the same name. If this is the case, an ExceptionFactory exception will be raised with the code OBJECT_CONFLICT.
If the function pointer is null, an ExceptionFactory exception will be raised with the code OBJECT_CONFLICT.
entname | the name used to subscribe the entity. |
ent | pointer to a function allocating an entity from an instance name. |
Definition at line 34 of file src/dgraph/factory.cpp.
|
private |
The entity map storing information about how to instantiate an Entity.
Definition at line 169 of file include/dynamic-graph/factory.h.
|
staticprivate |
to the unique object of the class
Definition at line 172 of file include/dynamic-graph/factory.h.