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 void PoolStorage::writeGraph(const std::string &aFileName) {
142  size_t IdxPointFound = aFileName.rfind(".");
143  std::string tmp1 = aFileName.substr(0, IdxPointFound);
144  size_t IdxSeparatorFound = aFileName.rfind("/");
145  std::string GenericName;
146  if (IdxSeparatorFound != std::string::npos)
147  GenericName = tmp1.substr(IdxSeparatorFound, tmp1.length());
148  else
149  GenericName = tmp1;
150 
151  /* Opening the file and writing the first comment. */
152  std::ofstream GraphFile(aFileName.c_str(), std::ofstream::out);
153  GraphFile << "/* This graph has been automatically generated. " << std::endl;
154  GraphFile << " */" << std::endl;
155  GraphFile << "digraph \"" << GenericName << "\" { ";
156  GraphFile << "\t graph [ label=\"" << GenericName
157  << "\" bgcolor = white rankdir=LR ]" << std::endl
158  << "\t node [ fontcolor = black, color = black,"
159  << "fillcolor = gold1, style=filled, shape=box ] ; " << std::endl;
160  GraphFile << "\tsubgraph cluster_Entities { " << std::endl;
161 
162  GraphFile << "\t} " << std::endl;
163 
164  for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
165  ++iter) {
166  Entity *ent = iter->second;
167  GraphFile << "\"" << ent->getName() << "\""
168  << " [ label = \"" << ent->getName() << "\" ," << std::endl
169  << " fontcolor = black, color = black, fillcolor=cyan,"
170  << " style=filled, shape=box ]" << std::endl;
171  ent->writeGraph(GraphFile);
172  }
173 
174  GraphFile << "}" << std::endl;
175 
176  GraphFile.close();
177 }
178 
179 void PoolStorage::writeCompletionList(std::ostream &os) {
180  for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
181  ++iter) {
182  Entity *ent = iter->second;
183  ent->writeCompletionList(os);
184  }
185 }
186 
187 static bool objectNameParser(std::istringstream &cmdparse, std::string &objName,
188  std::string &funName) {
189  const int SIZE = 128;
190  char buffer[SIZE];
191  cmdparse >> std::ws;
192  cmdparse.getline(buffer, SIZE, '.');
193  if (!cmdparse.good()) // The callback is not an object method
194  return false;
195 
196  objName = buffer;
197  // cmdparse.getline( buffer,SIZE );
198  // funName = buffer;
199  cmdparse >> funName;
200  return true;
201 }
202 
203 SignalBase<sigtime_t> &PoolStorage::getSignal(std::istringstream &sigpath) {
204  std::string objname, signame;
205  if (!objectNameParser(sigpath, objname, signame)) {
207  "Parse error in signal name");
208  }
209 
210  Entity &ent = getEntity(objname);
211  return ent.getSignal(signame);
212 }
213 
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL
@ UNREFERED_SIGNAL
Definition: exception-factory.h:23
entity.h
dynamicgraph::PoolStorage::getSignal
SignalBase< sigtime_t > & getSignal(std::istringstream &sigpath)
Get a signal by name.
Definition: src/dgraph/pool.cpp:203
dynamicgraph::PoolStorage::registerEntity
void registerEntity(const std::string &entname, Entity *ent)
Register an entity.
Definition: src/dgraph/pool.cpp:60
objectNameParser
static bool objectNameParser(std::istringstream &cmdparse, std::string &objName, std::string &funName)
Definition: src/dgraph/pool.cpp:187
dynamicgraph
dynamicgraph::PoolStorage::deregisterEntity
void deregisterEntity(const std::string &entname)
Unregister an entity.
Definition: src/dgraph/pool.cpp:75
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
dynamicgraph::Entity::writeGraph
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...
Definition: src/dgraph/entity.cpp:150
dynamicgraph::PoolStorage
Singleton that keeps track of all the entities.
Definition: include/dynamic-graph/pool.h:33
dgDEBUG
#define dgDEBUG(level)
Definition: debug.h:157
dynamicgraph::PoolStorage::instance_
static PoolStorage * instance_
Definition: include/dynamic-graph/pool.h:111
dynamicgraph::PoolStorage::writeGraph
void writeGraph(const std::string &aFileName)
This method write a graph description on the file named FileName.
Definition: src/dgraph/pool.cpp:141
dynamicgraph::PoolStorage::getEntity
Entity & getEntity(const std::string &name)
Get an entity.
Definition: src/dgraph/pool.cpp:93
dynamicgraph::PoolStorage::PoolStorage
PoolStorage()
Definition: include/dynamic-graph/pool.h:110
dynamicgraph::Entity::getName
const std::string & getName() const
Definition: include/dynamic-graph/entity.h:60
dynamicgraph::PoolStorage::clearPlugin
void clearPlugin(const std::string &name)
Disallocate an entity.
Definition: src/dgraph/pool.cpp:122
dgDEBUGOUT
#define dgDEBUGOUT(level)
Definition: debug.h:204
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::PoolStorage::getEntityMap
const Entities & getEntityMap() const
Const access to entity map.
Definition: src/dgraph/pool.cpp:104
dynamicgraph::ExceptionFactory::UNREFERED_OBJECT
@ UNREFERED_OBJECT
Definition: exception-factory.h:22
dynamicgraph::PoolStorage::existEntity
bool existEntity(const std::string &name)
Test if the entity exists.
Definition: src/dgraph/pool.cpp:108
dynamicgraph::Entity::writeCompletionList
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.
Definition: src/dgraph/entity.cpp:159
dynamicgraph::PoolStorage::writeCompletionList
void writeCompletionList(std::ostream &os)
Definition: src/dgraph/pool.cpp:179
dynamicgraph::PoolStorage::Entities
std::map< std::string, Entity * > Entities
Sorted set of entities with unique key (name).
Definition: include/dynamic-graph/pool.h:39
dynamicgraph::PoolStorage::getInstance
static PoolStorage * getInstance()
Get unique instance of the class.
Definition: src/dgraph/pool.cpp:31
dynamicgraph::Entity::getSignal
SignalBase< sigtime_t > & getSignal(const std::string &signalName)
Provides a reference to the signal named signalName.
dynamicgraph::PoolStorage::destroy
static void destroy()
Destroy the unique instance of the class.
Definition: src/dgraph/pool.cpp:38
dynamicgraph::PoolStorage::~PoolStorage
~PoolStorage()
Default destructor.
Definition: src/dgraph/pool.cpp:43
dynamicgraph::SignalBase< sigtime_t >
dynamicgraph::PoolStorage::entityMap
Entities entityMap
Set of basic objects of the SoT.
Definition: include/dynamic-graph/pool.h:107
debug.h
compile.name
name
Definition: compile.py:23
pool.h


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Oct 22 2023 02:27:08