| 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 "load_param.h" | 
|---|
| 29 | #include "base_object.h" | 
|---|
| 30 | #include "debug.h" | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 | Creates a factory to a Loadable Class. | 
|---|
| 34 | this should be used at the beginning of all the Classes that should be loadable (in the cc-file) | 
|---|
| 35 | \todo make factoryName a BaseObject-parameter. (else it would be redundant) | 
|---|
| 36 | */ | 
|---|
| 37 | #define CREATE_FACTORY(CLASS_NAME) tFactory<CLASS_NAME>* global_##CLASS_NAME##Factory = new tFactory<CLASS_NAME>(#CLASS_NAME) | 
|---|
| 38 |  | 
|---|
| 39 | //! The Factory is a loadable object handler | 
|---|
| 40 | class Factory : public BaseObject { | 
|---|
| 41 |  | 
|---|
| 42 | public: | 
|---|
| 43 | Factory (const char* factoryName = NULL); | 
|---|
| 44 | ~Factory (); | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | virtual BaseObject* fabricate(const TiXmlElement* root); | 
|---|
| 48 | void initialize(); | 
|---|
| 49 | void registerFactory( Factory* factory); | 
|---|
| 50 | void setFactoryName(const char* factoryName); | 
|---|
| 51 | /** \returns the name of the factory */ | 
|---|
| 52 | const char* getFactoryName() { return this->factoryName; }; | 
|---|
| 53 | /** \brief sets the Next factory in the list \param nextFactory the next factory */ | 
|---|
| 54 | inline void setNext( Factory* nextFactory) { this->next = nextFactory; }; | 
|---|
| 55 | /** \returns the next factory */ | 
|---|
| 56 | Factory* getNext(void) const { return this->next; }; | 
|---|
| 57 |  | 
|---|
| 58 | protected: | 
|---|
| 59 | char*         factoryName;          //!< the name of the factory | 
|---|
| 60 | private: | 
|---|
| 61 | Factory*      next;                 //!< pointer to the next factory. | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | /** | 
|---|
| 65 | \brief a factory that is able to load any kind of Object | 
|---|
| 66 | (this is a Functor) | 
|---|
| 67 | */ | 
|---|
| 68 | template<class T> class tFactory : public Factory | 
|---|
| 69 | { | 
|---|
| 70 | public: | 
|---|
| 71 | tFactory(const char* factoryName); | 
|---|
| 72 | virtual ~tFactory(); | 
|---|
| 73 |  | 
|---|
| 74 | private: | 
|---|
| 75 | BaseObject* fabricate(const TiXmlElement* root); | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 | \brief construnts a factory with | 
|---|
| 80 | \param factoryName the name of the factory | 
|---|
| 81 | */ | 
|---|
| 82 | template<class T> | 
|---|
| 83 | tFactory<T>::tFactory(const char* factoryName) : Factory(factoryName) | 
|---|
| 84 | { | 
|---|
| 85 | PRINTF(5)("fileName: %s loadable\n", this->factoryName); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | template<class T> | 
|---|
| 90 | tFactory<T>::~tFactory() | 
|---|
| 91 | {} | 
|---|
| 92 |  | 
|---|
| 93 | template<class T> | 
|---|
| 94 | BaseObject* tFactory<T>::fabricate(const TiXmlElement* root) | 
|---|
| 95 | { | 
|---|
| 96 | if(!strcmp(root->Value(), getFactoryName())) | 
|---|
| 97 | return new T ( root); | 
|---|
| 98 | else if( getNext() != NULL) | 
|---|
| 99 | return getNext()->fabricate( root); | 
|---|
| 100 | else | 
|---|
| 101 | return NULL; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | #endif /* _FACTORY_H */ | 
|---|
| 105 |  | 
|---|