Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

NodeWalkingProxy.cpp

Go to the documentation of this file.
00001 #include "NodeWalkingProxy.h"
00002 #include "X3DComponentWalker.h"
00003 #include "WalkingFunction.h"
00004 #include "NodeVisitingProxy.h"
00005 #include "RootWalker.h"
00006 #include "Type.h"
00007 #include "X3DNode.h"
00008 
00009 using namespace X3DTK;
00010 using namespace std;
00011 
00012 NodeWalkingProxy::NodeWalkingProxy()
00013 : X3DNodeProxy()
00014 {
00015   Type::addNodeProxy(this);
00016   nodeVisitingProxy_ = 0;
00017   setComponentWalker(new RootWalker());
00018 }
00019 
00020 NodeWalkingProxy::~NodeWalkingProxy()
00021 {
00022   if (autoDelete)
00023     for (list<X3DComponentWalker *>::iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00024     {
00025       (*it)->removeOneProxy();
00026       if ((*it)->getProxyNumber() == 0)
00027         delete *it;
00028     }    
00029  
00030   Type::removeNodeProxy(this);
00031 }
00032 
00033 void NodeWalkingProxy::setNodeVisitingProxy(const NodeVisitingProxy *nodeVisitingProxy)
00034 {
00035   nodeVisitingProxy_ = (NodeVisitingProxy *)nodeVisitingProxy;
00036   for (list<X3DComponentWalker *>::iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00037     (*it)->setNodeVisitingProxy(nodeVisitingProxy);
00038 }
00039 
00040 void NodeWalkingProxy::setComponentWalker(X3DComponentWalker *component)
00041 {
00042   //replace the equivalent component.
00043   X3DComponentWalker *replacedComponent = 0;
00044   bool replaced = false;
00045   for (list<X3DComponentWalker *>::iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00046   {
00047     if ((*it)->getName() == component->getName())
00048     {
00049       replacedComponent = *it;
00050       *it = (X3DComponentWalker *)component; 
00051       replacedComponent->removeOneProxy();
00052       component->addOneProxy();      
00053       replaced = true;
00054     }  
00055   }
00056   
00057   if (replacedComponent == component)
00058     return;
00059   
00060   if (!replaced)
00061   {
00062     componentList_.push_back((X3DComponentWalker *)component);
00063     component->addOneProxy();
00064   }  
00065   
00066   //remove replaced component.
00067   if ((autoDelete) && (replacedComponent != 0) && (replacedComponent->getProxyNumber() == 0))
00068     delete replacedComponent;
00069         
00070   component->setNodeWalkingProxy(this);
00071   component->setNodeVisitingProxy(nodeVisitingProxy_);
00072   
00073   SFString name = component->getName();
00074     
00075   //iterating the types.
00076   for (unsigned int id = 0; id < walkingArray_.size(); ++id)
00077     walkingArray_[id] = getWalkingFunctionOf(Type::getTypeOfId(id));
00078 }
00079 
00080 void NodeWalkingProxy::reset()
00081 {
00082   //remove components
00083   if (autoDelete)
00084     for (list<X3DComponentWalker *>::iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00085     {
00086       (*it)->removeOneProxy();
00087       if ((*it)->getProxyNumber() == 0)
00088         delete *it;
00089     }    
00090  
00091   componentList_.clear();
00092 }
00093 
00094 void NodeWalkingProxy::addType(const Type *type)
00095 {
00096   //resize the array.
00097   if ((int)walkingArray_.size() <= type->getId())
00098     walkingArray_.resize(type->getId() + 1);
00099 
00100   walkingArray_[type->getId()] = getWalkingFunctionOf(type);
00101 }
00102 
00103 WalkingFunction *NodeWalkingProxy::getWalkingFunctionOf(const Type *type) const
00104 {
00105   Type *t = (Type *)type;
00106   WalkingFunction *WF = 0;
00107   while (t != 0)
00108   {
00109     SFString name = t->getName();
00110     for (list<X3DComponentWalker *>::const_iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00111     {
00112       WF = (*it)->getWalkingFunctionOf(name);
00113       if (WF != 0)
00114         break;
00115     }
00116     if (WF != 0)
00117       break;
00118     
00119     t = t->getParent();
00120   }
00121   
00122   return WF;
00123 }
00124 
00125 NodeWalkingProxy *X3DTK::joinNodeWalkingProxies(NodeWalkingProxy *N0, NodeWalkingProxy *N1)
00126 {
00127   NodeWalkingProxy *N = new NodeWalkingProxy();
00128   
00129   for (list<X3DComponentWalker *>::const_iterator it = N0->componentList_.begin(); it != N0->componentList_.end(); ++it)
00130     N->setComponentWalker(*it);
00131   
00132   for (list<X3DComponentWalker *>::const_iterator it = N1->componentList_.begin(); it != N1->componentList_.end(); ++it)
00133     N->setComponentWalker(*it);
00134   
00135   return N;  
00136 }

Generated on Wed May 14 10:03:10 2003 for X3DToolKit by doxygen1.3