| 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 philosophy stuff | 
|---|
| 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 "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) tFactory<CLASS_NAME>* global_##CLASS_NAME##Factory = new tFactory<CLASS_NAME>(#CLASS_NAME) | 
|---|
| 36 | //! The Factory is | 
|---|
| 37 | class Factory { | 
|---|
| 38 |  | 
|---|
| 39 | public: | 
|---|
| 40 | Factory (const char* name = NULL); | 
|---|
| 41 | ~Factory (); | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | virtual BaseObject* fabricate(const TiXmlElement* root); | 
|---|
| 45 | void initialize(); | 
|---|
| 46 | void registerFactory( Factory* factory); | 
|---|
| 47 | void setFactoryName(const char* name); | 
|---|
| 48 | const char* getFactoryName() {return factoryName;}; | 
|---|
| 49 | void setNext( Factory* factory) {next = factory;} | 
|---|
| 50 | Factory* getNext() {return next;} | 
|---|
| 51 |  | 
|---|
| 52 | private: | 
|---|
| 53 | char* factoryName; | 
|---|
| 54 |  | 
|---|
| 55 | Factory* next; | 
|---|
| 56 | }; | 
|---|
| 57 |  | 
|---|
| 58 | template<class T> class tFactory : public Factory | 
|---|
| 59 | { | 
|---|
| 60 | public: | 
|---|
| 61 | tFactory(const char* name); | 
|---|
| 62 | virtual ~tFactory(); | 
|---|
| 63 |  | 
|---|
| 64 | private: | 
|---|
| 65 | BaseObject* fabricate(const TiXmlElement* root); | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | template<class T> | 
|---|
| 69 | tFactory<T>::tFactory(const char* name) : Factory(name) | 
|---|
| 70 | { | 
|---|
| 71 | PRINTF(5)("fileName: %s\n", name); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | template<class T> | 
|---|
| 76 | tFactory<T>::~tFactory() | 
|---|
| 77 | {} | 
|---|
| 78 |  | 
|---|
| 79 | template<class T> | 
|---|
| 80 | BaseObject* tFactory<T>::fabricate(const TiXmlElement* root) | 
|---|
| 81 | { | 
|---|
| 82 | if(!strcmp(root->Value(), getFactoryName())) | 
|---|
| 83 | return new T ( root); | 
|---|
| 84 | else if( getNext() != NULL) | 
|---|
| 85 | return getNext()->fabricate( root); | 
|---|
| 86 | else | 
|---|
| 87 | return NULL; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | // helper function | 
|---|
| 91 |  | 
|---|
| 92 | const char* grabParameter(const TiXmlElement* root, const char* name); | 
|---|
| 93 |  | 
|---|
| 94 | #endif /* _FACTORY_H */ | 
|---|
| 95 |  | 
|---|