1 | /*! |
---|
2 | @file ClassFactory.h |
---|
3 | @brief Definition and implementation of the ClassFactory class |
---|
4 | |
---|
5 | The ClassFactory is able to create new objects of a specific class. |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef _ClassFactory_H__ |
---|
9 | #define _ClassFactory_H__ |
---|
10 | |
---|
11 | #include "Identifier.h" |
---|
12 | #include "Debug.h" |
---|
13 | |
---|
14 | namespace orxonox |
---|
15 | { |
---|
16 | // ############################### |
---|
17 | // ### ClassFactory ### |
---|
18 | // ############################### |
---|
19 | //! The ClassFactory is able to create new objects of a specific class. |
---|
20 | template <class T> |
---|
21 | class ClassFactory : public BaseFactory |
---|
22 | { |
---|
23 | public: |
---|
24 | static bool create(const std::string& name); |
---|
25 | BaseObject* fabricate(); |
---|
26 | |
---|
27 | private: |
---|
28 | ClassFactory() {} // Don't create |
---|
29 | ClassFactory(const ClassFactory& factory) {} // Don't copy |
---|
30 | ~ClassFactory() {} // Don't delete |
---|
31 | |
---|
32 | static T* createNewObject(); |
---|
33 | }; |
---|
34 | |
---|
35 | /** |
---|
36 | @brief Adds the ClassFactory to the Identifier of the same type and the Identifier to the Factory. |
---|
37 | @return Always true (this is needed because the compiler only allows assignments before main()) |
---|
38 | */ |
---|
39 | template <class T> |
---|
40 | bool ClassFactory<T>::create(const std::string& name) |
---|
41 | { |
---|
42 | COUT(4) << "*** Create entry for " << name << " in Factory.\n"; |
---|
43 | ClassIdentifier<T>::getIdentifier()->addFactory(new ClassFactory<T>); |
---|
44 | Factory::add(name, ClassIdentifier<T>::getIdentifier()); |
---|
45 | |
---|
46 | return true; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | @brief Creates and returns a new object of class T. |
---|
51 | @return The new object |
---|
52 | */ |
---|
53 | template <class T> |
---|
54 | BaseObject* ClassFactory<T>::fabricate() |
---|
55 | { |
---|
56 | return ClassFactory<T>::createNewObject(); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | @brief Creates and returns a new object of class T; this is a wrapper for the new operator. |
---|
61 | @return The new object |
---|
62 | */ |
---|
63 | template <class T> |
---|
64 | T* ClassFactory<T>::createNewObject() |
---|
65 | { |
---|
66 | return new T; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | #endif |
---|