GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/dgraph/pool.cpp Lines: 102 114 89.5 %
Date: 2023-03-15 12:04:10 Branches: 92 182 50.5 %

Line Branch Exec Source
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
31
41
PoolStorage *PoolStorage::getInstance() {
32
41
  if (instance_ == 0) {
33
11
    instance_ = new PoolStorage;
34
  }
35
41
  return instance_;
36
}
37
38
3
void PoolStorage::destroy() {
39
3
  delete instance_;
40
3
  instance_ = NULL;
41
3
}
42
43
9
PoolStorage::~PoolStorage() {
44
  dgDEBUGIN(15);
45
46
5
  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
2
       iter = entityMap.begin()) {
50
    dgDEBUG(15) << "Delete \"" << (iter->first) << "\"" << std::endl;
51
2
    Entity *entity = iter->second;
52
2
    deregisterEntity(iter);
53
2
    delete (entity);
54
  }
55
3
  instance_ = 0;
56
  dgDEBUGOUT(15);
57
3
}
58
59
/* --------------------------------------------------------------------- */
60
17
void PoolStorage::registerEntity(const std::string &entname, Entity *ent) {
61
17
  Entities::iterator entkey = entityMap.find(entname);
62
17
  if (entkey != entityMap.end())  // key does exist
63
  {
64
    throw ExceptionFactory(
65
        ExceptionFactory::OBJECT_CONFLICT,
66
        "Another entity already defined with the same name. ",
67

1
        "Entity name is <%s>.", entname.c_str());
68
  } else {
69
    dgDEBUG(10) << "Register entity <" << entname << "> in the pool."
70
                << std::endl;
71
16
    entityMap[entname] = ent;
72
  }
73
16
}
74
75
2
void PoolStorage::deregisterEntity(const std::string &entname) {
76
2
  Entities::iterator entkey = entityMap.find(entname);
77
2
  if (entkey == entityMap.end())  // key doesnot exist
78
  {
79
    throw ExceptionFactory(ExceptionFactory::OBJECT_CONFLICT,
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
2
    deregisterEntity(entkey);
86
  }
87
2
}
88
89
4
void PoolStorage::deregisterEntity(const Entities::iterator &entity) {
90
4
  entityMap.erase(entity);
91
4
}
92
93
15
Entity &PoolStorage::getEntity(const std::string &name) {
94
  dgDEBUG(25) << "Get <" << name << ">" << std::endl;
95
15
  Entities::iterator entPtr = entityMap.find(name);
96
15
  if (entPtr == entityMap.end()) {
97
    DG_THROW ExceptionFactory(ExceptionFactory::UNREFERED_OBJECT,
98
                              "Unknown entity.", " (while calling <%s>)",
99

1
                              name.c_str());
100
  } else
101
14
    return *entPtr->second;
102
}
103
104
1
const PoolStorage::Entities &PoolStorage::getEntityMap() const {
105
1
  return entityMap;
106
}
107
108
bool PoolStorage::existEntity(const std::string &name) {
109
  return entityMap.find(name) != entityMap.end();
110
}
111
112
2
bool PoolStorage::existEntity(const std::string &name, Entity *&ptr) {
113
2
  Entities::iterator entPtr = entityMap.find(name);
114
2
  if (entPtr == entityMap.end())
115
1
    return false;
116
  else {
117
1
    ptr = entPtr->second;
118
1
    return true;
119
  }
120
}
121
122
1
void PoolStorage::clearPlugin(const std::string &name) {
123
  dgDEBUGIN(5);
124
2
  std::list<Entity *> toDelete;
125
126
1
  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
1
  for (std::list<Entity *>::iterator iter = toDelete.begin();
132
1
       iter != toDelete.end(); ++iter)
133
    delete (Entity *)*iter;
134
  dgDEBUGOUT(5);
135
1
}
136
137
/* --------------------------------------------------------------------- */
138
139
#include <dynamic-graph/entity.h>
140
141
#ifdef WIN32
142
#include <time.h>
143
#endif /*WIN32*/
144
145
1
void PoolStorage::writeGraph(const std::string &aFileName) {
146
1
  size_t IdxPointFound = aFileName.rfind(".");
147
2
  std::string tmp1 = aFileName.substr(0, IdxPointFound);
148
1
  size_t IdxSeparatorFound = aFileName.rfind("/");
149
2
  std::string GenericName;
150
1
  if (IdxSeparatorFound != std::string::npos)
151
    GenericName = tmp1.substr(IdxSeparatorFound, tmp1.length());
152
  else
153
1
    GenericName = tmp1;
154
155
  /* Reading local time */
156
  time_t ltime;
157
1
  ltime = time(NULL);
158
  struct tm ltimeformatted;
159
#ifdef WIN32
160
  localtime_s(&ltimeformatted, &ltime);
161
#else
162
1
  localtime_r(&ltime, &ltimeformatted);
163
#endif /*WIN32*/
164
165
  /* Opening the file and writing the first comment. */
166
2
  std::ofstream GraphFile(aFileName.c_str(), std::ofstream::out);
167

1
  GraphFile << "/* This graph has been automatically generated. " << std::endl;
168
1
  GraphFile << "   " << 1900 + ltimeformatted.tm_year
169

1
            << " Month: " << 1 + ltimeformatted.tm_mon
170

1
            << " Day: " << ltimeformatted.tm_mday
171


1
            << " Time: " << ltimeformatted.tm_hour << ":"
172
1
            << ltimeformatted.tm_min;
173

1
  GraphFile << " */" << std::endl;
174

1
  GraphFile << "digraph \"" << GenericName << "\" { ";
175
  GraphFile << "\t graph [ label=\"" << GenericName
176


1
            << "\" bgcolor = white rankdir=LR ]" << std::endl
177
            << "\t node [ fontcolor = black, color = black,"
178

1
            << "fillcolor = gold1, style=filled, shape=box ] ; " << std::endl;
179

1
  GraphFile << "\tsubgraph cluster_Entities { " << std::endl;
180
181

1
  GraphFile << "\t} " << std::endl;
182
183
2
  for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
184
1
       ++iter) {
185
1
    Entity *ent = iter->second;
186
1
    GraphFile << "\"" << ent->getName() << "\""
187



1
              << " [ label = \"" << ent->getName() << "\" ," << std::endl
188
              << "   fontcolor = black, color = black, fillcolor=cyan,"
189

1
              << " style=filled, shape=box ]" << std::endl;
190
1
    ent->writeGraph(GraphFile);
191
  }
192
193

1
  GraphFile << "}" << std::endl;
194
195
1
  GraphFile.close();
196
1
}
197
198
1
void PoolStorage::writeCompletionList(std::ostream &os) {
199
2
  for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
200
1
       ++iter) {
201
1
    Entity *ent = iter->second;
202
1
    ent->writeCompletionList(os);
203
  }
204
1
}
205
206
6
static bool objectNameParser(std::istringstream &cmdparse, std::string &objName,
207
                             std::string &funName) {
208
6
  const int SIZE = 128;
209
  char buffer[SIZE];
210
6
  cmdparse >> std::ws;
211
6
  cmdparse.getline(buffer, SIZE, '.');
212

6
  if (!cmdparse.good())  // The callback is not an object method
213
1
    return false;
214
215
5
  objName = buffer;
216
  // cmdparse.getline( buffer,SIZE );
217
  // funName = buffer;
218
5
  cmdparse >> funName;
219
5
  return true;
220
}
221
222
6
SignalBase<int> &PoolStorage::getSignal(std::istringstream &sigpath) {
223
13
  std::string objname, signame;
224

6
  if (!objectNameParser(sigpath, objname, signame)) {
225
    DG_THROW ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL,
226

1
                              "Parse error in signal name");
227
  }
228
229
5
  Entity &ent = getEntity(objname);
230
10
  return ent.getSignal(signame);
231
}
232
233
PoolStorage *PoolStorage::instance_ = 0;