src/dgraph/pool.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * Fran├žois Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 /* --------------------------------------------------------------------- */
11 /* --- INCLUDE --------------------------------------------------------- */
12 /* --------------------------------------------------------------------- */
13 
14 /* --- DYNAMIC-GRAPH --- */
15 #include "dynamic-graph/pool.h"
16 
17 #include <list>
18 #include <sstream>
19 #include <string>
20 #include <typeinfo>
21 
22 #include "dynamic-graph/debug.h"
23 #include "dynamic-graph/entity.h"
24 
25 using namespace dynamicgraph;
26 
27 /* --------------------------------------------------------------------- */
28 /* --- CLASS ----------------------------------------------------------- */
29 /* --------------------------------------------------------------------- */
30 
32  if (instance_ == 0) {
33  instance_ = new PoolStorage;
34  }
35  return instance_;
36 }
37 
39  delete instance_;
40  instance_ = NULL;
41 }
42 
44  dgDEBUGIN(15);
45 
46  for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
47  // Here, this is normal that the next iteration is at the beginning
48  // of the map as deregisterEntity remove the element iter from the map.
49  iter = entityMap.begin()) {
50  dgDEBUG(15) << "Delete \"" << (iter->first) << "\"" << std::endl;
51  Entity *entity = iter->second;
52  deregisterEntity(iter);
53  delete (entity);
54  }
55  instance_ = 0;
56  dgDEBUGOUT(15);
57 }
58 
59 /* --------------------------------------------------------------------- */
60 void PoolStorage::registerEntity(const std::string &entname, Entity *ent) {
61  Entities::iterator entkey = entityMap.find(entname);
62  if (entkey != entityMap.end()) // key does exist
63  {
64  throw ExceptionFactory(
66  "Another entity already defined with the same name. ",
67  "Entity name is <%s>.", entname.c_str());
68  } else {
69  dgDEBUG(10) << "Register entity <" << entname << "> in the pool."
70  << std::endl;
71  entityMap[entname] = ent;
72  }
73 }
74 
75 void PoolStorage::deregisterEntity(const std::string &entname) {
76  Entities::iterator entkey = entityMap.find(entname);
77  if (entkey == entityMap.end()) // key doesnot exist
78  {
80  "Entity not defined yet. ", "Entity name is <%s>.",
81  entname.c_str());
82  } else {
83  dgDEBUG(10) << "Deregister entity <" << entname << "> from the pool."
84  << std::endl;
85  deregisterEntity(entkey);
86  }
87 }
88 
89 void PoolStorage::deregisterEntity(const Entities::iterator &entity) {
90  entityMap.erase(entity);
91 }
92 
93 Entity &PoolStorage::getEntity(const std::string &name) {
94  dgDEBUG(25) << "Get <" << name << ">" << std::endl;
95  Entities::iterator entPtr = entityMap.find(name);
96  if (entPtr == entityMap.end()) {
98  "Unknown entity.", " (while calling <%s>)",
99  name.c_str());
100  } else
101  return *entPtr->second;
102 }
103 
105  return entityMap;
106 }
107 
108 bool PoolStorage::existEntity(const std::string &name) {
109  return entityMap.find(name) != entityMap.end();
110 }
111 
112 bool PoolStorage::existEntity(const std::string &name, Entity *&ptr) {
113  Entities::iterator entPtr = entityMap.find(name);
114  if (entPtr == entityMap.end())
115  return false;
116  else {
117  ptr = entPtr->second;
118  return true;
119  }
120 }
121 
122 void PoolStorage::clearPlugin(const std::string &name) {
123  dgDEBUGIN(5);
124  std::list<Entity *> toDelete;
125 
126  for (Entities::iterator entPtr = entityMap.begin(); entPtr != entityMap.end();
127  ++entPtr)
128  if (entPtr->second->getClassName() == name)
129  toDelete.push_back(entPtr->second);
130 
131  for (std::list<Entity *>::iterator iter = toDelete.begin();
132  iter != toDelete.end(); ++iter)
133  delete (Entity *)*iter;
134  dgDEBUGOUT(5);
135 }
136 
137 /* --------------------------------------------------------------------- */
138 
139 #include <dynamic-graph/entity.h>
140 
141 #ifdef WIN32
142 #include <time.h>
143 #endif /*WIN32*/
144 
145 void PoolStorage::writeGraph(const std::string &aFileName) {
146  size_t IdxPointFound = aFileName.rfind(".");
147  std::string tmp1 = aFileName.substr(0, IdxPointFound);
148  size_t IdxSeparatorFound = aFileName.rfind("/");
149  std::string GenericName;
150  if (IdxSeparatorFound != std::string::npos)
151  GenericName = tmp1.substr(IdxSeparatorFound, tmp1.length());
152  else
153  GenericName = tmp1;
154 
155  /* Reading local time */
156  time_t ltime;
157  ltime = time(NULL);
158  struct tm ltimeformatted;
159 #ifdef WIN32
160  localtime_s(&ltimeformatted, &ltime);
161 #else
162  localtime_r(&ltime, &ltimeformatted);
163 #endif /*WIN32*/
164 
165  /* Opening the file and writing the first comment. */
166  std::ofstream GraphFile(aFileName.c_str(), std::ofstream::out);
167  GraphFile << "/* This graph has been automatically generated. " << std::endl;
168  GraphFile << " " << 1900 + ltimeformatted.tm_year
169  << " Month: " << 1 + ltimeformatted.tm_mon
170  << " Day: " << ltimeformatted.tm_mday
171  << " Time: " << ltimeformatted.tm_hour << ":"
172  << ltimeformatted.tm_min;
173  GraphFile << " */" << std::endl;
174  GraphFile << "digraph \"" << GenericName << "\" { ";
175  GraphFile << "\t graph [ label=\"" << GenericName
176  << "\" bgcolor = white rankdir=LR ]" << std::endl
177  << "\t node [ fontcolor = black, color = black,"
178  << "fillcolor = gold1, style=filled, shape=box ] ; " << std::endl;
179  GraphFile << "\tsubgraph cluster_Entities { " << std::endl;
180 
181  GraphFile << "\t} " << std::endl;
182 
183  for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
184  ++iter) {
185  Entity *ent = iter->second;
186  GraphFile << "\"" << ent->getName() << "\""
187  << " [ label = \"" << ent->getName() << "\" ," << std::endl
188  << " fontcolor = black, color = black, fillcolor=cyan,"
189  << " style=filled, shape=box ]" << std::endl;
190  ent->writeGraph(GraphFile);
191  }
192 
193  GraphFile << "}" << std::endl;
194 
195  GraphFile.close();
196 }
197 
198 void PoolStorage::writeCompletionList(std::ostream &os) {
199  for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
200  ++iter) {
201  Entity *ent = iter->second;
202  ent->writeCompletionList(os);
203  }
204 }
205 
206 static bool objectNameParser(std::istringstream &cmdparse, std::string &objName,
207  std::string &funName) {
208  const int SIZE = 128;
209  char buffer[SIZE];
210  cmdparse >> std::ws;
211  cmdparse.getline(buffer, SIZE, '.');
212  if (!cmdparse.good()) // The callback is not an object method
213  return false;
214 
215  objName = buffer;
216  // cmdparse.getline( buffer,SIZE );
217  // funName = buffer;
218  cmdparse >> funName;
219  return true;
220 }
221 
222 SignalBase<int> &PoolStorage::getSignal(std::istringstream &sigpath) {
223  std::string objname, signame;
224  if (!objectNameParser(sigpath, objname, signame)) {
226  "Parse error in signal name");
227  }
228 
229  Entity &ent = getEntity(objname);
230  return ent.getSignal(signame);
231 }
232 
void deregisterEntity(const std::string &entname)
Unregister an entity.
void writeGraph(const std::string &aFileName)
This method write a graph description on the file named FileName.
Singleton that keeps track of all the entities.
static bool objectNameParser(std::istringstream &cmdparse, std::string &objName, std::string &funName)
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
#define dgDEBUGOUT(level)
Definition: debug.h:204
virtual std::ostream & writeGraph(std::ostream &os) const
This method is used to write down in os the edges of the graph by calling the signals writeGraph meth...
void registerEntity(const std::string &entname, Entity *ent)
Register an entity.
void writeCompletionList(std::ostream &os)
virtual std::ostream & writeCompletionList(std::ostream &os) const
This method is used write in the output stream os the signals names and the commands of the entity...
bool existEntity(const std::string &name)
Test if the entity exists.
Entity & getEntity(const std::string &name)
Get an entity.
static PoolStorage * getInstance()
Get unique instance of the class.
#define dgDEBUG(level)
Definition: debug.h:157
static void destroy()
Destroy the unique instance of the class.
const std::string & getName() const
std::map< std::string, Entity * > Entities
Sorted set of entities with unique key (name).
const Entities & getEntityMap() const
Const access to entity map.
#define DG_THROW
~PoolStorage()
Default destructor.
SignalBase< int > & getSignal(const std::string &signalName)
Provides a reference to the signal named signalName.
SignalBase< int > & getSignal(std::istringstream &sigpath)
Get a signal by name.
void clearPlugin(const std::string &name)
Disallocate an entity.
#define dgDEBUGIN(level)
VP_DEBUG.
Definition: debug.h:202
Entities entityMap
Set of basic objects of the SoT.


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