Analysis Software
Documentation for sPHENIX simulation 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 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
20 #include <boost/algorithm/string.hpp>
21 #pragma GCC diagnostic pop
22 
23 #include <vector>
24 
26  : currentNode(node)
27 {
28 }
29 
31  : currentNode(nullptr)
32 {
33 }
34 
37 {
40  PHNode* thisNode;
41  while ((thisNode = iter()))
42  {
43  subNodeList.append(thisNode);
44  }
45  return subNodeList;
46 }
47 
49 {
50  currentNode->print();
51 }
52 
53 PHNode*
54 PHNodeIterator::findFirst(const std::string& requiredType, const std::string& requiredName)
55 {
57  PHNode* thisNode;
58  while ((thisNode = iter()))
59  {
60  if (thisNode->getType() == requiredType && thisNode->getName() == requiredName)
61  {
62  return thisNode;
63  }
64  else
65  {
66  if (thisNode->getType() == "PHCompositeNode")
67  {
68  PHNodeIterator nodeIter(static_cast<PHCompositeNode*>(thisNode));
69  PHNode* nodeFoundInSubTree = nodeIter.findFirst(requiredType.c_str(), requiredName.c_str());
70  if (nodeFoundInSubTree) return nodeFoundInSubTree;
71  }
72  }
73  }
74  return nullptr;
75 }
76 
77 PHNode*
79 {
81  PHNode* thisNode;
82  while ((thisNode = iter()))
83  {
84  if (thisNode->getName() == requiredName)
85  {
86  return thisNode;
87  }
88  else
89  {
90  if (thisNode->getType() == "PHCompositeNode")
91  {
92  PHNodeIterator nodeIter(static_cast<PHCompositeNode*>(thisNode));
93  PHNode* nodeFoundInSubTree = nodeIter.findFirst(requiredName.c_str());
94  if (nodeFoundInSubTree)
95  {
96  return nodeFoundInSubTree;
97  }
98  }
99  }
100  }
101  return nullptr;
102 }
103 
104 bool PHNodeIterator::cd(const std::string& pathString)
105 {
106  bool success = true;
107  if (pathString.empty())
108  {
109  while (currentNode->getParent())
110  {
111  currentNode = static_cast<PHCompositeNode*>(currentNode->getParent());
112  }
113  }
114  else
115  {
116  std::vector<std::string> splitpath;
117  boost::split(splitpath, pathString, boost::is_any_of(phooldefs::nodetreepathdelim));
118  bool pathFound;
119  PHNode* subNode;
120  int i = 0;
121  for (const auto & iter : splitpath)
122  {
123  i++;
124  if (iter == "..")
125  {
126  if (currentNode->getParent())
127  {
128  currentNode = static_cast<PHCompositeNode*>(currentNode->getParent());
129  }
130  else
131  {
132  success = false;
133  }
134  }
135  else
136  {
138  pathFound = false;
139  while ((subNode = subNodeIter()))
140  {
141  if (subNode->getType() == "PHCompositeNode" && subNode->getName() == iter)
142  {
143  currentNode = static_cast<PHCompositeNode*>(subNode);
144  pathFound = true;
145  }
146  }
147  if (!pathFound)
148  {
149  success = false;
150  }
151  }
152  }
153  }
154  return success;
155 }
156 
158 {
159  return currentNode->addNode(newNode);
160 }
161 
163 {
164  operation(currentNode);
166  PHNode* thisNode;
167  while ((thisNode = iter()))
168  {
169  if (thisNode->getType() == "PHCompositeNode")
170  {
171  PHNodeIterator subNodeIter(static_cast<PHCompositeNode*>(thisNode));
172  subNodeIter.forEach(operation);
173  }
174  else
175  {
176  operation(thisNode);
177  }
178  }
179 }
180 
182 {
183  forEach(operation);
184 }