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