Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/util/Exception.h @ 7367

Last change on this file since 7367 was 7367, checked in by rgrieder, 14 years ago

Added Doxygen documentation for ExprParser, MathConvert, OrxEnum, OrxAssert and TriBool.
Adjusted Doxygen documentation for Clock and Exception.

  • Property svn:eol-style set to native
File size: 8.3 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 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
31@brief
32    Declaration of facilities to handle exceptions.
33@details
34    Any exception thrown should inherit from orxonox::Exception. There is a macro
35    \ref CREATE_ORXONOX_EXCEPTION to create a new exception (you can find a list of
36    available exceptions in the docs of this file). <br>
37    Throwing exception is also very simple:
38    @code
39        ThrowException(General, "Something went wrong");
40    @endcode
41    (\c General is a type of exception, see docs of this file) <br>
42    The exception will automatically contain information about file, line number
43    and function name it occurred in. <br><br>
44    There is also some magic you can do for numbers, etc.:
45    @code
46        ThrowException(General, "Error code: " << myInt << ". more info...");
47    @endcode
48    It works with an std::ostringstream, so you can feed it all sorts of values.
49*/
50
51#ifndef _Exception_H__
52#define _Exception_H__
53
54#include "UtilPrereqs.h"
55
56#include <exception>
57#include <sstream>
58#include <string>
59#include "Debug.h"
60
61namespace orxonox
62{
63    /** Base class for all exceptions (derived from std::exception).
64    @details
65        This class provides support for information about the file, the line
66        and the function the error occurred.
67    @see Exception.h
68    */
69    class _UtilExport Exception : public std::exception
70    {
71    public:
72        /**
73        @brief
74            Creates the exception but doesn't yet compose the full descrption (because of the virtual functions)
75        @param description
76            Exception description as string. This message is supposed to help developers!
77        @param lineNumber
78            The number of the code-line in which the exception occurred
79        @param filename
80            The file in which the exception occurred
81        @param functionName
82            The function in which the exception occurred
83        */
84        Exception(const std::string& description, unsigned int lineNumber,
85                  const char* filename, const char* functionName);
86        //! Simplified constructor with just a description. If you need more, use the other one.
87        Exception(const std::string& description);
88
89        //! Needed for compatibility with std::exception
90        virtual ~Exception() throw() { }
91        //! Returns the error description
92        const char* what() const throw();
93
94        /** Returns a full description with type, line, file and function
95        @remarks
96            The composed full description gets stored to fullDescription_. But for compliance
97            with std::exception, this method has to be const. Hence fullDescription_ is declared
98            as mutable.
99        */
100        virtual const std::string& getFullDescription() const;
101        //! Returns the string name of the exception type
102        virtual std::string        getTypeName()        const = 0;
103        //! Returns the short developer written exception
104        virtual const std::string& getDescription()     const { return this->description_; }
105        //! Returns the line number on which the exception occurred.
106        virtual const unsigned int getLineNumber()      const { return this->lineNumber_; }
107        //! Returns the function in which the exception occurred.
108        virtual const std::string& getFunctionName()    const { return this->functionName_; }
109        //! Returns the filename in which the exception occurred.
110        virtual const std::string& getFilename()        const { return this->filename_; }
111
112        /**
113        @brief
114            Retrieves information from an exception caught with "..."
115            Works for std::exception and CEGUI::Exception
116        @remarks
117            Never ever call this function without an exception in the stack!
118        */
119        static std::string handleMessage();
120
121    protected:
122        std::string description_;             //!< User typed text about why the exception occurred
123        unsigned int lineNumber_;             //!< Line on which the exception occurred
124        std::string functionName_;            //!< Function (including namespace and class) where the exception occurred
125        std::string filename_;                //!< File where the exception occurred
126        // mutable because "what()" is a const method
127        mutable std::string fullDescription_; //!< Full description with line, file and function
128    };
129
130//! Creates a new type of exception that inherits from orxonox::Exception
131#define CREATE_ORXONOX_EXCEPTION(ExceptionName)                                     \
132    class ExceptionName##Exception : public Exception                               \
133    {                                                                               \
134    public:                                                                         \
135        ExceptionName##Exception(const std::string& description,                    \
136                unsigned int lineNumber, const char* filename,                      \
137                const char* functionName)                                           \
138            : Exception(description, lineNumber, filename, functionName)            \
139        { }                                                                         \
140                                                                                    \
141        ExceptionName##Exception(const std::string& description)                    \
142            : Exception(description)                                                \
143        { }                                                                         \
144                                                                                    \
145        ~ExceptionName##Exception() throw() { }                                     \
146                                                                                    \
147        std::string getTypeName() const { return #ExceptionName; }                  \
148    }
149
150    // Creates all possible exception types.
151    // If you want to add a new type, simply copy and adjust a new line here.
152    CREATE_ORXONOX_EXCEPTION(General);
153    CREATE_ORXONOX_EXCEPTION(FileNotFound);
154    CREATE_ORXONOX_EXCEPTION(Argument);
155    CREATE_ORXONOX_EXCEPTION(PhysicsViolation);
156    CREATE_ORXONOX_EXCEPTION(ParseError);
157    CREATE_ORXONOX_EXCEPTION(PluginsNotFound);
158    CREATE_ORXONOX_EXCEPTION(InitialisationFailed);
159    CREATE_ORXONOX_EXCEPTION(InitialisationAborted);
160    CREATE_ORXONOX_EXCEPTION(NotImplemented);
161    CREATE_ORXONOX_EXCEPTION(GameState);
162    CREATE_ORXONOX_EXCEPTION(NoGraphics);
163    CREATE_ORXONOX_EXCEPTION(AbortLoading);
164
165    /** Helper function that forwards an exception and displays the message.
166    @details
167        This is necessary because only when using 'throw' the objects storage is managed.
168    */
169    template <class T>
170    inline const T& exceptionThrowerHelper(const T& exception)
171    {
172        // let the catcher decide whether to display the message below level 4
173        COUT(4) << exception.getFullDescription() << std::endl;
174        return exception;
175    }
176
177/** Throws an exception and logs a message beforehand.
178@param type
179    Type of the exception as literal (General, Initialisation, etc.)
180@param description
181    Exception description as string
182@see Exception.h
183*/
184#define ThrowException(type, description) \
185    throw orxonox::exceptionThrowerHelper(type##Exception(static_cast<std::ostringstream&>(std::ostringstream().flush() << description).str(), __LINE__, __FILE__, __FUNCTIONNAME__))
186
187} /* namespace orxonox */
188
189#endif /* _Exception_H__ */
Note: See TracBrowser for help on using the repository browser.