Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/core/ClassFactory.h @ 859

Last change on this file since 859 was 859, checked in by landauf, 16 years ago

more or less a copy of the trunk

File size: 3.1 KB
Line 
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
28/*!
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
35#ifndef _ClassFactory_H__
36#define _ClassFactory_H__
37
38#include <string>
39
40#include "CorePrereqs.h"
41
42#include "Factory.h"
43#include "Identifier.h"
44#include "Debug.h"
45
46namespace orxonox
47{
48    // ###############################
49    // ###      ClassFactory       ###
50    // ###############################
51    //! The ClassFactory is able to create new objects of a specific class.
52    template <class T>
53    class ClassFactory : public BaseFactory
54    {
55        public:
56            static bool create(const std::string& name);
57            BaseObject* fabricate();
58
59        private:
60            ClassFactory() {}                               // Don't create
61            ClassFactory(const ClassFactory& factory) {}    // Don't copy
62            virtual ~ClassFactory() {}                      // Don't delete
63
64            static T* createNewObject();
65    };
66
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    */
71    template <class T>
72    bool ClassFactory<T>::create(const std::string& name)
73    {
74        COUT(4) << "*** Create entry for " << name << " in Factory." << std::endl;
75        ClassIdentifier<T>::getIdentifier()->addFactory(new ClassFactory<T>);
76        Factory::add(name, ClassIdentifier<T>::getIdentifier());
77
78        return true;
79    }
80
81    /**
82        @brief Creates and returns a new object of class T.
83        @return The new object
84    */
85    template <class T>
86    BaseObject* ClassFactory<T>::fabricate()
87    {
88        return ClassFactory<T>::createNewObject();
89    }
90
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    */
95    template <class T>
96    T* ClassFactory<T>::createNewObject()
97    {
98        return new T;
99    }
100}
101
102#endif /* _ClassFactory_H__ */
Note: See TracBrowser for help on using the repository browser.