src/dgraph/factory.cpp
Go to the documentation of this file.
1 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
2 // JRL, CNRS/AIST.
3 //
4 
6 
7 #include <boost/foreach.hpp>
8 
9 #include "dynamic-graph/debug.h"
10 
11 using namespace std;
12 using namespace dynamicgraph;
13 
14 namespace dynamicgraph {
15 FactoryStorage *FactoryStorage::getInstance() {
16  if (instance_ == 0) {
17  instance_ = new FactoryStorage;
18  }
19  return instance_;
20 }
21 
22 void FactoryStorage::destroy() {
23  delete instance_;
24  instance_ = NULL;
25 }
26 
27 FactoryStorage::FactoryStorage() : entityMap() {}
28 
30  instance_ = 0;
31  dgDEBUGINOUT(25);
32 }
33 
34 void FactoryStorage::registerEntity(const std::string &entname,
36  dgDEBUGIN(25);
37  if (existEntity(entname)) {
40  "Another entity class already defined with the same name. ",
41  "(while adding entity class <%s> inside the factory).",
42  entname.c_str());
43  dgERRORF(
44  "Another entity class already defined with the same name. "
45  "(while adding entity class <%s> inside the factory).",
46  entname.c_str());
47  } else {
48  if (!ent) {
49  // FIXME: we should have a better error code for that.
51  "Bad entity constructor.");
52  }
53 
54  dgDEBUG(30) << "Register entity <" << entname << "> in the factory."
55  << std::endl;
56  entityMap[entname] = ent;
57  }
58  dgDEBUGOUT(25);
59 }
60 
61 void FactoryStorage::deregisterEntity(const std::string &entname) {
62  dgDEBUGIN(25);
63  if (!existEntity(entname)) {
65  ExceptionFactory::OBJECT_CONFLICT, "Entity class not defined yet. ",
66  "(while removing entity class <%s>).", entname.c_str());
68  "Entity class not defined yet. "
69  "(while removing entity class <%s>).",
70  entname.c_str());
71  } else {
72  dgDEBUG(30) << "Deregister entity <" << entname << "> from the factory."
73  << std::endl;
74  entityMap.erase(entname);
75  }
76  dgDEBUGOUT(25);
77 }
78 
79 Entity *FactoryStorage::newEntity(const std::string &classname,
80  const std::string &objname) const {
81  dgDEBUG(15) << "New <" << classname << ">Entity <" << objname << ">"
82  << std::endl;
83 
84  EntityMap::const_iterator entPtr = entityMap.find(classname);
85  if (entPtr == entityMap.end()) {
87  ExceptionFactory::UNREFERED_OBJECT, "Unknown entity.",
88  " (while calling new_entity <%s>)", classname.c_str());
89  }
90  return entPtr->second(objname);
91 }
92 
93 // This checks efficiently if a key exists in an STL map using the
94 // approach suggested by Scott Meyer's Effective STL (item 24).
95 bool FactoryStorage::existEntity(const std::string &name) const {
96  EntityMap::const_iterator lb = entityMap.lower_bound(name);
97  return lb != entityMap.end() && !(entityMap.key_comp()(name, lb->first));
98 }
99 
100 // FIXME: this should be removed at some point.
101 void FactoryStorage::listEntities(std::vector<std::string> &outList) const {
102  typedef std::pair<std::string, EntityConstructor_ptr> iter_t;
103  BOOST_FOREACH (const iter_t &entity, entityMap)
104  outList.push_back(entity.first);
105 }
106 
107 EntityRegisterer::EntityRegisterer(const std::string &entityClassName,
109  : entityName(entityClassName) {
110  dgDEBUGIN(15);
111  FactoryStorage::getInstance()->registerEntity(entityClassName, maker);
112  dgDEBUGOUT(15);
113 }
114 
116  dgDEBUGIN(15);
118  dgDEBUGOUT(15);
119 }
120 
121 // The global factory.
123 } // end of namespace dynamicgraph.
dynamicgraph::EntityRegisterer::EntityRegisterer
EntityRegisterer(const std::string &entityClassName, FactoryStorage::EntityConstructor_ptr maker)
Register entity to the global factory.
Definition: src/dgraph/factory.cpp:107
dynamicgraph::FactoryStorage::EntityConstructor_ptr
Entity *(* EntityConstructor_ptr)(const std::string &)
Function pointer providing an entity instance from its name.
Definition: include/dynamic-graph/factory.h:85
dynamicgraph
dynamicgraph::EntityRegisterer::~EntityRegisterer
~EntityRegisterer()
Unregister entity to the global factory.
Definition: src/dgraph/factory.cpp:115
dynamicgraph::FactoryStorage::entityMap
EntityMap entityMap
The entity map storing information about how to instantiate an Entity.
Definition: include/dynamic-graph/factory.h:169
dynamicgraph::Entity
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
Definition: include/dynamic-graph/entity.h:52
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT
@ OBJECT_CONFLICT
Definition: exception-factory.h:28
dgDEBUG
#define dgDEBUG(level)
Definition: debug.h:157
dynamicgraph::FactoryStorage
Provides a way to create Entity objects from their class name.
Definition: include/dynamic-graph/factory.h:81
dgERRORF
void dgERRORF(const int, const char *,...)
Definition: debug.h:173
dynamicgraph::FactoryStorage::instance_
static FactoryStorage * instance_
\pointer to the unique object of the class
Definition: include/dynamic-graph/factory.h:172
dynamicgraph::FactoryStorage::registerEntity
void registerEntity(const std::string &entname, EntityConstructor_ptr ent)
Add a new entity to the factory.
Definition: src/dgraph/factory.cpp:34
dynamicgraph::FactoryStorage::~FactoryStorage
~FactoryStorage()
Definition: src/dgraph/factory.cpp:29
dgDEBUGOUT
#define dgDEBUGOUT(level)
Definition: debug.h:204
dynamicgraph::FactoryStorage::listEntities
void listEntities(std::vector< std::string > &list) const
List the available entities.
Definition: src/dgraph/factory.cpp:101
dgDEBUGIN
#define dgDEBUGIN(level)
VP_DEBUG.
Definition: debug.h:202
DG_THROW
#define DG_THROW
Definition: exception-abstract.h:24
dynamicgraph::ExceptionFactory
Generic error class.
Definition: exception-factory.h:18
dynamicgraph::FactoryStorage::deregisterEntity
void deregisterEntity(const std::string &entname)
Delete an entity from the factory.
Definition: src/dgraph/factory.cpp:61
dynamicgraph::ExceptionFactory::UNREFERED_OBJECT
@ UNREFERED_OBJECT
Definition: exception-factory.h:22
factory.h
dynamicgraph::FactoryStorage::existEntity
bool existEntity(const std::string &name) const
Check if an Entity associated with a particular name has already been registered.
Definition: src/dgraph/factory.cpp:95
dynamicgraph::EntityRegisterer::entityName
const std::string entityName
Name of the entity registered when the instance has been initialized.
Definition: include/dynamic-graph/factory.h:196
dgDEBUGINOUT
#define dgDEBUGINOUT(level)
Definition: debug.h:206
debug.h
dynamicgraph::FactoryStorage::newEntity
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
Definition: src/dgraph/factory.cpp:79
compile.name
name
Definition: compile.py:23
dynamicgraph::FactoryStorage::getInstance
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
Definition: src/dgraph/factory.cpp:15


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Thu Jun 13 2024 02:26:22