Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5776 was 5776, checked in by landauf, 15 years ago

moved SubclassIdentifier to a separate file

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