Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:26

0001 //-----------------------------------------------------------------------------
0002 //
0003 //  The PHOOL's Software
0004 //  Copyright (C) PHENIX collaboration, 1999
0005 //
0006 //  Implementation of class PHCompositeNode
0007 //
0008 //-----------------------------------------------------------------------------
0009 #include "PHCompositeNode.h"
0010 #include "PHPointerListIterator.h"
0011 #include "phool.h"
0012 #include "phooldefs.h"
0013 
0014 #include <iostream>
0015 
0016 PHCompositeNode::PHCompositeNode(const std::string& n)
0017   : PHNode(n, "PHCompositeNode")
0018 {
0019   type = "PHCompositeNode";
0020 }
0021 
0022 PHCompositeNode::~PHCompositeNode()
0023 {
0024   // we need to mark this node to be to deleted
0025   // The recursive taking out of deleted subnodes via
0026   // forgetMe interferes with the way the PHPointerList::clearAndDestroy()
0027   // works but it has to be executed in case the PHCompositeNode is
0028   // a parent and supposed to stay. Then the deleted node has to take itself
0029   // out of the node list
0030   deleteMe = 1;
0031   subNodes.clearAndDestroy();
0032 }
0033 
0034 bool PHCompositeNode::addNode(PHNode* newNode)
0035 {
0036   //
0037   // Check all existing subNodes for name-conflict.
0038   //
0039   PHPointerListIterator<PHNode> nodeIter(subNodes);
0040   PHNode* thisNode;
0041   while ((thisNode = nodeIter()))
0042   {
0043     if (thisNode->getName() == newNode->getName())
0044     {
0045       std::cout << PHWHERE << "Node " << newNode->getName()
0046                 << " already exists" << std::endl;
0047       return false;
0048     }
0049   }
0050   //
0051   // No conflict, so we can append the new node.
0052   //
0053   newNode->setParent(this);
0054   return (subNodes.append(newNode));
0055 }
0056 
0057 void PHCompositeNode::prune()
0058 {
0059   PHPointerListIterator<PHNode> nodeIter(subNodes);
0060   PHNode* thisNode;
0061   while ((thisNode = nodeIter()))
0062   {
0063     if (!thisNode->isPersistent())
0064     {
0065       subNodes.removeAt(nodeIter.pos());
0066       --nodeIter;
0067       delete thisNode;
0068     }
0069     else
0070     {
0071       thisNode->prune();
0072     }
0073   }
0074 }
0075 
0076 void PHCompositeNode::forgetMe(PHNode* child)
0077 {
0078   // if this PHCompositeNode is supposed to be deleted,
0079   // do not remove the child from the list,
0080   // otherwise the clearanddestroy() bookkeeping gets
0081   // confused and deletes only every other node
0082   if (deleteMe)
0083   {
0084     return;
0085   }
0086   PHPointerListIterator<PHNode> nodeIter(subNodes);
0087   PHNode* thisNode;
0088   while (child && (thisNode = nodeIter()))
0089   {
0090     if (thisNode == child)
0091     {
0092       subNodes.removeAt(nodeIter.pos());
0093       child = nullptr;
0094     }
0095   }
0096 }
0097 
0098 bool PHCompositeNode::write(PHIOManager* IOManager, const std::string& path)
0099 {
0100   std::string newPath = name;
0101   if (!path.empty())
0102   {
0103     newPath = path + phooldefs::branchpathdelim + name;
0104   }
0105   PHPointerListIterator<PHNode> nodeIter(subNodes);
0106   PHNode* thisNode;
0107   bool success = true;
0108   while ((thisNode = nodeIter()))
0109   {
0110     if (!(thisNode->write(IOManager, newPath)))
0111     {
0112       success = false;
0113     }
0114   }
0115   return success;
0116 }
0117 
0118 void PHCompositeNode::print(const std::string& path)
0119 {
0120   std::string newPath = "   " + path;
0121   std::cout << path << name << " (" << type << ")/" << std::endl;
0122   PHPointerListIterator<PHNode> nodeIter(subNodes);
0123   PHNode* thisNode;
0124   while ((thisNode = nodeIter()))
0125   {
0126     thisNode->print(newPath);
0127   }
0128 }