Belle II Software  release-05-02-19
PathIterator.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/core/Path.h>
14 #include <memory>
15 
16 
17 namespace Belle2 {
23  class Module;
24 
28  class PathIterator {
29  public:
31  explicit PathIterator(const PathPtr& path) :
32  m_path(path),
33  m_iter(path->m_elements.begin()),
34  m_end(path->m_elements.end()),
36  {
38  }
39 
41  explicit PathIterator(const PathPtr& path, const PathIterator& parentIterator) :
42  m_path(path),
43  m_iter(path->m_elements.begin()),
44  m_end(path->m_elements.end()),
45  m_parentIterator(new PathIterator(parentIterator))
46  {
48  }
49 
51  void next()
52  {
53  if (!isDone())
54  ++m_iter;
56  }
57 
60  {
61  if (!isDone() and dynamic_cast<Path*>(m_iter->get())) {
62  //we're pointing to another Path
63  *this = PathIterator(std::static_pointer_cast<Path>(*m_iter), *this);
64  }
65  //check _afterwards_ if we need to jump back up
66  if (isDone() and m_parentIterator) {
67  //jump back to parent iterator
68  *this = *(m_parentIterator.get());
69  next(); //go to next module
70  }
71  }
72 
74  bool isDone() const { return (m_iter == m_end); }
75 
77  Module* get() const { return dynamic_cast<Module*>(m_iter->get()); }
78 
79 
80  private:
81  PathPtr m_path;
82  std::list<std::shared_ptr<PathElement> >::const_iterator m_iter;
83  std::list<std::shared_ptr<PathElement> >::const_iterator m_end;
84  std::shared_ptr<PathIterator> m_parentIterator;
85  };
86 
88 } // end namespace Belle2
Belle2::PathIterator::m_parentIterator
std::shared_ptr< PathIterator > m_parentIterator
If this is non-NULL, we jump to m_parentIterator+1 after m_iter is done.
Definition: PathIterator.h:92
Belle2::PathIterator::next
void next()
increment.
Definition: PathIterator.h:59
Belle2::PathIterator::PathIterator
PathIterator(const PathPtr &path)
Constructor.
Definition: PathIterator.h:39
Belle2::PathIterator::m_path
PathPtr m_path
keep the path around to ensure iterators remain valid.
Definition: PathIterator.h:89
Belle2::PathIterator::m_end
std::list< std::shared_ptr< PathElement > >::const_iterator m_end
iterator to end of Path.
Definition: PathIterator.h:91
Belle2::PathIterator::m_iter
std::list< std::shared_ptr< PathElement > >::const_iterator m_iter
wrapped path list iterator.
Definition: PathIterator.h:90
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::PathPtr
std::shared_ptr< Path > PathPtr
Defines a pointer to a path object as a boost shared pointer.
Definition: Path.h:30
Belle2::PathIterator::get
Module * get() const
dereference.
Definition: PathIterator.h:85
Belle2::Path
Implements a path consisting of Module and/or Path objects.
Definition: Path.h:40
Belle2::PathIterator::descendIfNecessary
void descendIfNecessary()
Check if we're pointing to another path and descend if that is the case.
Definition: PathIterator.h:67
Belle2::PathIterator
Iterator over a Path (returning Module pointers).
Definition: PathIterator.h:36
Belle2::PathIterator::isDone
bool isDone() const
Are we finished iterating?
Definition: PathIterator.h:82