Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/core/ClassManager.h @ 1268

Last change on this file since 1268 was 1062, checked in by rgrieder, 17 years ago
  • changed header file inclusion order
File size: 5.8 KB
Line 
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 ClassManager.h
31    @brief Definition and Implementation of the ClassManager template.
32
33    The ClassManager is a helper-class for ClassIdentifier. Because ClassIdentifiers must
34    be unique, they are created through the IdentifierDistributor-class to assure the
35    uniqueness of the ClassIdentifier. But accessing Identifiers through IdentifierDistributor
36    is slow, because it uses strings and a map. Thats why we use the ClassManager-template: It's
37    a singleton like ClassIdentifier, but it doesn't hurt if there are multiple instances in
38    different libraries, because they all store the same pointer to the unique ClassIdentifier
39    which they've retrieved through IdentifierDistributor.
40*/
41
42#ifndef _ClassManager_H__
43#define _ClassManager_H__
44
45#include "CorePrereqs.h"
46
47#include <string>
48
49#include "Identifier.h"
50#include "IdentifierDistributor.h"
51
52namespace orxonox
53{
54    //! ClassManager is a helper class to allow faster access on the ClassIdentifiers.
55    /**
56        Because accessing the IdentifierDistributor is slow, the ClassManager accesses it once
57        and stores the result in a member-variable. IdentifierDistributor assures the uniqueness
58        of the ClassIdentifier, even if there are multiple instances of the ClassManager-template
59        in different libraries.
60    */
61    template <class T>
62    class ClassManager
63    {
64        public:
65            static ClassManager<T>* getSingleton();
66            static ClassIdentifier<T>* getIdentifier();
67            static const std::string& getName();
68
69        private:
70            ClassManager();
71            ClassManager(const ClassIdentifier<T>& identifier) {}    // don't copy
72            ~ClassManager() {}                                       // don't delete
73
74            bool bInitialized_;                 //!< This is false until the ClassIdentifier gets assigned
75            ClassIdentifier<T>* identifier_;    //!< The unique ClassIdentifier for the class T
76    };
77
78    /**
79        @brief Constructor: Marks the ClassManager as uninitialized.
80    */
81    template <class T>
82    ClassManager<T>::ClassManager()
83    {
84        this->bInitialized_ = false;
85    }
86
87    /**
88        @brief Returns the one and only instance of this class for the template parameter T.
89        @return The instance
90    */
91    template <class T>
92    ClassManager<T>* ClassManager<T>::getSingleton()
93    {
94        static ClassManager<T> theOneAndOnlyInstance = ClassManager<T>();
95        return &theOneAndOnlyInstance;
96    }
97
98    /**
99        @brief Creates the only instance of this class for the template class T and retrieves a unique Identifier for the given name.
100        @return The unique Identifier
101    */
102    template <class T>
103    ClassIdentifier<T>* ClassManager<T>::getIdentifier()
104    {
105        // Check if the ClassManager is already initialized
106        if (!ClassManager<T>::getSingleton()->bInitialized_)
107        {
108            // Get the name of the class
109            std::string name = typeid(T).name();
110
111            // It's not -> retrieve the ClassIdentifier through IdentifierDistributor
112            COUT(4) << "*** ClassManager: Request Identifier Singleton for " << name << "." << std::endl;
113
114            // First create a ClassIdentifier in case there's no instance existing yet
115            ClassIdentifier<T>* temp = new ClassIdentifier<T>();
116
117            // Ask the IdentifierDistributor for the unique ClassIdentifier
118            ClassManager<T>::getSingleton()->identifier_ = (ClassIdentifier<T>*)IdentifierDistributor::getIdentifier(name, temp);
119
120            // If the retrieved Identifier differs from our proposal, we don't need the proposal any more
121            if (temp != ClassManager<T>::getSingleton()->identifier_)
122            {
123                COUT(4) << "*** ClassManager: Requested Identifier for " << name << " was already existing and got assigned." << std::endl;
124
125                // Delete the unnecessary proposal
126                delete temp;
127            }
128            else
129            {
130                COUT(4) << "*** ClassManager: Requested Identifier for " << name << " was not yet existing and got created." << std::endl;
131            }
132
133            ClassManager<T>::getSingleton()->bInitialized_ = true;
134        }
135
136        // Finally return the unique ClassIdentifier
137        return ClassManager<T>::getSingleton()->identifier_;
138    }
139
140    /**
141        @brief Returns the name of the class the ClassManager belongs to.
142        @return The name
143    */
144    template <class T>
145    const std::string& ClassManager<T>::getName()
146    {
147        static std::string unknownClassName = std::string("unknown");
148
149        if (ClassManager<T>::getSingleton()->bInitialized_)
150            return ClassManager<T>::getSingleton()->identifier_->getName();
151        else
152            return unknownClassName;
153    }
154}
155
156#endif /* _ClassManager_H__ */
Note: See TracBrowser for help on using the repository browser.