Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added files from objecthierarchy, changed includes

File size: 1.9 KB
Line 
1/*!
2    @file ClassFactory.h
3    @brief Definition and implementation of the ClassFactory class
4
5    The ClassFactory is able to create new objects of a specific class.
6*/
7
8#ifndef _ClassFactory_H__
9#define _ClassFactory_H__
10
11#include "Identifier.h"
12
13namespace orxonox
14{
15    // ###############################
16    // ###      ClassFactory       ###
17    // ###############################
18    //! The ClassFactory is able to create new objects of a specific class.
19    template <class T>
20    class ClassFactory : public BaseFactory
21    {
22        public:
23            static bool create(const std::string& name);
24            BaseObject* fabricate();
25
26        private:
27            ClassFactory() {}                               // Don't create
28            ClassFactory(const ClassFactory& factory) {}    // Don't copy
29            ~ClassFactory() {}                              // Don't delete
30
31            static T* createNewObject();
32    };
33
34    /**
35        @brief Adds the ClassFactory to the Identifier of the same type and the Identifier to the Factory.
36        @return Always true (this is needed because the compiler only allows assignments before main())
37    */
38    template <class T>
39    bool ClassFactory<T>::create(const std::string& name)
40    {
41        ClassIdentifier<T>::getIdentifier()->addFactory(new ClassFactory<T>);
42        Factory::add(name, ClassIdentifier<T>::getIdentifier());
43
44        return true;
45    }
46
47    /**
48        @brief Creates and returns a new object of class T.
49        @return The new object
50    */
51    template <class T>
52    BaseObject* ClassFactory<T>::fabricate()
53    {
54        return ClassFactory<T>::createNewObject();
55    }
56
57    /**
58        @brief Creates and returns a new object of class T; this is a wrapper for the new operator.
59        @return The new object
60    */
61    template <class T>
62    T* ClassFactory<T>::createNewObject()
63    {
64        return new T;
65    }
66}
67
68#endif
Note: See TracBrowser for help on using the repository browser.