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.
void registerEntity(const std::string &entname, EntityConstructor_ptr ent)
Add a new entity to the factory.
static FactoryStorage * instance_
to the unique object of the class
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
#define dgDEBUGOUT(level)
Definition: debug.h:204
bool existEntity(const std::string &name) const
Check if an Entity associated with a particular name has already been registered. ...
void deregisterEntity(const std::string &entname)
Delete an entity from the factory.
#define dgDEBUGINOUT(level)
Definition: debug.h:206
void listEntities(std::vector< std::string > &list) const
List the available entities.
void dgERRORF(const int, const char *,...)
Definition: debug.h:173
const std::string entityName
Name of the entity registered when the instance has been initialized.
#define dgDEBUG(level)
Definition: debug.h:157
Entity *(* EntityConstructor_ptr)(const std::string &)
Function pointer providing an entity instance from its name.
EntityMap entityMap
The entity map storing information about how to instantiate an Entity.
~EntityRegisterer()
Unregister entity to the global factory.
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
#define DG_THROW
Provides a way to create Entity objects from their class name.
#define dgDEBUGIN(level)
VP_DEBUG.
Definition: debug.h:202
EntityRegisterer(const std::string &entityClassName, FactoryStorage::EntityConstructor_ptr maker)
Register entity to the global factory.


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