Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/SubclassIdentifier.h @ 5814

Last change on this file since 5814 was 5814, checked in by scheusso, 15 years ago

build fixes

  • Property svn:eol-style set to native
File size: 7.1 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
31    @brief Definition of SubclassIdentifier.
32
33    SubclassIdentifier is a separated class, acting like an Identifier, but has a given class.
34    You can only assign Identifiers of exactly the given class or of a derivative to a SubclassIdentifier.
35*/
36
37#ifndef _SubclassIdentifier_H__
38#define _SubclassIdentifier_H__
39
40#include <cstdlib>
41
42#include "CorePrereqs.h"
43
44#include "util/Debug.h"
45#include "Identifier.h"
46
47namespace orxonox
48{
49    // ###############################
50    // ###   SubclassIdentifier    ###
51    // ###############################
52    //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites.
53    /**
54        You can only assign an Identifier that belongs to a class T (or derived) to a SubclassIdentifier<T>.
55        If you assign something else, the program aborts.
56        Because we know the minimum type, a dynamic_cast is done, which makes it easier to create a new object.
57    */
58    template <class T>
59    class SubclassIdentifier
60    {
61        public:
62            /**
63                @brief Constructor: Automaticaly assigns the Identifier of the given class.
64            */
65            SubclassIdentifier()
66            {
67                this->identifier_ = ClassIdentifier<T>::getIdentifier();
68            }
69
70            /**
71                @brief Constructor: Assigns the given Identifier.
72                @param identifier The Identifier
73            */
74            SubclassIdentifier(Identifier* identifier)
75            {
76                this->operator=(identifier);
77            }
78
79            /**
80                @brief Copyconstructor: Assigns the identifier of the other SubclassIdentifier.
81                @param identifier The other SublcassIdentifier
82            */
83            template <class O>
84            SubclassIdentifier(const SubclassIdentifier<O>& identifier)
85            {
86                this->operator=(identifier.getIdentifier());
87            }
88
89            /**
90                @brief Overloading of the = operator: assigns the identifier and checks its type.
91                @param identifier The Identifier to assign
92                @return The SubclassIdentifier itself
93            */
94            const SubclassIdentifier<T>& operator=(Identifier* identifier)
95            {
96                if (!identifier || !identifier->isA(ClassIdentifier<T>::getIdentifier()))
97                {
98                    COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
99                    if (identifier)
100                    {
101                        COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl;
102                        COUT(1) << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl;
103                    }
104                    else
105                    {
106                        COUT(1) << "Error: Can't assign NULL identifier" << std::endl;
107                    }
108                }
109                else
110                {
111                    this->identifier_ = identifier;
112                }
113                return *this;
114            }
115
116            /**
117                @brief Overloading of the = operator: assigns the identifier of the other SubclassIdentifier.
118                @param identifier The other SublcassIdentifier
119            */
120            template <class O>
121            const SubclassIdentifier<T>& operator=(const SubclassIdentifier<O>& identifier)
122            {
123                return this->operator=(identifier.getIdentifier());
124            }
125
126            /**
127                @brief Overloading of the * operator: returns the assigned identifier.
128            */
129            inline Identifier* operator*() const
130            {
131                return this->identifier_;
132            }
133
134            /**
135                @brief Overloading of the -> operator: returns the assigned identifier.
136            */
137            inline Identifier* operator->() const
138            {
139                return this->identifier_;
140            }
141
142            /**
143                @brief Returns the assigned identifier. This allows you to assign a SubclassIdentifier to a normal Identifier*.
144            */
145            inline operator Identifier*() const
146            {
147                return this->identifier_;
148            }
149
150            /**
151                @brief Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T.
152                @return The new object
153            */
154            T* fabricate(BaseObject* creator) const
155            {
156                BaseObject* newObject = this->identifier_->fabricate(creator);
157
158                // Check if the creation was successful
159                if (newObject)
160                {
161                    return orxonox_cast<T*>(newObject);
162                }
163                else
164                {
165                    // Something went terribly wrong
166                    if (this->identifier_)
167                    {
168                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
169                        COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl;
170                        COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl;
171                    }
172                    else
173                    {
174                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
175                        COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl;
176                    }
177
178                    COUT(1) << "Aborting..." << std::endl;
179                    abort();
180                    return 0;
181                }
182            }
183
184            /** @brief Returns the assigned identifier. @return The identifier */
185            inline Identifier* getIdentifier() const
186                { return this->identifier_; }
187
188        private:
189            Identifier* identifier_;            //!< The assigned identifier
190    };
191}
192
193#endif /* _SubclassIdentifier_H__ */
Note: See TracBrowser for help on using the repository browser.