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<sigtime_t> &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 bool Entity::hasSignal(const string &signame) const {
110  return (!(signalMap.find(signame) == signalMap.end()));
111 }
112 
113 SignalBase<sigtime_t> &Entity::getSignal(const string &signame) {
114  SignalMap::iterator sigkey = signalMap.find(signame);
115  if (sigkey == signalMap.end()) /* key does NOT exist */
116  {
118  "The requested signal is not registered", ": %s",
119  signame.c_str());
120  }
121  return *(sigkey->second);
122 }
123 
124 const SignalBase<sigtime_t> &Entity::getSignal(const string &signame) const {
125  SignalMap::const_iterator sigkey = signalMap.find(signame);
126  if (sigkey == signalMap.end()) /* key does NOT exist */
127  {
129  "The requested signal is not registered", ": %s",
130  signame.c_str());
131  }
132  return *(sigkey->second);
133 }
134 
135 std::ostream &Entity::displaySignalList(std::ostream &os) const {
136  os << "--- <" << getName() << "> signal list: " << endl;
137  const SignalMap::const_iterator iterend = signalMap.end();
138  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
139  ++iter) {
140  os << " ";
141  if ((++iter)-- == iterend)
142  os << "`";
143  else
144  os << "|";
145  os << "-- <" << *(iter->second) << endl;
146  }
147  return os;
148 }
149 
150 std::ostream &Entity::writeGraph(std::ostream &os) const {
151  const SignalMap::const_iterator iterend = signalMap.end();
152  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
153  ++iter) {
154  (*(iter->second)).writeGraph(os);
155  }
156  return os;
157 }
158 
159 std::ostream &Entity::writeCompletionList(std::ostream &os) const {
160  const SignalMap::const_iterator iterend = signalMap.end();
161  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
162  ++iter) {
163  os << getName() << "." << (*(iter->second)).shortName() << std::endl;
164  }
165 
166  os << getCommandList() << std::endl;
167  return os;
168 }
169 
170 void Entity::display(std::ostream &os) const {
171  os << this->getClassName() << ": " << name;
172 }
173 
174 std::ostream &dynamicgraph::operator<<(std::ostream &os, const Entity &ent) {
175  ent.display(os);
176  return os;
177 }
178 
180 
181 /* --- PARAMS --------------------------------------------------------------- */
182 /* --- PARAMS --------------------------------------------------------------- */
183 /* --- PARAMS --------------------------------------------------------------- */
184 
185 static std::string Entity_COMMAND_LIST = "print\nsignals\nsignalDep";
186 const std::string &Entity::getCommandList() const {
187  return Entity_COMMAND_LIST;
188 }
189 
191 void Entity::addCommand(const std::string &inName, Command *command) {
192  if (commandMap.count(inName) != 0) {
195  "Command " + inName + " already registered in Entity.");
196  }
197  std::pair<const std::string, Command *> item(inName, command);
198  commandMap.insert(item);
199 }
200 
202 std::map<const std::string, Command *> Entity::getNewStyleCommandMap() {
203  return commandMap;
204 }
205 
206 Command *Entity::getNewStyleCommand(const std::string &commandName) {
207  if (commandMap.count(commandName) != 1) {
210  "Command <" + commandName + "> is not registered in Entity.");
211  }
212  return commandMap[commandName];
213 }
214 
215 void Entity::sendMsg(const std::string &msg, MsgType t,
216  const std::string &lineId) {
217  logger_.stream(t, lineId) << "[" << name << "]" << msg << '\n';
218 }
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL
@ UNREFERED_SIGNAL
Definition: exception-factory.h:23
entity.h
dynamicgraph::operator<<
DYNAMIC_GRAPH_DLLAPI std::ostream & operator<<(std::ostream &os, const dynamicgraph::Entity &ent)
Definition: src/dgraph/entity.cpp:174
dynamicgraph::Entity::hasSignal
bool hasSignal(const std::string &signame) const
Test if a signal of name signame is present.
Definition: src/dgraph/entity.cpp:109
Entity_COMMAND_LIST
static std::string Entity_COMMAND_LIST
Definition: src/dgraph/entity.cpp:185
dynamicgraph::ExceptionFactory::SIGNAL_CONFLICT
@ SIGNAL_CONFLICT
Definition: exception-factory.h:26
dynamicgraph::Entity::~Entity
virtual ~Entity()
Definition: src/dgraph/entity.cpp:47
dynamicgraph
dynamicgraph::Entity::commandMap
CommandMap_t commandMap
Definition: include/dynamic-graph/entity.h:176
dynamicgraph::Entity::sendMsg
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.
Definition: src/dgraph/entity.cpp:215
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::UNREFERED_FUNCTION
@ UNREFERED_FUNCTION
Definition: exception-factory.h:24
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
dgDEBUG
#define dgDEBUG(level)
Definition: debug.h:157
dynamicgraph::Entity::name
std::string name
Definition: include/dynamic-graph/entity.h:174
dynamicgraph::Entity::getSignalMap
SignalMap getSignalMap() const
Provides a map of all the signals.
Definition: src/dgraph/entity.cpp:179
dynamicgraph::Entity::getNewStyleCommandMap
CommandMap_t getNewStyleCommandMap()
Provides the std::map where all the commands are registered.
Definition: src/dgraph/entity.cpp:202
dynamicgraph::Entity::displaySignalList
std::ostream & displaySignalList(std::ostream &os) const
Display the list of signals of this entity in output stream os.
Definition: src/dgraph/entity.cpp:135
dynamicgraph::Entity::getDocString
virtual std::string getDocString() const
Returns the Entity documentation.
Definition: src/dgraph/entity.cpp:104
dynamicgraph::Entity::SignalMap
std::map< std::string, SignalBase< sigtime_t > * > SignalMap
Definition: include/dynamic-graph/entity.h:54
dynamicgraph::Entity::getName
const std::string & getName() const
Definition: include/dynamic-graph/entity.h:60
command.h
dgERRORF
void dgERRORF(const int, const char *,...)
Definition: debug.h:173
sig
dynamicgraph::SignalArray_const< double > sig
Definition: signal-all.cpp:25
dynamicgraph::command::Command
Definition: command.h:35
dynamicgraph::Entity::getClassName
virtual const std::string & getClassName() const
Definition: include/dynamic-graph/entity.h:61
dgDEBUGOUT
#define dgDEBUGOUT(level)
Definition: debug.h:204
dynamicgraph::Entity::display
virtual void display(std::ostream &os) const
Display information on the entity inside the output stream os.
Definition: src/dgraph/entity.cpp:170
DG_THROW
#define DG_THROW
Definition: exception-abstract.h:24
dynamicgraph::Entity::getNewStyleCommand
command::Command * getNewStyleCommand(const std::string &cmdName)
Provides the pointer towards the Command object cmdName.
Definition: src/dgraph/entity.cpp:206
dynamicgraph::ExceptionFactory
Generic error class.
Definition: exception-factory.h:18
dynamicgraph::Entity::logger_
Logger logger_
Definition: include/dynamic-graph/entity.h:177
dynamicgraph::MsgType
MsgType
Definition: logger.h:29
dynamicgraph::Entity::getCommandList
const std::string & getCommandList() const
Definition: src/dgraph/entity.cpp:186
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::Entity::entityRegistration
void entityRegistration()
Definition: src/dgraph/entity.cpp:25
dynamicgraph::Logger::stream
RTLoggerStream stream()
Definition: logger.h:200
dynamicgraph::Entity::getSignal
SignalBase< sigtime_t > & getSignal(const std::string &signalName)
Provides a reference to the signal named signalName.
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
Add a command to Entity.
Definition: src/dgraph/entity.cpp:191
dynamicgraph::SignalArray
TODO.
Definition: fwd.hh:47
dynamicgraph::SignalBase< sigtime_t >
dynamicgraph::Entity::signalDeregistration
void signalDeregistration(const std::string &name)
Definition: src/dgraph/entity.cpp:89
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
Definition: src/dgraph/entity.cpp:59
dynamicgraph::Entity::signalMap
SignalMap signalMap
Definition: include/dynamic-graph/entity.h:175
debug.h
compile.name
name
Definition: compile.py:23
pool.h


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38