00001 00002 // X3DComponentCreator.h // 00004 00005 #ifndef X3DCOMPONENTCREATOR_H 00006 #define X3DCOMPONENTCREATOR_H 00007 00008 #include "X3DComponent.h" 00009 #include "CreationFunction.h" 00010 00011 namespace X3DTK { 00012 namespace X3D { 00013 00015 00016 class X3DComponentCreator : public X3DComponent 00017 { 00018 public: 00020 X3DComponentCreator(); 00022 virtual ~X3DComponentCreator() = 0; 00023 00025 CreationFunction *getCreationFunctionOf(const SFString &name) const; 00027 inline CreationDict getCreationDict() const {return _creationDict;}; 00028 00030 bool contains(const SFString &name) const; 00031 00032 protected: 00034 template<class T> void defineNewNode() 00035 { 00036 Type::beginFunctionDefinition(); 00037 00038 SFNode t = create<T>(); 00039 SFString name = t->getTypeName(); 00040 Component *TComponent = t->getType()->getComponent(); 00041 //checking if the node belongs to the component. 00042 if (TComponent != component) 00043 { 00044 SFString sg1, sg2; 00045 if (component->getSceneGraphName() != "") 00046 sg1 = " of the " + component->getSceneGraphName() + " scene graph"; 00047 if (t->getType()->getSceneGraphName() != "") 00048 sg1 = " of the " + t->getType()->getSceneGraphName() + " scene graph"; 00049 00050 std::cerr << "warning: defineNewNode for " << name << " which doesn't belong to the " << component->getName() << " component" << sg1 << "," << std::endl; 00051 std::cerr << " but to the " << t->getType()->getComponentName() << " component" << sg2 << std::endl; 00052 } 00053 else 00054 defineNewCreationFunction(t->getTypeName(), &X3DComponentCreator::create<T>); 00055 00056 delete t; 00057 Type::endFunctionDefinition(); 00058 }; 00059 00060 private: 00061 CreationDict _creationDict; 00062 00063 template<class T> 00064 SFNode create() const {return new T();}; 00065 00067 template<class C> void defineNewCreationFunction(const SFString &name, X3D::SFNode (C::*ptrF)() const) 00068 { 00069 CreationFunction *CF = new CreationFunction(reinterpret_cast<ptrToCreationFunction>(ptrF), this); 00070 _creationDict.insert(std::pair<SFString, CreationFunction *>(name, CF)); 00071 }; 00072 }; 00073 00074 } 00075 } 00076 00077 #endif