[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 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 | |
---|
| 29 | /** |
---|
[2171] | 30 | @file |
---|
[1505] | 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 | |
---|
| 36 | #ifndef _ClassFactory_H__ |
---|
| 37 | #define _ClassFactory_H__ |
---|
| 38 | |
---|
| 39 | #include "CorePrereqs.h" |
---|
| 40 | |
---|
| 41 | #include <string> |
---|
| 42 | |
---|
[3196] | 43 | #include "util/Debug.h" |
---|
[1505] | 44 | #include "Factory.h" |
---|
| 45 | #include "Identifier.h" |
---|
| 46 | |
---|
| 47 | namespace orxonox |
---|
| 48 | { |
---|
| 49 | // ############################### |
---|
| 50 | // ### ClassFactory ### |
---|
| 51 | // ############################### |
---|
| 52 | //! The ClassFactory is able to create new objects of a specific class. |
---|
| 53 | template <class T> |
---|
| 54 | class ClassFactory : public BaseFactory |
---|
| 55 | { |
---|
[5610] | 56 | friend class Factory; |
---|
| 57 | |
---|
[1505] | 58 | public: |
---|
[2087] | 59 | static bool create(const std::string& name, bool bLoadable = true); |
---|
| 60 | BaseObject* fabricate(BaseObject* creator); |
---|
[1505] | 61 | |
---|
| 62 | private: |
---|
[5610] | 63 | ClassFactory(bool bLoadable) : bLoadable_(bLoadable) {} |
---|
| 64 | ClassFactory(const ClassFactory& factory); // Don't copy |
---|
[1505] | 65 | virtual ~ClassFactory() {} // Don't delete |
---|
| 66 | |
---|
[5610] | 67 | Identifier* createIdentifier(const std::string& name); |
---|
| 68 | |
---|
| 69 | bool bLoadable_; |
---|
[1505] | 70 | }; |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | @brief Adds the ClassFactory to the Identifier of the same type and the Identifier to the Factory. |
---|
[2087] | 74 | @param name The name of the class |
---|
| 75 | @param bLoadable True if the class can be loaded through XML |
---|
[1505] | 76 | @return Always true (this is needed because the compiler only allows assignments before main()) |
---|
| 77 | */ |
---|
| 78 | template <class T> |
---|
[2087] | 79 | bool ClassFactory<T>::create(const std::string& name, bool bLoadable) |
---|
[1505] | 80 | { |
---|
| 81 | COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl; |
---|
[5610] | 82 | Factory::add(name, new ClassFactory<T>(bLoadable)); |
---|
[1505] | 83 | |
---|
| 84 | return true; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | @brief Creates and returns a new object of class T. |
---|
| 89 | @return The new object |
---|
| 90 | */ |
---|
| 91 | template <class T> |
---|
[3196] | 92 | inline BaseObject* ClassFactory<T>::fabricate(BaseObject* creator) |
---|
[1505] | 93 | { |
---|
[5610] | 94 | return new T(creator); |
---|
[1505] | 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | */ |
---|
| 99 | template <class T> |
---|
[5610] | 100 | inline Identifier* ClassFactory<T>::createIdentifier(const std::string& name) |
---|
[1505] | 101 | { |
---|
[5610] | 102 | Identifier* identifier = ClassIdentifier<T>::getIdentifier(name); |
---|
| 103 | identifier->addFactory(this); |
---|
| 104 | identifier->setLoadable(this->bLoadable_); |
---|
| 105 | return identifier; |
---|
[1505] | 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | #endif /* _ClassFactory_H__ */ |
---|