| [4597] | 1 | /* | 
|---|
| [4250] | 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
 | 3 |  | 
|---|
 | 4 |    Copyright (C) 2004 orx | 
|---|
 | 5 |  | 
|---|
 | 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
 | 7 |    it under the terms of the GNU General Public License as published by | 
|---|
 | 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
 | 9 |    any later version. | 
|---|
 | 10 |  | 
|---|
 | 11 |    ### File Specific: | 
|---|
 | 12 |    main-programmer: Christian Meyer | 
|---|
 | 13 |    co-programmer: Benjamin Grauer | 
|---|
 | 14 | */ | 
|---|
 | 15 |  | 
|---|
| [4597] | 16 | /*! | 
|---|
| [4885] | 17 |  * @file factory.h | 
|---|
 | 18 |  * @brief A loadable object handler | 
|---|
| [3940] | 19 | */ | 
|---|
 | 20 |  | 
|---|
| [4250] | 21 |  | 
|---|
| [3940] | 22 | #ifndef _FACTORY_H | 
|---|
 | 23 | #define _FACTORY_H | 
|---|
 | 24 |  | 
|---|
 | 25 | class BaseObject; | 
|---|
 | 26 |  | 
|---|
| [4239] | 27 | #include "tinyxml.h" | 
|---|
| [4597] | 28 | #include "base_object.h" | 
|---|
| [4171] | 29 | #include "debug.h" | 
|---|
| [3940] | 30 |  | 
|---|
| [4597] | 31 | /** | 
|---|
| [4885] | 32 |  * Creates a factory to a Loadable Class. | 
|---|
 | 33 |  * this should be used at the beginning of all the Classes that should be loadable (in the cc-file) | 
|---|
| [4004] | 34 | */ | 
|---|
| [5750] | 35 | #define CREATE_FACTORY(CLASS_NAME, CLASS_ID) \ | 
|---|
 | 36 |     tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID) | 
|---|
| [4487] | 37 |  | 
|---|
 | 38 | //! The Factory is a loadable object handler | 
|---|
| [4597] | 39 | class Factory : public BaseObject { | 
|---|
| [3940] | 40 |  | 
|---|
 | 41 |  public: | 
|---|
| [5750] | 42 |   Factory (const char* factoryName = NULL, ClassID classID = CL_NULL); | 
|---|
| [5156] | 43 |   virtual ~Factory (); | 
|---|
| [3940] | 44 |  | 
|---|
| [5155] | 45 |   void fabricate(const char* className, const char* entityName); | 
|---|
| [5750] | 46 |   virtual BaseObject* fabricate(ClassID classID) = NULL; | 
|---|
| [4730] | 47 |   virtual BaseObject* fabricate(const TiXmlElement* root) = NULL; | 
|---|
| [5156] | 48 |   virtual BaseObject* fabricateDirect() = NULL; | 
|---|
| [4739] | 49 |  | 
|---|
 | 50 |   static void registerFactory( Factory* factory); | 
|---|
| [4836] | 51 |   /** @returns the first factory */ | 
|---|
| [4739] | 52 |   static Factory* getFirst() { return Factory::first; }; | 
|---|
| [4597] | 53 |  | 
|---|
| [4932] | 54 |   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 |  | 
|---|
| [5750] | 60 |  | 
|---|
 | 61 |   protected: | 
|---|
 | 62 |     ClassID           classID;              //!< The CLass-Identifyer of the Factory. | 
|---|
 | 63 |  | 
|---|
| [4739] | 64 |   private: | 
|---|
 | 65 |     Factory*          next;                 //!< pointer to the next factory. | 
|---|
 | 66 |     static Factory*   first;                //!< A pointer to the first factory. | 
|---|
| [3940] | 67 | }; | 
|---|
 | 68 |  | 
|---|
| [4487] | 69 | /** | 
|---|
| [4836] | 70 |  *  a factory that is able to load any kind of Object | 
|---|
| [5750] | 71 |  * (this is a Functor) | 
|---|
 | 72 |  */ | 
|---|
| [4004] | 73 | template<class T> class tFactory : public Factory | 
|---|
 | 74 | { | 
|---|
| [5750] | 75 |   public: | 
|---|
 | 76 |     tFactory(const char* factoryName, ClassID classID); | 
|---|
 | 77 |     virtual ~tFactory(); | 
|---|
| [4597] | 78 |  | 
|---|
| [4004] | 79 |   private: | 
|---|
| [5750] | 80 |     virtual BaseObject* fabricate(ClassID classID); | 
|---|
 | 81 |     virtual BaseObject* fabricate(const TiXmlElement* root); | 
|---|
 | 82 |     virtual BaseObject* fabricateDirect(); | 
|---|
| [4004] | 83 | }; | 
|---|
 | 84 |  | 
|---|
| [4487] | 85 | /** | 
|---|
| [4836] | 86 |  *  construnts a factory with | 
|---|
 | 87 |  * @param factoryName the name of the factory | 
|---|
| [4487] | 88 | */ | 
|---|
| [4004] | 89 | template<class T> | 
|---|
| [5750] | 90 |     tFactory<T>::tFactory(const char* factoryName, ClassID classID) : Factory(factoryName, classID) | 
|---|
| [4004] | 91 | { | 
|---|
| [5357] | 92 |   PRINTF(4)("Class: %s loadable\n", this->getName()); | 
|---|
| [4004] | 93 | } | 
|---|
 | 94 |  | 
|---|
| [5750] | 95 | /** | 
|---|
 | 96 |  * destructs the type-Factory | 
|---|
 | 97 |  */ | 
|---|
| [4004] | 98 | template<class T> | 
|---|
| [5750] | 99 |     tFactory<T>::~tFactory() | 
|---|
| [4004] | 100 | {} | 
|---|
 | 101 |  | 
|---|
| [5750] | 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 |  */ | 
|---|
| [4004] | 107 | template<class T> | 
|---|
| [5750] | 108 |     BaseObject* tFactory<T>::fabricate(const TiXmlElement* root) | 
|---|
| [4597] | 109 | { | 
|---|
| [5156] | 110 |   if (root == NULL) | 
|---|
 | 111 |     return NULL; | 
|---|
 | 112 |  | 
|---|
| [4730] | 113 |   if(!strcmp(root->Value(), this->getName())) | 
|---|
| [4004] | 114 |     return new T ( root); | 
|---|
| [4597] | 115 |   else if( getNext() != NULL) | 
|---|
| [4004] | 116 |     return getNext()->fabricate( root); | 
|---|
| [4597] | 117 |   else | 
|---|
| [4004] | 118 |     return NULL; | 
|---|
 | 119 | } | 
|---|
 | 120 |  | 
|---|
| [5750] | 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 |  */ | 
|---|
| [5156] | 127 | template<class T> | 
|---|
| [5750] | 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 |   else | 
|---|
 | 135 |     return NULL; | 
|---|
 | 136 | } | 
|---|
 | 137 |  | 
|---|
 | 138 | /** | 
|---|
 | 139 |  * directly fabricate an Entity of this factory. | 
|---|
 | 140 |  */ | 
|---|
 | 141 | template<class T> | 
|---|
| [5156] | 142 |     BaseObject* tFactory<T>::fabricateDirect() | 
|---|
 | 143 | { | 
|---|
 | 144 |   return new T((const TiXmlElement*)NULL); | 
|---|
 | 145 | } | 
|---|
 | 146 |  | 
|---|
| [3940] | 147 | #endif /* _FACTORY_H */ | 
|---|
 | 148 |  | 
|---|