00001
00002
00004
00005 #ifndef X3DCOMPONENTCREATOR_H
00006 #define X3DCOMPONENTCREATOR_H
00007
00008 #include "X3DComponent.h"
00009 #include "X3D_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:
00033 template<class T>
00034 void defineNode()
00035 {
00036 defineCreationFunction<T, X3DComponentCreator>(&X3DComponentCreator::create<T>);
00037 };
00038
00039 private:
00040 template<class T>
00041 SFNode create() const
00042 {
00043 return new T();
00044 };
00045
00047 template<class T, class C> void defineCreationFunction(SFNode (C::*ptrF)() const)
00048 {
00049 SFType::beginFunctionDefinition();
00050 SFNode t = new T();
00051 SFString name = t->getTypeName();
00052 SFComponent *TComponent = t->getType()->getComponent();
00053
00054 if (TComponent != component)
00055 {
00056 SFString sg1, sg2;
00057 if (component->getSceneGraphName() != "")
00058 sg1 = " of the " + component->getSceneGraphName() + " scene graph";
00059 if (t->getType()->getSceneGraphName() != "")
00060 sg1 = " of the " + t->getType()->getSceneGraphName() + " scene graph";
00061
00062 cx3d << "warning: defineNode for " << name << " which doesn't belong to the " << component->getName() << " component" << sg1 << "," << std::endl;
00063 cx3d << " but to the " << t->getType()->getComponentName() << " component" << sg2 << std::endl;
00064 }
00065 else
00066 {
00067 CreationFunction *CF = new CreationFunction(reinterpret_cast<ptrToCreationFunction>(ptrF), this);
00068 _creationDict.insert(std::pair<SFString, CreationFunction *>(name, CF));
00069 }
00070 delete t;
00071 SFType::endFunctionDefinition();
00072 };
00073
00074 CreationDict _creationDict;
00075 };
00076
00077 }
00078 }
00079
00080 #endif