GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/dgraph/entity.cpp Lines: 97 101 96.0 %
Date: 2023-03-15 12:04:10 Branches: 94 160 58.8 %

Line Branch Exec Source
1
/*
2
 * Copyright 2010,
3
 * François Bleibel,
4
 * Olivier Stasse,
5
 *
6
 * CNRS/AIST
7
 *
8
 */
9
10
/*! Local framework includes */
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
16
/*! System includes */
17
#include <stdlib.h>
18
19
#include <sstream>
20
21
using namespace std;
22
using namespace dynamicgraph;
23
using dynamicgraph::command::Command;
24
25
17
void Entity::entityRegistration() {
26
17
  PoolStorage::getInstance()->registerEntity(name, this);
27
16
}
28
29
1
void Entity::entityDeregistration() {
30
1
  PoolStorage::getInstance()->deregisterEntity(name);
31
1
}
32
33
21
Entity::Entity(const string &name__) : name(name__) {
34
  dgDEBUG(15) << "New entity <" << name__ << ">" << endl;
35
17
  if (name.length() == 0) {
36
2
    stringstream oss;
37
2
    oss << rand();
38
    // name = this->getClassName();
39
    // Cannot call a virtual function from the constructor
40
2
    name += "::";
41

2
    name += oss.str();
42
  }
43
44
17
  entityRegistration();
45
16
}
46
47
16
Entity::~Entity() {
48
  dgDEBUG(25) << "# In (" << name << " { " << endl;
49
32
  for (std::map<const std::string, Command *>::iterator it = commandMap.begin();
50
48
       it != commandMap.end(); ++it) {
51
16
    delete it->second;
52
  }
53
  dgDEBUGOUT(25);
54
}
55
56
/* -------------------------------------------------------------------------- */
57
/* --- SIGNALS -------------------------------------------------------------- */
58
/* -------------------------------------------------------------------------- */
59
7
void Entity::signalRegistration(const SignalArray<int> &signals) {
60

20
  for (unsigned int i = 0; i < signals.getSize(); ++i) {
61
14
    SignalBase<int> &sig = signals[i];
62
    // const string& signame = sig.getName ();
63
28
    istringstream iss(sig.getName());
64
14
    const int SIZE = 128;
65
    char buffer[SIZE];
66

80
    while (iss.good()) {
67
66
      iss.getline(buffer, SIZE, ':');
68
    }
69
28
    const string &signame(buffer);
70
71
14
    SignalMap::iterator sigkey = signalMap.find(signame);
72
14
    if (sigkey != signalMap.end())  // key does exist
73
    {
74
1
      dgERRORF("Key %s already exist in the signalMap.", signame.c_str());
75
1
      if (sigkey->second != &sig) {
76
        throw ExceptionFactory(
77
            ExceptionFactory::SIGNAL_CONFLICT,
78
            "Another signal already defined with the same name. ",
79

1
            "Signame is <%s>.", signame.c_str());
80
      }
81
    } else {
82
      dgDEBUG(10) << "Register signal <" << signame << "> for entity <"
83
                  << getName() << "> ." << endl;
84
13
      signalMap[signame] = &sig;
85
    }
86
  }
87
6
}
88
89
3
void Entity::signalDeregistration(const std::string &signame) {
90
3
  SignalMap::iterator sigkey = signalMap.find(signame);
91
3
  if (sigkey == signalMap.end())  // key does not exist
92
  {
93
1
    dgERRORF("Key %s does not exist in the signalMap.", signame.c_str());
94
    throw ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL,
95
                           "No signal defined with the given name. ",
96

1
                           " (while erasing <%s>).", signame.c_str());
97
  } else {
98
    dgDEBUG(10) << "Deregister signal <" << signame << "> for entity <"
99
                << getName() << "> ." << endl;
100
2
    signalMap.erase(signame);
101
  }
102
2
}
103
104
1
std::string Entity::getDocString() const {
105
1
  std::string docString("No header documentation.");
106
1
  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
1
bool Entity::hasSignal(const string &signame) const {
120
1
  return (!(signalMap.find(signame) == signalMap.end()));
121
}
122
123
12
SignalBase<int> &Entity::getSignal(const string &signame) {
124


12
  __DG_ENTITY_GET_SIGNAL__(iterator);
125
}
126
127
1
const SignalBase<int> &Entity::getSignal(const string &signame) const {
128


1
  __DG_ENTITY_GET_SIGNAL__(const_iterator);
129
}
130
131
2
std::ostream &Entity::displaySignalList(std::ostream &os) const {
132


2
  os << "--- <" << getName() << "> signal list: " << endl;
133
2
  const SignalMap::const_iterator iterend = signalMap.end();
134
4
  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
135
2
       ++iter) {
136
2
    os << "    ";
137
2
    if ((++iter)-- == iterend)
138
1
      os << "`";
139
    else
140
1
      os << "|";
141

2
    os << "-- <" << *(iter->second) << endl;
142
  }
143
2
  return os;
144
}
145
146
2
std::ostream &Entity::writeGraph(std::ostream &os) const {
147
2
  const SignalMap::const_iterator iterend = signalMap.end();
148
4
  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
149
2
       ++iter) {
150
2
    (*(iter->second)).writeGraph(os);
151
  }
152
2
  return os;
153
}
154
155
2
std::ostream &Entity::writeCompletionList(std::ostream &os) const {
156
2
  const SignalMap::const_iterator iterend = signalMap.end();
157
4
  for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
158
2
       ++iter) {
159


2
    os << getName() << "." << (*(iter->second)).shortName() << std::endl;
160
  }
161
162

2
  os << getCommandList() << std::endl;
163
2
  return os;
164
}
165
166
2
void Entity::display(std::ostream &os) const {
167
2
  os << this->getClassName() << ": " << name;
168
2
}
169
170
1
std::ostream &dynamicgraph::operator<<(std::ostream &os, const Entity &ent) {
171
1
  ent.display(os);
172
1
  return os;
173
}
174
175
1
Entity::SignalMap Entity::getSignalMap() const { return signalMap; }
176
177
/* --- PARAMS --------------------------------------------------------------- */
178
/* --- PARAMS --------------------------------------------------------------- */
179
/* --- PARAMS --------------------------------------------------------------- */
180
181
static std::string Entity_COMMAND_LIST = "print\nsignals\nsignalDep";
182
3
const std::string &Entity::getCommandList() const {
183
3
  return Entity_COMMAND_LIST;
184
}
185
186
/// Add a command to Entity
187
30
void Entity::addCommand(const std::string &inName, Command *command) {
188

30
  if (commandMap.count(inName) != 0) {
189
    DG_THROW ExceptionFactory(
190
        ExceptionFactory::OBJECT_CONFLICT,
191

1
        "Command " + inName + " already registered in Entity.");
192
  }
193
58
  std::pair<const std::string, Command *> item(inName, command);
194
29
  commandMap.insert(item);
195
29
}
196
197
/// Return the list of command objects
198
1
std::map<const std::string, Command *> Entity::getNewStyleCommandMap() {
199
1
  return commandMap;
200
}
201
202
4
Command *Entity::getNewStyleCommand(const std::string &commandName) {
203
4
  if (commandMap.count(commandName) != 1) {
204
    DG_THROW ExceptionFactory(
205
        ExceptionFactory::UNREFERED_FUNCTION,
206

1
        "Command <" + commandName + "> is not registered in Entity.");
207
  }
208
3
  return commandMap[commandName];
209
}
210
211
8000
void Entity::sendMsg(const std::string &msg, MsgType t,
212
                     const std::string &lineId) {
213



8000
  logger_.stream(t, lineId) << "[" << name << "]" << msg << '\n';
214
8000
}