ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHTypedNodeIterator.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHTypedNodeIterator.h
1 #ifndef PHOOL_PHTYPEDNODEITERATOR_H
2 #define PHOOL_PHTYPEDNODEITERATOR_H
3 
4 #include "PHNodeIterator.h"
5 
6 #include <cstddef>
7 
8 class TObject;
9 template <typename T>
10 class PHIODataNode;
11 class PHCompositeNode;
12 
13 // A special PHOOL node iterator that simplifies finding and adding
14 // data nodes. This version is a templated class, to allow its use
15 // interactively in CINT. (CINT only allows function templates in
16 // very recent versions, which we aren't using yet.)
17 // @author Kyle Pope
18 
19 template <class T>
21 {
22  public:
25  : PHNodeIterator(n)
26  {
27  myIODataNode = nullptr;
28  }
29 
31  : PHNodeIterator()
32  {
33  myIODataNode = nullptr;
34  }
35 
36  T& operator*();
37 
39  // virtual ~PHTypedNodeIterator(); // Need a virtual ~PHNodeIterator() !
40  ~PHTypedNodeIterator() override {}
46  PHIODataNode<T>* FindIODataNode(const char* name);
47 
48  PHIODataNode<T>* find(const char* name);
49 
56  bool AddIODataNode(T* data, const char* name);
57 
58  bool insert(T* data, const char* name);
59 
60  protected:
62 };
63 
64 template <class T>
65 T&
67 {
68  return *(myIODataNode->getData());
69 }
70 
71 template <class T>
74 {
75  return find(name);
76 }
77 
78 template <class T>
81 {
82  // TODO: also check that "name" is not a null string!
83  if (!name)
84  {
85  return 0;
86  }
87 
88  // Can't do dynamic_cast here; it fails if node was created as
89  // PHIODataNode<X> instead of PHIODataNode<T>, even if T is a
90  // derived class of X! In general, T -> X does not imply A<T> ->
91  // A<X>. ("->" denotes "derives from", and "A" is any template
92  // class)
93  myIODataNode =
94  static_cast<PHIODataNode<T>*>(findFirst("PHIODataNode", name));
95 
96  return myIODataNode;
97 }
98 
99 template <class T>
101 {
102  return insert(data, name);
103 }
104 
105 template <class T>
107 {
108  // TODO: also check that "name" is not a null string!
109  if (!name)
110  {
111  return false;
112  }
113 
114  // For IODataNode, ought to check (if possible) that T derives from
115  // TObject. Will typeid() give us this info?
117  if (!n)
118  {
119  // problem creating node?
120  return false;
121  }
122 
123  return addNode(n);
124 }
125 
126 // Typedef to simplify notation.
128 
129 #endif