Cat
Hierarchy.cpp
Go to the documentation of this file.
1 // $Id: $
2 // Include files
3 
4 // local
5 #include "Hierarchy.h"
6 
7 //-----------------------------------------------------------------------------
8 // Implementation file for class : Hierarchy
9 //
10 // 2006-10-26 :
11 //-----------------------------------------------------------------------------
12 
13 //=============================================================================
14 // Standard constructor, initializes variables
15 //=============================================================================
17  m_parent (0){
18  m_origin=this;
19 }
20 
21 //=============================================================================
22 // Destructor
23 //=============================================================================
25  if (0!=parent()){
26  parent()->delChild(this);
27  }
28  debug("deleting Hierarchy "+name(),"Hierarchy::~Hierarchy");
29 }
30 
31 
32 //=========================================================================
33 //
34 //=========================================================================
36  std::vector<Hierarchy*> listlocale;
37  std::vector<Hierarchy*>::iterator iter;
38  info("loop on "+name()+" children.","Hierarchy::clear");
39  for (iter=m_children.begin();iter!=m_children.end();iter++){
40  info("processing "+(*iter)->name()+".","Hierarchy::clear");
41 /*
42  (*iter)->clear();
43 // this->delChild((*iter));
44  info("obj "+(*iter)->name()+" being cleared.","Hierarchy::clear");
45  delete (*iter);
46  info("Object deleted.","Hierarchy::clear");
47  m_children.erase(iter);
48  info("Object removed from the tree.","Hierarchy::clear");
49 */
50  (*iter)->clear();
51  info("Adding object "+(*iter)->name()+" from the Hierarchy to the list of deleted objects.","Hierarchy::clear");
52  listlocale.push_back((*iter));
53  }
54 
55  for (iter=listlocale.begin();iter!=listlocale.end();iter++){
56  info("Removing object "+(*iter)->name()+".","Hierarchy::clear");
57  this->delChild(*iter);
58 // m_children.erase(iter);
59  delete (*iter);
60  }
61  info("Getting out of "+name());
62 }
63 
64 //=========================================================================
65 //
66 //=========================================================================
68  m_parent = parent;
69  if ((Hierarchy*)0==parent) return;
70 
71  Hierarchy *curr = this;
72  Hierarchy *prev = parent;
73  while ( prev != 0 ){
74  curr = prev;
75  prev = curr->parent();
76  }
77  if (parent->origin()!=0) m_origin=parent->origin();
78 }
79 
80 //=========================================================================
81 //
82 //=========================================================================
84  element->setParent(this);
85  m_children.push_back(element);
86  debug(element->name()+" added to the child tree.","Hierarchy::addChild");
87 }
88 
89 //=========================================================================
90 //
91 //=========================================================================
93  bool flag=false;
94  std::vector<Hierarchy*>::iterator iter,remove;
95  for (iter=m_children.begin();(iter!=m_children.end());iter++){
96  if (*iter==element){
97  remove=iter;
98  flag=true;
99  }
100  }
101  if (flag){
102  debug("removing "+(*remove)->name()+" from the tree.","Hierarchy::delChild");
103  m_children.erase(remove);
104  }
105 }
106 
107 //=========================================================================
108 //
109 //=========================================================================
110 void Hierarchy::delChild(std::string n){
111  bool flag=false;
112  std::vector<Hierarchy*>::iterator iter,remove;
113  for (iter=m_children.begin();iter!=m_children.end();iter++){
114  if ((*iter)->name()==n){ remove=iter; flag=true;}
115  }
116  if (flag){
117  debug("removing "+(*remove)->name()+" from the tree.","Hierarchy::delChild");
118  m_children.erase(remove);
119  }
120 }
121 
122 //=========================================================================
123 //
124 //=========================================================================
125 unsigned long Hierarchy::numberOfChildren ( ){
126  return m_children.size();
127 }
128 
129 
130 //=========================================================================
131 //
132 //=========================================================================
134  std::string newpath = path;
135  std::string up("..");
136  std::string separator(1,'/');
137 
138  Hierarchy * newcurrent = 0;
139 
140  // info("path="+path,"Hierarchy::child");
141 
142  if (path.compare("")==0 || path.compare("/")==0) {
143  // debug("return origin","Hierarchy::child");
144  return origin();
145  }
146 
147  if (path.compare(name())==0){
148  // debug("return itself","Hierarchy::child");
149  return this;
150  }
151 
152  if (path.compare("..")==0){
153  if (0!=this->parent()) return this->parent();
154  else return this;
155  }
156 
157  if (path.compare("../")==0){
158  if (0!=this->parent()) return this->parent();
159  else return this;
160  }
161 
162 
163  int npos=path.find(separator,0);
164 
165  // info("find separator in "+itos(npos)+" of "+path,"Hierarchy::child");
166 
167  // remove last separator
168  if ( npos == (int)(path.size()-1) ) {
169  newpath = std::string(path,0,npos);
170  path = newpath;
171  }
172 
173  if (npos==0){
174  // debug("Going back to origin and calling child","Hierarchy::child");
175  newpath=std::string(path,1,path.size()-1);
176  return origin()->child(newpath);
177  }
178  else{
179  if ( npos== (int)(std::string::npos) ){
180  // debug("Getting chid "+path+" of "+this->name(),"Hierarchy::child");
181  std::vector <Hierarchy*> list = children();
182  std::vector<Hierarchy*>::iterator iter;
183  for (iter=list.begin();iter!=list.end();iter++){
184  if ((*iter)->name().compare(path)==0){
185  return *iter;
186  }
187  }
188  warning(this->name()+std::string(" has no child '")+path+"'","Hierarchy::child");
189  return this;
190  }
191  else
192  {
193  int ipos=path.find(separator,0);
194  // info("default behaviour "+path+" with separator in "+itos(ipos),"Hierarchy::child");
195 
196  std::string newcurrentname=std::string(path,0,ipos);
197  newpath=std::string(path,ipos+1,path.size()-1);
198 
199  // info("looking now for "+newpath+" from "+newcurrentname,"Hierarchy::child");
200 
201  if (0==newcurrentname.compare(origin()->name())){
202  // info("current is computer. Looking for children"+newcurrentname,"Hierarchy::child");
203  return origin()->child(newpath);
204  }
205 
206  newcurrent = (Hierarchy*)0;
207 
208  std::vector <Hierarchy*> list = children();
209  std::vector<Hierarchy*>::iterator iter;
210  for (iter=list.begin();iter!=list.end();iter++){
211  if ((*iter)->name().compare(newcurrentname)==0){
212  newcurrent = (*iter);
213  }
214  }
215 
216 
217  if ((Hierarchy*)0==newcurrent){
218  if (newcurrentname.compare("..")==0 && 0!=parent()){
219  newcurrent=this->parent();
220  // debug("newcurrent was .. -> parent="+parent()->name());
221  }
222  else
223  {
224  warning(this->name()+" has no child '"+newcurrentname+"'",
225  "Hierarchy::child");
226  return this;
227  }
228  }
229  // debug("recurrence call for "+newpath+" on "+newcurrent->name(),"Hierarchy::child");
230  return newcurrent -> child ( newpath );
231  }
232  }
233 }
234 
235 
236 //=========================================================================
237 //
238 //=========================================================================
240 
241  std::string newpath = path;
242 
243  std::string up("..");
244  std::string separator(1,'/');
245  std::string typeopen(1,'[');
246  std::string typeclose(1,']');
247 
248  Hierarchy * newcurrent = 0;
249 
250  unsigned int npos=path.find(separator,0);
251  unsigned int opos=path.find(typeopen,0);
252  if ( npos==std::string::npos || npos == path.size()-1 ){
253  if ( path.compare("..")==0 ) {
254  return parent();
255  }
256 
257  if ( npos == path.size()-1 ) {
258  newpath = std::string(path,0,opos);
259  path = newpath;
260  }
261 
262  std::vector < Hierarchy* > list = children();
263  std::vector < Hierarchy* >::iterator iter;
264  for (iter=list.begin();iter!=list.end();iter++){
265  std::string notypepath = std::string(path,0,opos);
266  if ((*iter)->name().compare(notypepath)==0){
267  return *iter;
268  }
269  }
270  warning(this->name()+std::string(" has no child ") +path,"Hierarchy::child");
271  return 0;
272  }
273 
274  else {
275 
276  if (std::string(path,0,3).compare(std::string("../"))==0) {
277  newpath=std::string(path,3,path.size()-3);
278  newcurrent = parent();
279  }
280  if (std::string(path,0,1).compare(std::string("/"))==0) {
281  newpath=std::string(path,1,path.size()-1);
282  newcurrent = ( Hierarchy* ) m_origin;
283  }
284  if ((std::string(path,0,3).compare(std::string("../")) !=0 ) &&
285  std::string(path,0,1).compare(std::string("/"))!=0 ) {
286  opos = path.find(typeopen,0);
287  int cpos = path.find(typeclose,0);
288  std::string name = std::string (path,0,opos);
289  newcurrent = childTyped( name );
290  if (newcurrent ==0){
291  warning(path+": no child found with such a name","Hierarchy::child");
292  }
293  newpath = std::string (path,cpos+2,path.size()-cpos-1);
294  }
295  return newcurrent -> childTyped ( newpath );
296  }
297 }
298 
299 
300 //=========================================================================
301 //
302 //=========================================================================
304  return ( m_children.size()>0 );
305 }
306 
307 //=========================================================================
308 //
309 //=========================================================================
310 void Hierarchy::tree(std::string indent){
311  std::string line=indent+" -> "+name()+"["+type()+"] ";
312  if (std::string("")==indent){
313  info("Hierarchy for Object "+name()+"["+type()+"]","Hierarchy::tree");
314  }
315  msgSvc(MsgSvc::NONE,line,"");
316  std::vector<Hierarchy*> list = children();
317  std::vector<Hierarchy*>::iterator iter;
318  for (iter=list.begin() ; iter!=list.end() ; ++iter){
319  std::string tab=" ";
320  (*iter)->tree(indent+tab);
321  }
322 }
323 
324 //=========================================================================
325 //
326 //=========================================================================
328  Hierarchy *parent = this->parent();
329  if ( 0 != parent){
330  if (parent->type().compare( type )==0) {
331  return parent;
332  }
333  else
334  return parent->parent( type );
335  }
336  else {
337  return (Hierarchy*)NULL;
338  }
339 }
340 
341 //=========================================================================
342 //
343 //=========================================================================
344 std::string Hierarchy::path(std::string str){
345  str="/"+name()+str;
347  if (0!=m_parent){
348  return m_parent->path(str);
349  }
350  return str;
351 }
352 
353 //=========================================================================
354 //
355 //=========================================================================
356 std::string Hierarchy::pathTyped(std::string str){
358  if (0!=m_parent){
359  str="/"+name()+"["+type()+"]"+str;
360  return m_parent->pathTyped(str);
361  }
362  return str;
363 }
364 
365 //=============================================================================
void info(std::string mymsg)
Definition: Object.h:38
Hierarchy * childTyped(std::string)
Definition: Hierarchy.cpp:239
Hierarchy * child(std::string)
Definition: Hierarchy.cpp:133
Hierarchy()
Standard constructor.
Definition: Hierarchy.cpp:16
void setParent(Hierarchy *parent)
Definition: Hierarchy.cpp:67
Hierarchy * m_origin
Definition: Hierarchy.h:52
bool hasChildren()
Definition: Hierarchy.cpp:303
std::vector< Hierarchy * > children()
Definition: Hierarchy.h:33
void delChild(Hierarchy *)
Definition: Hierarchy.cpp:92
Hierarchy * parent()
Definition: Hierarchy.h:28
unsigned long numberOfChildren()
Definition: Hierarchy.cpp:125
std::vector< Hierarchy * > m_children
Definition: Hierarchy.h:53
virtual ~Hierarchy()
Destructor.
Definition: Hierarchy.cpp:24
void debug(std::string mymsg)
Definition: Object.h:37
void tree()
Definition: Hierarchy.h:46
std::string name() const
Definition: Object.h:28
virtual void addChild(Hierarchy *element)
Definition: Hierarchy.cpp:83
void msgSvc(int level, std::string msg, std::string name)
Definition: Object.h:33
std::string pathTyped(std::string=std::string(""))
Definition: Hierarchy.cpp:356
std::string type()
Definition: Object.h:29
Hierarchy * m_parent
Definition: Hierarchy.h:46
void warning(std::string mymsg)
Definition: Object.h:39
void clear()
Definition: Hierarchy.cpp:35
Hierarchy * origin()
Definition: Hierarchy.h:30
std::string path(std::string=std::string(""))
Definition: Hierarchy.cpp:344