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