src/dgraph/entity.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 
11 #include <dynamic-graph/command.h>
12 #include <dynamic-graph/debug.h>
13 #include <dynamic-graph/entity.h>
14 #include <dynamic-graph/pool.h>
15 
17 #include <stdlib.h>
18 
19 #include <sstream>
20 
21 using namespace std;
22 using namespace dynamicgraph;
24 
25 void Entity::entityRegistration() {
26  PoolStorage::getInstance()->registerEntity(name, this);
27 }
28 
29 void Entity::entityDeregistration() {
30  PoolStorage::getInstance()->deregisterEntity(name);
31 }
32 
33 Entity::Entity(const string &name__) : name(name__) {
34  dgDEBUG(15) << "New entity <" << name__ << ">" << endl;
35  if (name.length() == 0) {
36  stringstream oss;
37  oss << rand();
38  // name = this->getClassName();
39  // Cannot call a virtual function from the constructor
40  name += "::";
41  name += oss.str();
42  }
43 
45 }
46 
48  dgDEBUG(25) << "# In (" << name << " { " << endl;
49  for (std::map<const std::string, Command *>::iterator it = commandMap.begin();
50  it != commandMap.end(); ++it) {
51  delete it->second;
52  }
53  dgDEBUGOUT(25);
54 }
55 
56 /* -------------------------------------------------------------------------- */
57 /* --- SIGNALS -------------------------------------------------------------- */
58 /* -------------------------------------------------------------------------- */
60  for (unsigned int i = 0; i < signals.getSize(); ++i) {
61  SignalBase<int> &sig = signals[i];
62  // const string& signame = sig.getName ();
63  istringstream iss(sig.getName());
64  const int SIZE = 4096;
65  char buffer[SIZE];
66  while (iss.good()) {
67  iss.getline(buffer, SIZE, ':');
68  }
69  const string &signame(buffer);
70 
71  SignalMap::iterator sigkey = signalMap.find(signame);
72  if (sigkey != signalMap.end()) // key does exist
73  {
74  dgERRORF("Key %s already exist in the signalMap.", signame.c_str());
75  if (sigkey->second != &sig) {
76  throw ExceptionFactory(
78  "Another signal already defined with the same name. ",
79  "Signame is <%s>.", signame.c_str());
80  }
81  } else {
82  dgDEBUG(10) << "Register signal <" << signame << "> for entity <"
83  << getName() << "> ." << endl;
84  signalMap[signame] = &sig;
85  }
86  }
87 }
88 
89 void Entity::signalDeregistration(const std::string &signame) {
90  SignalMap::iterator sigkey = signalMap.find(signame);
91  if (sigkey == signalMap.end()) // key does not exist
92  {
93  dgERRORF("Key %s does not exist in the signalMap.", signame.c_str());
95  "No signal defined with the given name. ",
96  " (while erasing <%s>).", signame.c_str());
97  } else {
98  dgDEBUG(10) << "Deregister signal <" << signame << "> for entity <"
99  << getName() << "> ." << endl;
100  signalMap.erase(signame);
101  }
102 }
103 
104 std::string Entity::getDocString() const {
105  std::string docString("No header documentation.");
106  return docString;
107 }
108 
109 #define __DG_ENTITY_GET_SIGNAL__(ITER_TYPE) \
110  SignalMap::ITER_TYPE sigkey = signalMap.find(signame); \
111  if (sigkey == signalMap.end()) /* key does NOT exist */ \
112  { \
113  throw ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL, \
114  "The requested signal is not registered", ": %s", \
115  signame.c_str()); \
116  } \
117  return *(sigkey->second);
118 
119 bool Entity::hasSignal(const string &signame) const {
120  return (!(signalMap.find(signame) == signalMap.end()));
121 }
122 
123 SignalBase<int> &Entity::getSignal(const string &signame) {
124  __DG_ENTITY_GET_SIGNAL__(iterator);
125 }
126 
127 const SignalBase<int> &Entity::getSignal(const string &signame) const {
128  __DG_ENTITY_GET_SIGNAL__(const_iterator);
129 }
130 
131 std::ostream &Entity::displaySignalList(std::ostream &os) const {
132  os << "--- <" << getName() << "> signal list: " << endl;
133  const SignalMap::const_iterator iterend = signalMap.end();
134  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
135  ++iter) {
136  os << " ";
137  if ((++iter)-- == iterend)
138  os << "`";
139  else
140  os << "|";
141  os << "-- <" << *(iter->second) << endl;
142  }
143  return os;
144 }
145 
146 std::ostream &Entity::writeGraph(std::ostream &os) const {
147  const SignalMap::const_iterator iterend = signalMap.end();
148  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
149  ++iter) {
150  (*(iter->second)).writeGraph(os);
151  }
152  return os;
153 }
154 
155 std::ostream &Entity::writeCompletionList(std::ostream &os) const {
156  const SignalMap::const_iterator iterend = signalMap.end();
157  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
158  ++iter) {
159  os << getName() << "." << (*(iter->second)).shortName() << std::endl;
160  }
161 
162  os << getCommandList() << std::endl;
163  return os;
164 }
165 
166 void Entity::display(std::ostream &os) const {
167  os << this->getClassName() << ": " << name;
168 }
169 
170 std::ostream &dynamicgraph::operator<<(std::ostream &os, const Entity &ent) {
171  ent.display(os);
172  return os;
173 }
174 
176 
177 /* --- PARAMS --------------------------------------------------------------- */
178 /* --- PARAMS --------------------------------------------------------------- */
179 /* --- PARAMS --------------------------------------------------------------- */
180 
181 static std::string Entity_COMMAND_LIST = "print\nsignals\nsignalDep";
182 const std::string &Entity::getCommandList() const {
183  return Entity_COMMAND_LIST;
184 }
185 
187 void Entity::addCommand(const std::string &inName, Command *command) {
188  if (commandMap.count(inName) != 0) {
191  "Command " + inName + " already registered in Entity.");
192  }
193  std::pair<const std::string, Command *> item(inName, command);
194  commandMap.insert(item);
195 }
196 
198 std::map<const std::string, Command *> Entity::getNewStyleCommandMap() {
199  return commandMap;
200 }
201 
202 Command *Entity::getNewStyleCommand(const std::string &commandName) {
203  if (commandMap.count(commandName) != 1) {
206  "Command <" + commandName + "> is not registered in Entity.");
207  }
208  return commandMap[commandName];
209 }
210 
211 void Entity::sendMsg(const std::string &msg, MsgType t,
212  const std::string &lineId) {
213  logger_.stream(t, lineId) << "[" << name << "]" << msg << '\n';
214 }
const std::string & getCommandList() const
std::ostream & displaySignalList(std::ostream &os) const
Display the list of signals of this entity in output stream os.
virtual std::string getDocString() const
Returns the Entity documentation.
#define __DG_ENTITY_GET_SIGNAL__(ITER_TYPE)
bool hasSignal(const std::string &signame) const
Test if a signal of name signame is present.
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
#define dgDEBUGOUT(level)
Definition: debug.h:204
RTLoggerStream stream()
Definition: logger.h:200
void sendMsg(const std::string &msg, MsgType t=MSG_TYPE_INFO, const std::string &lineId="")
Send messages msg with level t. Add string file and line to message.
void signalRegistration(const SignalArray< int > &signals)
SignalMap getSignalMap() const
Provides a map of all the signals.
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...
command::Command * getNewStyleCommand(const std::string &cmdName)
Provides the pointer towards the Command object cmdName.
std::map< std::string, SignalBase< int > * > SignalMap
dynamicgraph::SignalArray_const< double > sig
Definition: signal-all.cpp:25
virtual const std::string & getClassName() const
const std::string & getName() const
Definition: signal-base.h:42
void dgERRORF(const int, const char *,...)
Definition: debug.h:173
DYNAMIC_GRAPH_DLLAPI std::ostream & operator<<(std::ostream &os, const dynamicgraph::Entity &ent)
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...
virtual unsigned int getSize() const
Definition: signal-array.h:72
virtual void display(std::ostream &os) const
Display information on the entity inside the output stream os.
#define dgDEBUG(level)
Definition: debug.h:157
CommandMap_t getNewStyleCommandMap()
Provides the std::map where all the commands are registered.
static std::string Entity_COMMAND_LIST
const std::string & getName() const
#define DG_THROW
SignalBase< int > & getSignal(const std::string &signalName)
Provides a reference to the signal named signalName.
void signalDeregistration(const std::string &name)
void addCommand(const std::string &name, command::Command *command)
Add a command to Entity.


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