Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:16:19

0001 #ifndef PHOOL_PHDATANODEITERATOR_H
0002 #define PHOOL_PHDATANODEITERATOR_H
0003 
0004 #include "PHIODataNode.h"
0005 #include "PHNodeIterator.h"
0006 
0007 #include <cstddef>
0008 
0009 /**
0010  * A special PHOOL node iterator that simplifies finding and adding data
0011  * nodes.
0012  *
0013  * The methods are templated, rather than the class, to allow the same
0014  * iterator object to be used with different data node types.
0015  *
0016  * @author Kyle Pope
0017  */
0018 
0019 class PHDataNodeIterator : public PHNodeIterator
0020 {
0021  public:
0022   /// Constructor
0023   PHDataNodeIterator(PHCompositeNode* node);
0024 
0025   /// Destructor
0026   ~PHDataNodeIterator() override = default;
0027   /**
0028    * Finds an IODataNode of name "name" containing data of type "T".
0029    * A null pointer will be returned if the node is not found, or if
0030    * it contains data of the wrong type.  Note that the return
0031    * variable is also the first argument; this is necessary to resolve
0032    * the template type.
0033    */
0034   template <class T>
0035   PHIODataNode<T>* FindIODataNode(PHIODataNode<T>* node,
0036                                   const char* name);
0037 
0038   /**
0039    * Adds a data node called "name" to the tree, and inserts "data".
0040    * The data node is added at the current "directory" of this iterator
0041    * object, so remember to "cd" to the desired location in the tree!
0042    *
0043    */
0044   template <class T>
0045   PHBoolean AddIODataNode(T* data, const char* name);
0046 };
0047 
0048 inline PHDataNodeIterator::PHDataNodeIterator(PHCompositeNode* node)
0049   : PHNodeIterator(node)
0050 {
0051 }
0052 
0053 template <class T>
0054 PHIODataNode<T>*
0055 PHDataNodeIterator::FindIODataNode(PHIODataNode<T>* node,
0056                                    const char* name)
0057 {
0058   // TODO:  also check that "name" is not a null string!
0059   if (!name)
0060   {
0061     return 0;
0062   }
0063   // Can't do dynamic_cast here; it fails if node was created as
0064   // PHIODataNode<X> instead of PHIODataNode<T>, even if T is a
0065   // derived class of X!
0066   // In general, T -> X does not imply A<T> -> A<X>.
0067   // ("->" denotes "derives from", and "A" is any template class)
0068   node = static_cast<PHIODataNode<T>*>(findFirst("PHIODataNode",
0069                                                  name));
0070   return node;
0071 }
0072 
0073 template <class T>
0074 PHBoolean
0075 PHDataNodeIterator::AddIODataNode(T* data, const char* name)
0076 {
0077   // TODO:  also check that "name" is not a null string!
0078   if (!name)
0079   {
0080     return false;
0081   }
0082   // For IODataNode, ought to check (if possible) that T derives
0083   // from TObject.  Will typeid() give us this info?
0084 
0085   PHIODataNode<T>* n = new PHIODataNode<T>(data, name);
0086   if (!n)
0087   {
0088     return false;  // problem creating node?
0089   }
0090   return addNode(n);
0091 }
0092 
0093 #endif