DABC (Data Acquisition Backbone Core)  2.9.9
Iterator.cxx
Go to the documentation of this file.
1 // $Id: Iterator.cxx 1606 2013-05-28 12:57:02Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "dabc/Iterator.h"
17 
18 #include "dabc/logging.h"
19 
20 dabc::Iterator::Iterator(Reference topfolder, int maxlevel) :
21  fTop(topfolder),
22  fCurrent(),
23  fIndexes(),
24  fFolders(),
25  fFullName(),
26  fMaxLevel(maxlevel)
27 {
28 }
29 
30 dabc::Iterator::Iterator(Object* topfolder, int maxlevel) :
31  fTop(topfolder),
32  fCurrent(),
33  fIndexes(),
34  fFolders(),
35  fFullName(),
36  fMaxLevel(maxlevel)
37 {
38 }
39 
41 {
42 }
43 
44 dabc::Object* dabc::Iterator::next(bool goinside)
45 {
46  if (fCurrent()==0) {
47  fCurrent = fTop;
48  fIndexes.clear();
49  fFolders.Clear();
50  } else {
51 
52  int sz = fIndexes.size();
53  bool doagain = true;
54 
55  Reference child;
56 
57  if (goinside && (fCurrent()!=0) && ((fMaxLevel<0) || (sz<fMaxLevel)))
58  child = fCurrent()->GetChildRef(0);
59 
60  if (child()!=0) {
61  fIndexes.resize(sz+1);
62  fIndexes[sz] = 0;
63  fFolders.Add(fCurrent);
64  fCurrent = child;
65  } else
66 
67  while (doagain) {
68 
69  if (sz==0) { fCurrent.Release(); break; }
70 
71  fIndexes[sz-1]++;
72 
73  Reference child = fFolders.GetObject(sz-1)->GetChildRef(fIndexes[sz-1]);
74 
75  if (child()!=0) {
76  doagain = false;
77  fCurrent = child;
78  } else {
79  sz--;
80  fFolders.RemoveAt(sz);
81  fIndexes.resize(sz);
82  }
83  }
84  }
85 
86  if (fCurrent()) fFullName = fCurrent()->ItemName(false);
87 
88  return fCurrent();
89 }
90 
92 {
93  return (lvl<fFolders.GetSize()) ? fFolders[lvl].GetObject() : 0;
94 }
95 
96 
97 void dabc::Iterator::PrintHieararchy(Reference ref)
98 {
99  dabc::Iterator iter(ref);
100  while (iter.next())
101  DOUT0("%*s %s", iter.level()*2, "", iter.name());
102 }
103 
Iterator over objects hierarchy
Definition: Iterator.h:36
virtual ~Iterator()
Definition: Iterator.cxx:40
Object * next(bool goinside=true)
Definition: Iterator.cxx:44
Object * parent(unsigned lvl)
Returns parents of current object.
Definition: Iterator.cxx:91
Iterator(Reference ref, int maxlevel=-1)
Limit how deep iterator allowed to go inside hierarchy.
Definition: Iterator.cxx:20
static void PrintHieararchy(Reference ref)
Definition: Iterator.cxx:97
Base class for most of the DABC classes.
Definition: Object.h:116
Reference GetChildRef(unsigned n) const
returns reference on child object
Definition: Object.cxx:684
void Release()
Releases reference on the object.
Definition: Reference.cxx:138
#define DOUT0(args ...)
Definition: logging.h:156