| 1 | /* | 
|---|
| 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 |  | 
|---|
| 16 | /*! | 
|---|
| 17 |  * @file factory.h | 
|---|
| 18 |  * @brief A loadable object handler | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | #ifndef _FACTORY_H | 
|---|
| 23 | #define _FACTORY_H | 
|---|
| 24 |  | 
|---|
| 25 | class BaseObject; | 
|---|
| 26 |  | 
|---|
| 27 | #include "tinyxml.h" | 
|---|
| 28 | #include "base_object.h" | 
|---|
| 29 | #include "debug.h" | 
|---|
| 30 |  | 
|---|
| 31 | /** | 
|---|
| 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) | 
|---|
| 34 | */ | 
|---|
| 35 | #define CREATE_FACTORY(CLASS_NAME, CLASS_ID) \ | 
|---|
| 36 |     tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID) | 
|---|
| 37 |  | 
|---|
| 38 | //! The Factory is a loadable object handler | 
|---|
| 39 | class Factory : public BaseObject { | 
|---|
| 40 |  | 
|---|
| 41 |  public: | 
|---|
| 42 |   Factory (const char* factoryName = NULL, ClassID classID = CL_NULL); | 
|---|
| 43 |   virtual ~Factory (); | 
|---|
| 44 |  | 
|---|
| 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; | 
|---|
| 49 |  | 
|---|
| 50 |   static void registerFactory( Factory* factory); | 
|---|
| 51 |   /** @returns the first factory */ | 
|---|
| 52 |   static Factory* getFirst() { return Factory::first; }; | 
|---|
| 53 |  | 
|---|
| 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 |  | 
|---|
| 60 |  | 
|---|
| 61 |   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. | 
|---|
| 67 | }; | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |  *  a factory that is able to load any kind of Object | 
|---|
| 71 |  * (this is a Functor) | 
|---|
| 72 |  */ | 
|---|
| 73 | template<class T> class tFactory : public Factory | 
|---|
| 74 | { | 
|---|
| 75 |   public: | 
|---|
| 76 |     tFactory(const char* factoryName, ClassID classID); | 
|---|
| 77 |     virtual ~tFactory(); | 
|---|
| 78 |  | 
|---|
| 79 |   private: | 
|---|
| 80 |     virtual BaseObject* fabricate(ClassID classID); | 
|---|
| 81 |     virtual BaseObject* fabricate(const TiXmlElement* root); | 
|---|
| 82 |     virtual BaseObject* fabricateDirect(); | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 | /** | 
|---|
| 86 |  *  construnts a factory with | 
|---|
| 87 |  * @param factoryName the name of the factory | 
|---|
| 88 | */ | 
|---|
| 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-Factory | 
|---|
| 97 |  */ | 
|---|
| 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 |   else | 
|---|
| 118 |     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 |   else | 
|---|
| 135 |     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 |  | 
|---|
| 147 | #endif /* _FACTORY_H */ | 
|---|
| 148 |  | 
|---|