Orxonox  0.0.5 Codename: Arcturus
SubclassIdentifier.h
Go to the documentation of this file.
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 
65 #ifndef _SubclassIdentifier_H__
66 #define _SubclassIdentifier_H__
67 
68 #include "core/CorePrereqs.h"
69 
70 #include <cstdlib>
71 #include "util/Output.h"
72 #include "Identifier.h"
73 
74 namespace orxonox
75 {
76  // ###############################
77  // ### SubclassIdentifier ###
78  // ###############################
89  template <class T>
91  {
92  public:
95  {
97  }
98 
100  SubclassIdentifier(Identifier* identifier) : identifier_(nullptr)
101  {
102  this->operator=(identifier);
103  }
104 
106  template <class O>
108  {
109  this->operator=(identifier.getIdentifier());
110  }
111 
118  {
119  if (!identifier || !identifier->isA(ClassIdentifier<T>::getIdentifier()))
120  {
121  orxout(internal_error) << "An error occurred in SubclassIdentifier (Identifier.h):" << endl;
122  if (identifier)
123  {
124  orxout(internal_error) << "Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << '!' << endl;
125  orxout(internal_error) << "SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << endl;
126  }
127  else
128  {
129  orxout(internal_error) << "Can't assign nullptr identifier" << endl;
130  }
131  }
132  else
133  {
134  this->identifier_ = identifier;
135  }
136  return *this;
137  }
138 
140  template <class O>
142  {
143  return this->operator=(identifier.getIdentifier());
144  }
145 
147  inline Identifier* operator*() const
148  {
149  return this->identifier_;
150  }
151 
153  inline Identifier* operator->() const
154  {
155  return this->identifier_;
156  }
157 
159  inline operator Identifier*() const
160  {
161  return this->identifier_;
162  }
163 
165  T* fabricate(Context* context) const
166  {
167  Identifiable* newObject = this->identifier_->fabricate(context);
168 
169  // Check if the creation was successful
170  if (newObject)
171  {
172  return orxonox_cast<T*>(newObject);
173  }
174  else
175  {
176  // Something went terribly wrong
177  if (this->identifier_)
178  {
179  orxout(user_error) << "An error occurred in SubclassIdentifier (Identifier.h):" << endl;
180  orxout(user_error) << "Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << '!' << endl;
181  orxout(user_error) << "Couldn't fabricate a new Object." << endl;
182  }
183  else
184  {
185  orxout(user_error) << "An error occurred in SubclassIdentifier (Identifier.h):" << endl;
186  orxout(user_error) << "Couldn't fabricate a new Object - Identifier is undefined." << endl;
187  }
188 
189  orxout(user_error) << "Aborting..." << endl;
190  abort();
191  return nullptr;
192  }
193  }
194 
196  inline Identifier* getIdentifier() const
197  { return this->identifier_; }
198 
199  private:
201  };
202 }
203 
204 #endif /* _SubclassIdentifier_H__ */
Identifier * getIdentifier() const
Returns the assigned identifier.
Definition: SubclassIdentifier.h:196
The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables t...
Definition: Identifier.h:262
Shared library macros, enums, constants and forward declarations for the core library ...
SubclassIdentifier(Identifier *identifier)
Constructor: Assigns the given Identifier.
Definition: SubclassIdentifier.h:100
Identifier * operator->() const
Overloading of the -> operator: returns the assigned identifier.
Definition: SubclassIdentifier.h:153
Identifiable is needed to create the class-hierarchy at startup and to store the Identifier.
Definition: Identifiable.h:50
Output level, used for error messages which are important for developers.
Definition: OutputDefinitions.h:95
Declaration of Identifier, definition of ClassIdentifier<T>; used to identify the class of an object...
static ClassIdentifier< T > * getIdentifier()
Returns the only instance of this class.
Definition: Identifier.h:329
Output level, used for error messages which are important for the user.
Definition: OutputDefinitions.h:91
Identifier * operator*() const
Overloading of the * operator: returns the assigned identifier.
Definition: SubclassIdentifier.h:147
Identifier * identifier_
The assigned identifier.
Definition: SubclassIdentifier.h:200
OutputStream & orxout(OutputLevel level=level::debug_output, const OutputContextContainer &context=context::undefined())
This helper function returns a reference to a commonly used instance of OutputStream.
Definition: Output.h:81
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
bool isA(const Identifier *identifier) const
Returns true, if the Identifier is at least of the given type.
Definition: Identifier.cc:316
SubclassIdentifier< T > & operator=(Identifier *identifier)
Overloading of the = operator: assigns the identifier and checks its type.
Definition: SubclassIdentifier.h:117
The Identifier is used to identify the class of an object and to store information about the class...
Definition: Identifier.h:109
Defines the helper function orxout() and includes all necessary headers to use the output system...
ORX_FORCEINLINE T orxonox_cast(U *source)
Casts on object of type Identifiable to any derived type that is registered in the class hierarchy...
Definition: Identifier.h:485
Definition: InputPrereqs.h:78
Definition: Context.h:45
Identifiable * fabricate(Context *context)
Creates an object of the type the Identifier belongs to.
Definition: Identifier.cc:107
T * fabricate(Context *context) const
Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type ...
Definition: SubclassIdentifier.h:165
SubclassIdentifier< T > & operator=(const SubclassIdentifier< O > &identifier)
Overloading of the = operator: assigns the identifier of another SubclassIdentifier.
Definition: SubclassIdentifier.h:141
SubclassIdentifier(const SubclassIdentifier< O > &identifier)
Copyconstructor: Assigns the identifier of another SubclassIdentifier.
Definition: SubclassIdentifier.h:107
const std::string & getName() const
Returns the name of the class the Identifier belongs to.
Definition: Identifier.h:127
The SubclassIdentifier acts almost like an Identifier, but has some prerequisites.
Definition: SubclassIdentifier.h:90
SubclassIdentifier()
Constructor: Automaticaly assigns the Identifier of the given class.
Definition: SubclassIdentifier.h:94