Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/core/ClassFactory.h @ 677

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

changed \n to std::endl

File size: 3.0 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 "Identifier.h"
39#include "Debug.h"
40
41namespace orxonox
42{
43    // ###############################
44    // ###      ClassFactory       ###
45    // ###############################
46    //! The ClassFactory is able to create new objects of a specific class.
47    template <class T>
48    class ClassFactory : public BaseFactory
49    {
50        public:
51            static bool create(const std::string& name);
52            BaseObject* fabricate();
53
54        private:
55            ClassFactory() {}                               // Don't create
56            ClassFactory(const ClassFactory& factory) {}    // Don't copy
57            ~ClassFactory() {}                              // Don't delete
58
59            static T* createNewObject();
60    };
61
62    /**
63        @brief Adds the ClassFactory to the Identifier of the same type and the Identifier to the Factory.
64        @return Always true (this is needed because the compiler only allows assignments before main())
65    */
66    template <class T>
67    bool ClassFactory<T>::create(const std::string& name)
68    {
69        COUT(4) << "*** Create entry for " << name << " in Factory." << std::endl;
70        ClassIdentifier<T>::getIdentifier()->addFactory(new ClassFactory<T>);
71        Factory::add(name, ClassIdentifier<T>::getIdentifier());
72
73        return true;
74    }
75
76    /**
77        @brief Creates and returns a new object of class T.
78        @return The new object
79    */
80    template <class T>
81    BaseObject* ClassFactory<T>::fabricate()
82    {
83        return ClassFactory<T>::createNewObject();
84    }
85
86    /**
87        @brief Creates and returns a new object of class T; this is a wrapper for the new operator.
88        @return The new object
89    */
90    template <class T>
91    T* ClassFactory<T>::createNewObject()
92    {
93        return new T;
94    }
95}
96
97#endif /* _ClassFactory_H__ */
Note: See TracBrowser for help on using the repository browser.