Changeset 5982 in orxonox.OLD for trunk/src/util/loading/factory.h
- Timestamp:
- Dec 8, 2005, 12:22:53 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/util/loading/factory.h
r5944 r5982 28 28 #include "base_object.h" 29 29 #include "debug.h" 30 #include <vector> 30 31 31 32 /** … … 40 41 41 42 public: 42 Factory (const char* factoryName = NULL, ClassID classID = CL_NULL);43 Factory (const char* factoryName, ClassID classID); 43 44 virtual ~Factory (); 44 45 45 void fabricate(const char* className, const char* entityName); 46 virtual BaseObject* fabricate(ClassID classID) = NULL; 47 virtual BaseObject* fabricate(const TiXmlElement* root) = NULL; 48 virtual BaseObject* fabricateDirect() = NULL; 46 static void deleteFactories(); 49 47 50 static void registerFactory( Factory* factory); 51 /** @returns the first factory */ 52 static Factory* getFirst() { return Factory::first; }; 48 static BaseObject* fabricate(const char* className); 49 static BaseObject* fabricate(ClassID classID); 50 static BaseObject* fabricate(const TiXmlElement* root = NULL); 51 52 bool operator==(ClassID classID) const { return (this->classID == classID); }; 53 bool operator==(const char* className) const; 53 54 54 55 protected: 55 /** sets the Next factory in the list @param nextFactory the next factory */ 56 inline void setNext( Factory* nextFactory) { this->next = nextFactory; }; 57 /** @returns the next factory */ 58 Factory* getNext() const { return this->next; }; 59 56 virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0; 60 57 61 58 protected: 62 ClassID classID; //!< The CLass-Identifyer of the Factory. 63 64 private: 65 Factory* next; //!< pointer to the next factory. 66 static Factory* first; //!< A pointer to the first factory. 59 ClassID classID; //!< The Class-Identifyer of the Factory. 60 const char* className; //!< The name of the Class. 61 static std::list<Factory*>* factoryList; //!< List of Registered Factories 67 62 }; 68 63 … … 73 68 template<class T> class tFactory : public Factory 74 69 { 75 public: 76 tFactory(const char* factoryName, ClassID classID); 77 virtual ~tFactory(); 70 public: 71 tFactory (const char* factoryName, ClassID classID) 72 : Factory(factoryName, classID) 73 { 74 } 78 75 79 76 private: 80 virtual BaseObject* fabricate(ClassID classID); 81 virtual BaseObject* fabricate(const TiXmlElement* root); 82 virtual BaseObject* fabricateDirect(); 77 /** 78 * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*) 79 * @param root the TiXmlElement T should load parameters from. 80 * @return the newly fabricated T. 81 */ 82 virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const 83 { 84 return new T(root); 85 } 83 86 }; 84 85 /**86 * construnts a factory with87 * @param factoryName the name of the factory88 */89 template<class T>90 tFactory<T>::tFactory(const char* factoryName, ClassID classID) : Factory(factoryName, classID)91 {92 PRINTF(4)("Class: %s loadable\n", this->getName());93 }94 95 /**96 * destructs the type-Factory97 */98 template<class T>99 tFactory<T>::~tFactory()100 {}101 102 /**103 * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)104 * @param root the TiXmlElement T should load parameters from.105 * @return the newly fabricated T, NULL otherwise.106 */107 template<class T>108 BaseObject* tFactory<T>::fabricate(const TiXmlElement* root)109 {110 if (root == NULL)111 return NULL;112 113 if(!strcmp(root->Value(), this->getName()))114 return new T ( root);115 else if( getNext() != NULL)116 return getNext()->fabricate( root);117 else118 return NULL;119 }120 121 122 /**123 * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)124 * @param classID the ClassID of T that should be created.125 * @return the newly fabricated T if fabricated NULL otherwise.126 */127 template<class T>128 BaseObject* tFactory<T>::fabricate(ClassID classID)129 {130 if(classID == this->classID)131 return this->fabricateDirect();132 else if( getNext() != NULL)133 return getNext()->fabricate( classID);134 else135 return NULL;136 }137 138 /**139 * directly fabricate an Entity of this factory.140 */141 template<class T>142 BaseObject* tFactory<T>::fabricateDirect()143 {144 return new T((const TiXmlElement*)NULL);145 }146 87 147 88 #endif /* _FACTORY_H */
Note: See TracChangeset
for help on using the changeset viewer.