Orxonox  0.0.5 Codename: Arcturus
Exception.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  * Reto Grieder
24  * Co-authors:
25  * ...
26  *
27  */
28 
57 #ifndef _Exception_H__
58 #define _Exception_H__
59 
60 #include "UtilPrereqs.h"
61 
62 #include <exception>
63 #include <sstream>
64 #include <string>
65 #include "Output.h"
66 
67 namespace orxonox
68 {
75  class _UtilExport Exception : public std::exception
76  {
77  public:
90  Exception(const std::string& description, unsigned int lineNumber,
91  const char* filename, const char* functionName);
93  Exception(const std::string& description);
94 
96  virtual ~Exception() throw() { }
98  const char* what() const throw();
99 
106  virtual const std::string& getFullDescription() const;
108  virtual std::string getTypeName() const = 0;
110  virtual const std::string& getDescription() const { return this->description_; }
112  virtual unsigned int getLineNumber() const { return this->lineNumber_; }
114  virtual const std::string& getFunctionName() const { return this->functionName_; }
116  virtual const std::string& getFilename() const { return this->filename_; }
117 
125  static std::string handleMessage();
126 
127  protected:
129  unsigned int lineNumber_;
132  // mutable because "what()" is a const method
134  };
135 
137 #define CREATE_ORXONOX_EXCEPTION(ExceptionName) \
138  class ExceptionName##Exception : public Exception \
139  { \
140  public: \
141  ExceptionName##Exception(const std::string& description, \
142  unsigned int lineNumber, const char* filename, \
143  const char* functionName) \
144  : Exception(description, lineNumber, filename, functionName) \
145  { } \
146  \
147  ExceptionName##Exception(const std::string& description) \
148  : Exception(description) \
149  { } \
150  \
151  ~ExceptionName##Exception() throw() { } \
152  \
153  std::string getTypeName() const { return #ExceptionName; } \
154  }
155 
156  // Creates all possible exception types.
157  // If you want to add a new type, simply copy and adjust a new line here.
158  CREATE_ORXONOX_EXCEPTION(General);
159  CREATE_ORXONOX_EXCEPTION(FileNotFound);
160  CREATE_ORXONOX_EXCEPTION(Argument);
161  CREATE_ORXONOX_EXCEPTION(PhysicsViolation);
162  CREATE_ORXONOX_EXCEPTION(ParseError);
163  CREATE_ORXONOX_EXCEPTION(PluginsNotFound);
164  CREATE_ORXONOX_EXCEPTION(InitialisationFailed);
165  CREATE_ORXONOX_EXCEPTION(InitialisationAborted);
166  CREATE_ORXONOX_EXCEPTION(NotImplemented);
167  CREATE_ORXONOX_EXCEPTION(GameState);
168  CREATE_ORXONOX_EXCEPTION(NoGraphics);
169  CREATE_ORXONOX_EXCEPTION(AbortLoading);
170 
175  template <class T>
176  inline const T& exceptionThrowerHelper(const T& exception)
177  {
178  // let the catcher decide whether to display the message also to the user
179  orxout(internal_error) << exception.getFullDescription() << endl;
180  return exception;
181  }
182 
190 #define ThrowException(type, description) \
191  throw orxonox::exceptionThrowerHelper(type##Exception(static_cast<std::ostringstream&>(std::ostringstream().flush() << description).str(), __LINE__, __FILE__, __FUNCTIONNAME__))
192 
193 } /* namespace orxonox */
194 
195 #endif /* _Exception_H__ */
unsigned int lineNumber_
Line on which the exception occurred.
Definition: Exception.h:129
#define _UtilExport
Definition: UtilPrereqs.h:60
std::string filename_
File where the exception occurred.
Definition: Exception.h:131
const T & exceptionThrowerHelper(const T &exception)
Helper function that forwards an exception and displays the message.
Definition: Exception.h:176
std::string description_
User typed text about why the exception occurred.
Definition: Exception.h:128
::std::string string
Definition: gtest-port.h:756
virtual const std::string & getFunctionName() const
Returns the function in which the exception occurred.
Definition: Exception.h:114
Base class for all exceptions (derived from std::exception).
Definition: Exception.h:75
Output level, used for error messages which are important for developers.
Definition: OutputDefinitions.h:95
virtual unsigned int getLineNumber() const
Returns the line number on which the exception occurred.
Definition: Exception.h:112
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
CREATE_ORXONOX_EXCEPTION(General)
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Shared library macros, enums, constants and forward declarations for the util library ...
Defines the helper function orxout() and includes all necessary headers to use the output system...
Definition: InputPrereqs.h:78
std::string fullDescription_
Full description with line, file and function.
Definition: Exception.h:133
std::string functionName_
Function (including namespace and class) where the exception occurred.
Definition: Exception.h:130
virtual ~Exception()
Needed for compatibility with std::exception.
Definition: Exception.h:96
virtual const std::string & getDescription() const
Returns the short developer written exception.
Definition: Exception.h:110
virtual const std::string & getFilename() const
Returns the filename in which the exception occurred.
Definition: Exception.h:116