00001 #include "NodeCreationProxy.h"
00002 #include "X3DComponentCreator.h"
00003 #include "CreationFunction.h"
00004 #include "Type.h"
00005 #include "Component.h"
00006
00007 #include <iostream>
00008
00009 using namespace X3DTK;
00010 using namespace std;
00011
00012 NodeCreationProxy::NodeCreationProxy()
00013 : X3DNodeProxy()
00014 {
00015 Type::addNodeProxy(this);
00016 }
00017
00018 NodeCreationProxy::~NodeCreationProxy()
00019 {
00020 if (autoDelete)
00021 for (list<X3DComponentCreator *>::iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00022 {
00023 (*it)->removeOneProxy();
00024 if ((*it)->getProxyNumber() == 0)
00025 delete *it;
00026 }
00027
00028 Type::removeNodeProxy(this);
00029 }
00030
00031 void NodeCreationProxy::setComponentCreator(X3DComponentCreator *component)
00032 {
00033
00034 X3DComponentCreator *replacedComponent = 0;
00035 bool replaced = false;
00036
00037 for (list<X3DComponentCreator *>::iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00038 {
00039 if ((*it)->getName() == component->getName())
00040 {
00041 replacedComponent = *it;
00042 *it = component;
00043 replacedComponent->removeOneProxy();
00044 component->addOneProxy();
00045 replaced = true;
00046 }
00047 }
00048
00049 if (!replaced)
00050 {
00051 componentList_.push_back(component);
00052 component->addOneProxy();
00053 }
00054 else
00055 {
00056
00057 CreationDict componentCreationDict = replacedComponent->getCreationDict();
00058 for (CreationDict::iterator it = componentCreationDict.begin(); it != componentCreationDict.end(); ++it)
00059 creationDict_.erase((*it).first);
00060 }
00061
00062
00063 if ((autoDelete) && (replacedComponent != 0) && (replacedComponent->getProxyNumber() == 0))
00064 delete replacedComponent;
00065
00066
00067 CreationDict componentCreationDict = component->getCreationDict();
00068 for (CreationDict::const_iterator it = componentCreationDict.begin(); it != componentCreationDict.end(); ++it)
00069 creationDict_[(*it).first] = (*it).second;
00070 }
00071
00072 SFNode NodeCreationProxy::createFromName(const SFString &name) const
00073 {
00074 CreationDict::const_iterator it = creationDict_.find(name);
00075 if (it != creationDict_.end())
00076 return (*it).second->create();
00077
00078 cerr << "NodeCreationProxy::createFromName: " << name << " has no CreationFunction!" << endl;
00079 return 0;
00080 }
00081
00082 void NodeCreationProxy::reset()
00083 {
00084
00085 if (autoDelete)
00086 for (list<X3DComponentCreator *>::iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00087 {
00088 (*it)->removeOneProxy();
00089 if ((*it)->getProxyNumber() == 0)
00090 delete *it;
00091 }
00092
00093 creationDict_.clear();
00094 componentList_.clear();
00095 }
00096
00097 SFString NodeCreationProxy::getComponentNameOf(const SFString &name) const
00098 {
00099
00100 for (list<X3DComponentCreator *>::const_iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00101 {
00102 if ((*it)->contains(name))
00103 return (*it)->getName();
00104 }
00105
00106 return "";
00107 }
00108
00109 void NodeCreationProxy::addType(const Type *type)
00110 {
00111
00112 SFString name = type->getName();
00113 CreationFunction *CF = getCreationFunctionOf(name);
00114 creationDict_[name] = CF;
00115 }
00116
00117 CreationFunction *NodeCreationProxy::getCreationFunctionOf(const SFString &name) const
00118 {
00119 CreationFunction *CF = 0;
00120
00121 for (list<X3DComponentCreator *>::const_iterator it = componentList_.begin(); it != componentList_.end(); ++it)
00122 {
00123 CF = (*it)->getCreationFunctionOf(name);
00124 if (CF != 0)
00125 break;
00126 }
00127
00128 return CF;
00129 }
00130
00131 NodeCreationProxy *X3DTK::joinNodeCreationProxy(NodeCreationProxy *N0, NodeCreationProxy *N1)
00132 {
00133 NodeCreationProxy *N = new NodeCreationProxy();
00134
00135 for (list<X3DComponentCreator *>::const_iterator it = N0->componentList_.begin(); it != N0->componentList_.end(); ++it)
00136 N->setComponentCreator(*it);
00137
00138 for (list<X3DComponentCreator *>::const_iterator it = N1->componentList_.begin(); it != N1->componentList_.end(); ++it)
00139 N->setComponentCreator(*it);
00140
00141 return N;
00142 }