Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
dynamicgraph::FactoryStorage Class Reference

Provides a way to create Entity objects from their class name. More...

#include <factory.h>

Inheritance diagram for dynamicgraph::FactoryStorage:
Inheritance graph
[legend]

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...
 
EntitynewEntity (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 FactoryStoragegetInstance ()
 Get pointer to unique object of the class. More...
 

Private Types

typedef std::map< std::string, EntityConstructor_ptrEntityMap
 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 FactoryStorageinstance_ = NULL
 to the unique object of the class More...
 

Detailed Description

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.

Member Typedef Documentation

◆ EntityConstructor_ptr

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.

◆ EntityMap

typedef std::map<std::string, EntityConstructor_ptr> dynamicgraph::FactoryStorage::EntityMap
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.

Constructor & Destructor Documentation

◆ ~FactoryStorage()

dynamicgraph::FactoryStorage::~FactoryStorage ( )

Definition at line 29 of file src/dgraph/factory.cpp.

◆ FactoryStorage()

dynamicgraph::FactoryStorage::FactoryStorage ( )
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.

Member Function Documentation

◆ deregisterEntity()

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.

Parameters
entnamethe entity name (as passed to registerEntity before)

Definition at line 61 of file src/dgraph/factory.cpp.

◆ destroy()

void dynamicgraph::FactoryStorage::destroy ( )
static

Destroy the unique instance of the class.

Definition at line 22 of file src/dgraph/factory.cpp.

◆ existEntity()

bool dynamicgraph::FactoryStorage::existEntity ( const std::string &  name) const

Check if an Entity associated with a particular name has already been registered.

Parameters
nameentity name
Returns
Do the entity exist?

Definition at line 95 of file src/dgraph/factory.cpp.

◆ getInstance()

FactoryStorage * dynamicgraph::FactoryStorage::getInstance ( )
static

Get pointer to unique object of the class.

Definition at line 15 of file src/dgraph/factory.cpp.

◆ listEntities()

void dynamicgraph::FactoryStorage::listEntities ( std::vector< std::string > &  list) const

List the available entities.

Available entities are appended to the method argument.

Parameters
listAvailable entities will be appended to list.

Definition at line 101 of file src/dgraph/factory.cpp.

◆ newEntity()

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.

Parameters
classnamethe name of the Entity type
objnamethe instance name
Returns
Dynamically allocated instance of classname.

Definition at line 79 of file src/dgraph/factory.cpp.

◆ registerEntity()

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.

Parameters
entnamethe name used to subscribe the entity.
entpointer to a function allocating an entity from an instance name.

Definition at line 34 of file src/dgraph/factory.cpp.

Member Data Documentation

◆ entityMap

EntityMap dynamicgraph::FactoryStorage::entityMap
private

The entity map storing information about how to instantiate an Entity.

Definition at line 169 of file include/dynamic-graph/factory.h.

◆ instance_

FactoryStorage * dynamicgraph::FactoryStorage::instance_ = NULL
staticprivate

to the unique object of the class

Definition at line 172 of file include/dynamic-graph/factory.h.


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


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03