[4250] | 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 | |
---|
[3940] | 16 | /*! |
---|
[4250] | 17 | \file factory.h |
---|
| 18 | \brief philosophy stuff |
---|
[3940] | 19 | */ |
---|
| 20 | |
---|
[4250] | 21 | |
---|
[3940] | 22 | #ifndef _FACTORY_H |
---|
| 23 | #define _FACTORY_H |
---|
| 24 | |
---|
| 25 | class BaseObject; |
---|
| 26 | |
---|
[4239] | 27 | #include "tinyxml.h" |
---|
[4240] | 28 | #include "load_param.h" |
---|
[4171] | 29 | #include "debug.h" |
---|
[3940] | 30 | |
---|
[4004] | 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 | */ |
---|
[4254] | 35 | #define CREATE_FACTORY(CLASS_NAME) tFactory<CLASS_NAME>* global_##CLASS_NAME##Factory = new tFactory<CLASS_NAME>(#CLASS_NAME) |
---|
[3940] | 36 | //! The Factory is |
---|
| 37 | class Factory { |
---|
| 38 | |
---|
| 39 | public: |
---|
[4004] | 40 | Factory (const char* name = NULL); |
---|
[3940] | 41 | ~Factory (); |
---|
[4003] | 42 | |
---|
[3940] | 43 | |
---|
[4253] | 44 | virtual BaseObject* fabricate(const TiXmlElement* root); |
---|
[4003] | 45 | void initialize(); |
---|
| 46 | void registerFactory( Factory* factory); |
---|
[4020] | 47 | void setFactoryName(const char* name); |
---|
| 48 | const char* getFactoryName() {return factoryName;}; |
---|
[4003] | 49 | void setNext( Factory* factory) {next = factory;} |
---|
| 50 | Factory* getNext() {return next;} |
---|
[3940] | 51 | |
---|
| 52 | private: |
---|
[4020] | 53 | char* factoryName; |
---|
[3940] | 54 | |
---|
| 55 | Factory* next; |
---|
| 56 | }; |
---|
| 57 | |
---|
[4004] | 58 | template<class T> class tFactory : public Factory |
---|
| 59 | { |
---|
| 60 | public: |
---|
| 61 | tFactory(const char* name); |
---|
| 62 | virtual ~tFactory(); |
---|
| 63 | |
---|
| 64 | private: |
---|
[4253] | 65 | BaseObject* fabricate(const TiXmlElement* root); |
---|
[4004] | 66 | }; |
---|
| 67 | |
---|
| 68 | template<class T> |
---|
| 69 | tFactory<T>::tFactory(const char* name) : Factory(name) |
---|
| 70 | { |
---|
[4133] | 71 | PRINTF(5)("fileName: %s\n", name); |
---|
[4004] | 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | template<class T> |
---|
| 76 | tFactory<T>::~tFactory() |
---|
| 77 | {} |
---|
| 78 | |
---|
| 79 | template<class T> |
---|
[4253] | 80 | BaseObject* tFactory<T>::fabricate(const TiXmlElement* root) |
---|
[4004] | 81 | { |
---|
[4020] | 82 | if(!strcmp(root->Value(), getFactoryName())) |
---|
[4004] | 83 | return new T ( root); |
---|
| 84 | else if( getNext() != NULL) |
---|
| 85 | return getNext()->fabricate( root); |
---|
| 86 | else |
---|
| 87 | return NULL; |
---|
| 88 | } |
---|
| 89 | |
---|
[3940] | 90 | // helper function |
---|
| 91 | |
---|
[4239] | 92 | const char* grabParameter(const TiXmlElement* root, const char* name); |
---|
[3940] | 93 | |
---|
| 94 | #endif /* _FACTORY_H */ |
---|
| 95 | |
---|