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 "parser/tinyxml/tinyxml.h" |
---|
28 | #include "base_object.h" |
---|
29 | #include "debug.h" |
---|
30 | #include <vector> |
---|
31 | #include <list> |
---|
32 | |
---|
33 | /** |
---|
34 | * Creates a factory to a Loadable Class. |
---|
35 | * this should be used at the beginning of all the Classes that should be loadable (in the cc-file) |
---|
36 | */ |
---|
37 | #define CREATE_FACTORY(CLASS_NAME, CLASS_ID) \ |
---|
38 | tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID) |
---|
39 | |
---|
40 | // #define CREATE_DYNAMIC_FACTORY(CLASS_NAME, CLASS_ID) \ |
---|
41 | // tFactory<CLASS_NAME>* global_##CLASS_NAME##_Dynamic_Factory = new DynamicLoader<CLASS_NAME>(#CLASS_NAME, CLASS_ID) |
---|
42 | |
---|
43 | //! The Factory is a loadable object handler |
---|
44 | class Factory : public BaseObject { |
---|
45 | |
---|
46 | public: |
---|
47 | virtual ~Factory (); |
---|
48 | |
---|
49 | static void deleteFactories(); |
---|
50 | |
---|
51 | static BaseObject* fabricate(const char* className); |
---|
52 | static BaseObject* fabricate(ClassID classID); |
---|
53 | static BaseObject* fabricate(const TiXmlElement* root = NULL); |
---|
54 | |
---|
55 | |
---|
56 | bool operator==(ClassID classID) const; |
---|
57 | bool operator==(const char* className) const; |
---|
58 | |
---|
59 | protected: |
---|
60 | Factory (const char* factoryName, ClassID classID); |
---|
61 | virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0; |
---|
62 | |
---|
63 | protected: |
---|
64 | ClassID classID; //!< The Class-Identifyer of the Factory. |
---|
65 | const char* className; //!< The name of the Class. |
---|
66 | static std::list<Factory*>* factoryList; //!< List of Registered Factories |
---|
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 | /** |
---|
77 | * creates a new type Factory to enable the loading of T |
---|
78 | * @param factoryName the Name of the Factory to load. |
---|
79 | * @param classID the ID of the Class to be created. |
---|
80 | */ |
---|
81 | tFactory (const char* factoryName, ClassID classID) |
---|
82 | : Factory(factoryName, classID) |
---|
83 | { } |
---|
84 | |
---|
85 | private: |
---|
86 | /** |
---|
87 | * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*) |
---|
88 | * @param root the TiXmlElement T should load parameters from. |
---|
89 | * @return the newly fabricated T. |
---|
90 | */ |
---|
91 | virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const |
---|
92 | { |
---|
93 | return new T(root); |
---|
94 | } |
---|
95 | }; |
---|
96 | |
---|
97 | #endif /* _FACTORY_H */ |
---|
98 | |
---|