Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/util/Exception.h @ 4663

Last change on this file since 4663 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

  • Property svn:eol-style set to native
File size: 6.7 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*/
34
35#ifndef _Exception_H__
36#define _Exception_H__
37
38#include "UtilPrereqs.h"
39
40#include <exception>
41#include <sstream>
42#include <string>
43#include "Debug.h"
44
45namespace orxonox
46{
47    /**
48    @brief
49        Base class for all exceptions (derived from std::exception).
50    @details
51        This class provides support for information about the file, the line
52        and the function the error occured.
53    */
54    class _UtilExport Exception : public std::exception
55    {
56    public:
57        /**
58        @brief
59            Creates the exception but doesn't yet compose the full descrption (because of the virtual functions)
60        @param description
61            Exception description as string. This message is supposed to help developers!
62        */
63        Exception(const std::string& description, unsigned int lineNumber,
64                  const char* filename, const char* functionName);
65        //! Simplified constructor with just a description. If you need more, use the other one.
66        Exception(const std::string& description);
67
68        //! Needed for compatibility with std::exception
69        virtual ~Exception() throw() { }
70
71        //! Returns a full description with type, line, file and function
72        virtual const std::string& getFullDescription() const;
73        //! Returns the string name of the exception type
74        virtual std::string        getTypeName()        const = 0;
75        //! Returns the short developer written exception
76        virtual const std::string& getDescription()     const { return this->description_; }
77        //! Returns the line number on which the exception occurred.
78        virtual const unsigned int getLineNumber()      const { return this->lineNumber_; }
79        //! Returns the function in which the exception occurred.
80        virtual const std::string& getFunctionName()    const { return this->functionName_; }
81        //! Returns the filename in which the exception occurred.
82        virtual const std::string& getFilename()        const { return this->filename_; }
83
84        //! Returns a full description of the error.
85        const char* what() const throw() { return getFullDescription().c_str(); }
86
87    protected:
88        std::string description_;             //!< User typed text about why the exception occurred
89        unsigned int lineNumber_;             //!< Line on which the exception occurred
90        std::string functionName_;            //!< Function (including namespace and class) where the exception occurred
91        std::string filename_;                //!< File where the exception occurred
92        // mutable because "what()" is a const method
93        mutable std::string fullDescription_; //!< Full description with line, file and function
94    };
95
96//! Creates a new type of exception that inherits from tracker::Exception
97#define CREATE_ORXONOX_EXCEPTION(ExceptionName)                                     \
98    class ExceptionName##Exception : public Exception                               \
99    {                                                                               \
100    public:                                                                         \
101        ExceptionName##Exception(const std::string& description,                    \
102                unsigned int lineNumber, const char* filename,                      \
103                const char* functionName)                                           \
104            : Exception(description, lineNumber, filename, functionName)            \
105        { }                                                                         \
106                                                                                    \
107        ExceptionName##Exception(const std::string& description)                    \
108            : Exception(description)                                                \
109        { }                                                                         \
110                                                                                    \
111        ~ExceptionName##Exception() throw() { }                                     \
112                                                                                    \
113        std::string getTypeName() const { return #ExceptionName; }                  \
114    }
115
116    // Creates all possible exception types.
117    // If you want to add a new type, simply copy and adjust a new line here.
118#ifndef DOXYGEN_SHOULD_SKIP_THIS
119    CREATE_ORXONOX_EXCEPTION(General);
120    CREATE_ORXONOX_EXCEPTION(FileNotFound);
121    CREATE_ORXONOX_EXCEPTION(Argument);
122    CREATE_ORXONOX_EXCEPTION(PhysicsViolation);
123    CREATE_ORXONOX_EXCEPTION(ParseError);
124    CREATE_ORXONOX_EXCEPTION(PluginsNotFound);
125    CREATE_ORXONOX_EXCEPTION(InitialisationFailed);
126    CREATE_ORXONOX_EXCEPTION(NotImplemented);
127    CREATE_ORXONOX_EXCEPTION(GameState);
128    CREATE_ORXONOX_EXCEPTION(NoGraphics);
129    CREATE_ORXONOX_EXCEPTION(AbortLoading);
130#endif
131
132    /**
133    @brief
134        Helper function that forwards an exception and displays the message.
135    @details
136        This is necessary because only when using 'throw' the objects storage is managed.
137    */
138    template <class T>
139    inline const T& exceptionThrowerHelper(const T& exception)
140    {
141        // let the catcher decide whether to display the message below level 4
142        COUT(4) << exception.getFullDescription() << std::endl;
143        return exception;
144    }
145
146/**
147@brief
148    Throws an exception and logs a message beforehand.
149@param type
150    Type of the exception as literal (General, Initialisation, etc.)
151@param description
152    Exception description as string
153*/
154#define ThrowException(type, description, ...) \
155    throw orxonox::exceptionThrowerHelper(type##Exception(static_cast<std::ostringstream&>(std::ostringstream().flush() << description).str(), __LINE__, __FILE__, __FUNCTIONNAME__))
156
157} /* namespace orxonox */
158
159#endif /* _Exception_H__ */
Note: See TracBrowser for help on using the repository browser.