| 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 | /** |
|---|
| 30 | @file |
|---|
| 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 | |
|---|
| 43 | #include "util/Debug.h" |
|---|
| 44 | #include "Identifier.h" |
|---|
| 45 | |
|---|
| 46 | namespace orxonox |
|---|
| 47 | { |
|---|
| 48 | // ########################### |
|---|
| 49 | // ### Factory ### |
|---|
| 50 | // ########################### |
|---|
| 51 | //! Base-class of ClassFactory. |
|---|
| 52 | class _CoreExport Factory |
|---|
| 53 | { |
|---|
| 54 | public: |
|---|
| 55 | virtual ~Factory() {}; |
|---|
| 56 | virtual BaseObject* fabricate(BaseObject* creator) = 0; |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | // ############################### |
|---|
| 60 | // ### ClassFactory ### |
|---|
| 61 | // ############################### |
|---|
| 62 | //! The ClassFactory is able to create new objects of a specific class. |
|---|
| 63 | template <class T> |
|---|
| 64 | class ClassFactory : public Factory |
|---|
| 65 | { |
|---|
| 66 | public: |
|---|
| 67 | /** |
|---|
| 68 | @brief Constructor: Adds the ClassFactory to the Identifier of the same type. |
|---|
| 69 | @param name The name of the class |
|---|
| 70 | @param bLoadable True if the class can be loaded through XML |
|---|
| 71 | */ |
|---|
| 72 | ClassFactory(const std::string& name, bool bLoadable = true) |
|---|
| 73 | { |
|---|
| 74 | COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl; |
|---|
| 75 | ClassIdentifier<T>::getIdentifier(name)->addFactory(this); |
|---|
| 76 | ClassIdentifier<T>::getIdentifier()->setLoadable(bLoadable); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | @brief Creates and returns a new object of class T. |
|---|
| 81 | @return The new object |
|---|
| 82 | */ |
|---|
| 83 | inline BaseObject* fabricate(BaseObject* creator) |
|---|
| 84 | { |
|---|
| 85 | return static_cast<BaseObject*>(new T(creator)); |
|---|
| 86 | } |
|---|
| 87 | }; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | #endif /* _ClassFactory_H__ */ |
|---|