pool.cpp 8.26 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
2
3
4
/*
 * Copyright 2010,
 * François Bleibel,
 * Olivier Stasse,
5
 *
Thomas Moulard's avatar
Thomas Moulard committed
6
 * CNRS/AIST
7
 *
Thomas Moulard's avatar
Thomas Moulard committed
8
 * This file is part of dynamic-graph.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * dynamic-graph is free software:
 * Redistribution and use in source and binary forms,
 * with or without modification, are permitted provided that the following
 * conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES,  * INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
Thomas Moulard's avatar
Thomas Moulard committed
31
 */
32
33
34
35
36

/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */

37
/* --- DYNAMIC-GRAPH --- */
38
39
#include <list>
#include <typeinfo>
40
41
42
43
44
45
#include <iostream>
#include <sstream>
#include <string>
#include "dynamic-graph/pool.h"
#include "dynamic-graph/debug.h"
#include "dynamic-graph/entity.h"
46
47
48
49
50
51
52

using namespace dynamicgraph;

/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
PoolStorage* PoolStorage::
getInstance()
{
  if (instance_ == 0) {
    instance_ = new PoolStorage;
  }
  return instance_;
}

void PoolStorage::
destroy()
{
  delete instance_;
  instance_ = NULL;
}

69
PoolStorage::
Thomas Moulard's avatar
Thomas Moulard committed
70
~PoolStorage  ()
71
72
73
{
  dgDEBUGIN(15);

74
  for( Entities::iterator iter=entityMap.begin (); iter!=entityMap.end ();
75
76
77
       // Here, this is normal that the next iteration is at the beginning
       // of the map as deregisterEntity remove the element iter from the map.
       iter=entityMap.begin())
78
79
80
    {
      dgDEBUG(15) << "Delete \""
		   << (iter->first) <<"\""<<std::endl;
81
82
83
      Entity* entity = iter->second;
      deregisterEntity(iter);
      delete (entity);
84
    }
85
  instance_ = 0;
86
87
88
89
90
91
92
93
94
  dgDEBUGOUT(15);
}



/* --------------------------------------------------------------------- */
void PoolStorage::
registerEntity( const std::string& entname,Entity* ent )
{
95
96
  Entities::iterator entkey = entityMap.find(entname);
  if( entkey != entityMap.end () ) // key does exist
97
98
99
    {
      throw ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
				 "Another entity already defined with the same name. ",
Thomas Moulard's avatar
Thomas Moulard committed
100
				 "Entity name is <%s>.",entname.c_str () );
101
102
103
104
105
    }
  else
    {
      dgDEBUG(10) << "Register entity <"<< entname
		   << "> in the pool." <<std::endl;
106
      entityMap[entname] = ent;
107
108
109
110
111
112
    }
}

void PoolStorage::
deregisterEntity( const std::string& entname )
{
113
114
  Entities::iterator entkey = entityMap.find(entname);
  if( entkey == entityMap.end () ) // key doesnot exist
115
116
117
    {
      throw ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
				 "Entity not defined yet. ",
Thomas Moulard's avatar
Thomas Moulard committed
118
				 "Entity name is <%s>.",entname.c_str () );
119
120
121
122
123
    }
  else
    {
      dgDEBUG(10) << "Deregister entity <"<< entname
		   << "> from the pool." <<std::endl;
124
      deregisterEntity(entkey);
125
126
127
    }
}

128
129
130
131
132
133
void PoolStorage::
deregisterEntity( const Entities::iterator& entity )
{
  entityMap.erase( entity );
}

134
135
136
137
Entity& PoolStorage::
getEntity( const std::string& name )
{
  dgDEBUG(25) << "Get <" << name << ">"<<std::endl;
138
139
  Entities::iterator entPtr = entityMap.find( name );
  if( entPtr == entityMap.end () )
140
141
142
    {
      DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_OBJECT,
				     "Unknown entity."," (while calling <%s>)",
Thomas Moulard's avatar
Thomas Moulard committed
143
				     name.c_str () );
144
145
146
147
    }
  else return *entPtr->second;
}

148
149
150
151
152
153
const PoolStorage::Entities& PoolStorage::
getEntityMap () const
{
  return entityMap;
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
bool PoolStorage::
existEntity (const std::string& name)
{
  return entityMap.find( name ) != entityMap.end();
}
bool PoolStorage::
existEntity (const std::string& name, Entity*& ptr)
{
  Entities::iterator entPtr = entityMap.find( name );
  if( entPtr == entityMap.end () ) return false;
  else
    {
      ptr = entPtr->second;
      return true;
    }
}


172
173
174
175
void PoolStorage::
clearPlugin( const std::string& name )
{
  dgDEBUGIN(5);
Thomas Moulard's avatar
Thomas Moulard committed
176
  std::list<Entity*> toDelete;
177

178
179
  for (Entities::iterator entPtr = entityMap.begin  ();
       entPtr != entityMap.end  (); ++entPtr)
Thomas Moulard's avatar
Thomas Moulard committed
180
    if (entPtr->second->getClassName () == name)
Thomas Moulard's avatar
Thomas Moulard committed
181
      toDelete.push_back (entPtr->second);
182

Thomas Moulard's avatar
Thomas Moulard committed
183
184
  for (std::list< Entity* >::iterator iter = toDelete.begin  ();
       iter != toDelete.end  (); ++iter)
Thomas Moulard's avatar
Thomas Moulard committed
185
    delete (Entity*) *iter;
186
187
188
189
190
191
192
  dgDEBUGOUT(5);
}



/* --------------------------------------------------------------------- */

193
#include <dynamic-graph/entity.h>
194
195
196
197
198
199
200
201
202
203
204
205
206

#ifdef WIN32
#include <time.h>
#endif /*WIN32*/

void PoolStorage::
writeGraph(const std::string &aFileName)
{
  size_t IdxPointFound = aFileName.rfind(".");
  std::string tmp1 = aFileName.substr(0,IdxPointFound);
  size_t IdxSeparatorFound = aFileName.rfind("/");
  std::string GenericName;
  if (IdxSeparatorFound!=std::string::npos)
Thomas Moulard's avatar
Thomas Moulard committed
207
    GenericName = tmp1.substr(IdxSeparatorFound,tmp1.length ());
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  else
    GenericName = tmp1;

  /* Reading local time */
  time_t ltime;
  ltime = time(NULL);
  struct tm ltimeformatted;
#ifdef WIN32
  localtime_s(&ltimeformatted,&ltime);
#else
  localtime_r(&ltime,&ltimeformatted);
#endif /*WIN32*/

  /* Opening the file and writing the first comment. */
Thomas Moulard's avatar
Thomas Moulard committed
222
  std::ofstream GraphFile (aFileName.c_str (),std::ofstream::out);
223
224
225
226
227
228
229
  GraphFile << "/* This graph has been automatically generated. " << std::endl;
  GraphFile << "   " << 1900+ltimeformatted.tm_year
	    << " Month: " << 1+ltimeformatted.tm_mon
	    << " Day: " << ltimeformatted.tm_mday
	    << " Time: " << ltimeformatted.tm_hour
	    << ":" << ltimeformatted.tm_min;
  GraphFile << " */" << std::endl;
230
  GraphFile << "digraph \"" << GenericName << "\" { ";
231
232
233
234
235
236
  GraphFile << "\t graph [ label=\"" << GenericName << "\" bgcolor = white rankdir=LR ]" << std::endl
	    << "\t node [ fontcolor = black, color = black, fillcolor = gold1, style=filled, shape=box ] ; " << std::endl;
  GraphFile << "\tsubgraph cluster_Entities { " << std::endl;

  GraphFile << "\t} " << std::endl;

237
238
  for( Entities::iterator iter=entityMap.begin ();
       iter!=entityMap.end (); ++iter)
239
240
    {
      Entity* ent = iter->second;
241
      GraphFile << "\"" << ent->getName () << "\""
Thomas Moulard's avatar
Thomas Moulard committed
242
		<<" [ label = \"" << ent->getName () << "\" ," << std::endl
243
244
245
246
247
248
249
		<<"   fontcolor = black, color = black, fillcolor=cyan, style=filled, shape=box ]" << std::endl;
      ent->writeGraph(GraphFile);
    }


  GraphFile << "}"<< std::endl;

Thomas Moulard's avatar
Thomas Moulard committed
250
  GraphFile.close ();
251
252
253
254
255
}

void PoolStorage::
writeCompletionList(std::ostream& os)
{
256
257
  for( Entities::iterator iter=entityMap.begin ();
       iter!=entityMap.end (); ++iter)
258
259
260
261
262
263
264
265
    {
      Entity* ent = iter->second;
      ent->writeCompletionList(os);
    }


}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
static bool
objectNameParser( std::istringstream& cmdparse,
		  std::string& objName,
		  std::string& funName  )
{
  const int SIZE=128;
  char buffer[SIZE];
  cmdparse >> std::ws;
  cmdparse.getline( buffer,SIZE,'.' );
  if(! cmdparse.good () ) // The callback is not an object method
    return false;

  objName = buffer;
  //cmdparse.getline( buffer,SIZE );
  //funName = buffer;
  cmdparse >> funName;
  return true;
}

SignalBase<int>&
PoolStorage::
getSignal( std::istringstream& sigpath )
{
  std::string objname,signame;
  if(! objectNameParser( sigpath,objname,signame ) )
    { DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_SIGNAL,
				     "Parse error in signal name" ); }

  Entity& ent = getEntity( objname );
  return ent.getSignal( signame );
}

298
PoolStorage* PoolStorage::instance_ = 0;
299