[790] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[871] | 28 | /** |
---|
[790] | 29 | @file ClassFactory.h |
---|
| 30 | @brief Definition and implementation of the ClassFactory class |
---|
| 31 | |
---|
| 32 | The ClassFactory is able to create new objects of a specific class. |
---|
| 33 | */ |
---|
| 34 | |
---|
[244] | 35 | #ifndef _ClassFactory_H__ |
---|
| 36 | #define _ClassFactory_H__ |
---|
| 37 | |
---|
[790] | 38 | #include <string> |
---|
| 39 | |
---|
| 40 | #include "CorePrereqs.h" |
---|
| 41 | |
---|
| 42 | #include "Factory.h" |
---|
[244] | 43 | #include "Identifier.h" |
---|
[790] | 44 | #include "Debug.h" |
---|
[244] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | // ############################### |
---|
| 49 | // ### ClassFactory ### |
---|
| 50 | // ############################### |
---|
[790] | 51 | //! The ClassFactory is able to create new objects of a specific class. |
---|
[244] | 52 | template <class T> |
---|
| 53 | class ClassFactory : public BaseFactory |
---|
| 54 | { |
---|
| 55 | public: |
---|
[790] | 56 | static bool create(const std::string& name); |
---|
[244] | 57 | BaseObject* fabricate(); |
---|
| 58 | |
---|
| 59 | private: |
---|
[790] | 60 | ClassFactory() {} // Don't create |
---|
| 61 | ClassFactory(const ClassFactory& factory) {} // Don't copy |
---|
| 62 | virtual ~ClassFactory() {} // Don't delete |
---|
[244] | 63 | |
---|
| 64 | static T* createNewObject(); |
---|
| 65 | }; |
---|
| 66 | |
---|
[790] | 67 | /** |
---|
| 68 | @brief Adds the ClassFactory to the Identifier of the same type and the Identifier to the Factory. |
---|
| 69 | @return Always true (this is needed because the compiler only allows assignments before main()) |
---|
| 70 | */ |
---|
[244] | 71 | template <class T> |
---|
[790] | 72 | bool ClassFactory<T>::create(const std::string& name) |
---|
[244] | 73 | { |
---|
[871] | 74 | COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl; |
---|
| 75 | ClassManager<T>::getIdentifier()->addFactory(new ClassFactory<T>); |
---|
| 76 | Factory::add(name, ClassManager<T>::getIdentifier()); |
---|
[244] | 77 | |
---|
| 78 | return true; |
---|
| 79 | } |
---|
| 80 | |
---|
[790] | 81 | /** |
---|
| 82 | @brief Creates and returns a new object of class T. |
---|
| 83 | @return The new object |
---|
| 84 | */ |
---|
[244] | 85 | template <class T> |
---|
| 86 | BaseObject* ClassFactory<T>::fabricate() |
---|
| 87 | { |
---|
| 88 | return ClassFactory<T>::createNewObject(); |
---|
| 89 | } |
---|
| 90 | |
---|
[790] | 91 | /** |
---|
| 92 | @brief Creates and returns a new object of class T; this is a wrapper for the new operator. |
---|
| 93 | @return The new object |
---|
| 94 | */ |
---|
[244] | 95 | template <class T> |
---|
| 96 | T* ClassFactory<T>::createNewObject() |
---|
| 97 | { |
---|
| 98 | return new T; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
[790] | 102 | #endif /* _ClassFactory_H__ */ |
---|