ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHNodeIterator.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHNodeIterator.cc
1 //-----------------------------------------------------------------------------
2 //
3 // The PHOOL's Software
4 // Copyright (C) PHENIX collaboration, 1999
5 //
6 // Implementation of class PHNodeIterator
7 //
8 // Author: Matthias Messer
9 //-----------------------------------------------------------------------------
10 #include "PHNodeIterator.h"
11 
12 #include "PHCompositeNode.h"
13 #include "PHNode.h"
14 #include "PHNodeOperation.h"
15 #include "PHPointerListIterator.h"
16 #include "phooldefs.h"
17 
18 #include <boost/algorithm/string.hpp>
19 
20 #include <utility>
21 #include <vector>
22 
24  : currentNode(node)
25 {
26 }
27 
29  : currentNode(nullptr)
30 {
31 }
32 
35 {
38  PHNode* thisNode;
39  while ((thisNode = iter()))
40  {
41  subNodeList.append(thisNode);
42  }
43  return subNodeList;
44 }
45 
47 {
48  currentNode->print();
49 }
50 
51 PHNode*
52 PHNodeIterator::findFirst(const std::string& requiredType, const std::string& requiredName)
53 {
55  PHNode* thisNode;
56  while ((thisNode = iter()))
57  {
58  if (thisNode->getType() == requiredType && thisNode->getName() == requiredName)
59  {
60  return thisNode;
61  }
62  else
63  {
64  if (thisNode->getType() == "PHCompositeNode")
65  {
66  PHNodeIterator nodeIter(static_cast<PHCompositeNode*>(thisNode));
67  PHNode* nodeFoundInSubTree = nodeIter.findFirst(requiredType.c_str(), requiredName.c_str());
68  if (nodeFoundInSubTree) return nodeFoundInSubTree;
69  }
70  }
71  }
72  return 0;
73 }
74 
75 PHNode*
76 PHNodeIterator::findFirst(const std::string& requiredName)
77 {
79  PHNode* thisNode;
80  while ((thisNode = iter()))
81  {
82  if (thisNode->getName() == requiredName)
83  {
84  return thisNode;
85  }
86  else
87  {
88  if (thisNode->getType() == "PHCompositeNode")
89  {
90  PHNodeIterator nodeIter(static_cast<PHCompositeNode*>(thisNode));
91  PHNode* nodeFoundInSubTree = nodeIter.findFirst(requiredName.c_str());
92  if (nodeFoundInSubTree)
93  {
94  return nodeFoundInSubTree;
95  }
96  }
97  }
98  }
99  return 0;
100 }
101 
102 bool PHNodeIterator::cd(const std::string& pathString)
103 {
104  bool success = true;
105  if (pathString.empty())
106  {
107  while (currentNode->getParent())
108  {
109  currentNode = static_cast<PHCompositeNode*>(currentNode->getParent());
110  }
111  }
112  else
113  {
114  std::vector<std::string> splitpath;
115  boost::split(splitpath, pathString, boost::is_any_of(phooldefs::nodetreepathdelim));
116  bool pathFound;
117  PHNode* subNode;
118  int i = 0;
119  for (std::vector<std::string>::const_iterator iter = splitpath.begin(); iter != splitpath.end(); ++iter)
120  {
121  i++;
122  if (*iter == "..")
123  {
124  if (currentNode->getParent())
125  {
126  currentNode = static_cast<PHCompositeNode*>(currentNode->getParent());
127  }
128  else
129  {
130  success = false;
131  }
132  }
133  else
134  {
136  pathFound = false;
137  while ((subNode = subNodeIter()))
138  {
139  if (subNode->getType() == "PHCompositeNode" && subNode->getName() == *iter)
140  {
141  currentNode = static_cast<PHCompositeNode*>(subNode);
142  pathFound = true;
143  }
144  }
145  if (!pathFound)
146  {
147  success = false;
148  }
149  }
150  }
151  }
152  return success;
153 }
154 
156 {
157  return currentNode->addNode(newNode);
158 }
159 
161 {
162  operation(currentNode);
164  PHNode* thisNode;
165  while ((thisNode = iter()))
166  {
167  if (thisNode->getType() == "PHCompositeNode")
168  {
169  PHNodeIterator subNodeIter(static_cast<PHCompositeNode*>(thisNode));
170  subNodeIter.forEach(operation);
171  }
172  else
173  {
174  operation(thisNode);
175  }
176  }
177 }
178 
180 {
181  forEach(operation);
182 }