Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/SubclassIdentifier.h @ 6032

Last change on this file since 6032 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

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