Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/util/Exception.h @ 2489

Last change on this file since 2489 was 2305, checked in by rgrieder, 16 years ago
  • Exported Exception displaying to an inline function instead of the exception constructor. This will make things easier with the SpecificExceptions. Going to do that when merging with OH2.
  • Added abort() statement to the global exception "try" in RootGameState
  • Changed global exception "try" to be activated always except when using the msvc debugger.
  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/physics/src/util/Exception.hmergedeligible
    /code/branches/ceguilua/src/util/Exception.h1802-1808
    /code/branches/core3/src/core/Exception.h1572-1739
    /code/branches/gcc43/src/core/Exception.h1580
    /code/branches/gui/src/core/Exception.h1635-1636
    /code/branches/input/src/core/Exception.h1629-1636
    /code/branches/objecthierarchy/src/util/Exception.h1911-2085,​2100
    /code/branches/pickups/src/util/Exception.h1926-2086
    /code/branches/questsystem/src/util/Exception.h1894-2088
    /code/branches/weapon/src/util/Exception.h1925-2094
File size: 5.5 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 the Exception class.
33*/
34
35#ifndef _Exception_H__
36#define _Exception_H__
37
38#include "UtilPrereqs.h"
39
40#include <string>
41#include <exception>
42#include <cassert>
43#include "Debug.h"
44
45// Define some ugly macros to make things more clear
46#define CREATE_ORXONOX_EXCEPTION(name) typedef SpecificException<Exception::name> name##Exception;
47#define RETURN_EXCEPTION_CODE(name) \
48    case Exception::name:           \
49        return #name;
50
51
52namespace orxonox
53{
54    class _UtilExport Exception : public std::exception
55    {
56    public:
57        enum ExceptionType
58        {
59            General,
60            FileNotFound,
61            Argument,
62            PhysicsViolation,
63            ParseError,
64            PluginsNotFound,
65            InitialisationFailed,
66            NotImplemented,
67            GameState
68        };
69
70        Exception(const std::string& description, int lineNumber,
71                  const char* filename, const char* functionName);
72        Exception(const std::string& description);
73
74        /// Needed for  compatibility with std::exception (from Ogre::Exception)
75        virtual ~Exception() throw() { }
76
77        virtual const std::string& getFullDescription() const;
78        virtual ExceptionType      getType()            const = 0;
79        virtual std::string        getTypeName()        const = 0;
80        virtual const std::string& getDescription()     const { return this->description_; }
81        virtual const int          getLineNumber()      const { return this->lineNumber_; }
82        virtual const std::string& getFunctionName()    const { return this->functionName_; }
83
84        /// Override std::exception::what (from Ogre::Exception)
85        const char* what() const throw() { return getFullDescription().c_str(); }
86
87    protected:
88        std::string description_;
89        int lineNumber_;
90        std::string functionName_;
91        std::string filename_;
92        // mutable because "what()" is a const method
93        mutable std::string fullDescription_;
94    };
95
96
97    template <Exception::ExceptionType Type>
98    class SpecificException : public Exception
99    {
100    public:
101        SpecificException(const std::string& description, int lineNumber,
102                  const char* filename, const char* functionName)
103                  : Exception(description, lineNumber, filename, functionName)
104        {
105        }
106
107        SpecificException(const std::string& description)
108            : Exception(description)
109        {
110        }
111
112        ~SpecificException() throw() { }
113
114        ExceptionType getType() const { return Type; }
115        std::string getTypeName() const
116        {
117            // note: break is not necessary due to the return in the macros.
118            switch (Type)
119            {
120            RETURN_EXCEPTION_CODE(General)
121            RETURN_EXCEPTION_CODE(FileNotFound);
122            RETURN_EXCEPTION_CODE(Argument);
123            RETURN_EXCEPTION_CODE(PhysicsViolation);
124            RETURN_EXCEPTION_CODE(ParseError);
125            RETURN_EXCEPTION_CODE(PluginsNotFound);
126            RETURN_EXCEPTION_CODE(InitialisationFailed);
127            RETURN_EXCEPTION_CODE(NotImplemented);
128            RETURN_EXCEPTION_CODE(GameState);
129            default:
130                return "";
131            }
132        }
133    };
134
135    // define the template spcialisations
136    CREATE_ORXONOX_EXCEPTION(General);
137    CREATE_ORXONOX_EXCEPTION(FileNotFound);
138    CREATE_ORXONOX_EXCEPTION(Argument);
139    CREATE_ORXONOX_EXCEPTION(PhysicsViolation);
140    CREATE_ORXONOX_EXCEPTION(ParseError);
141    CREATE_ORXONOX_EXCEPTION(PluginsNotFound);
142    CREATE_ORXONOX_EXCEPTION(InitialisationFailed);
143    CREATE_ORXONOX_EXCEPTION(NotImplemented);
144    CREATE_ORXONOX_EXCEPTION(GameState);
145
146    /**
147    @brief
148        Helper function that creates an exception, displays the message, but doesn't throw it.
149    */
150    template <class T>
151    inline const T& InternalHandleException(const T& exception)
152    {
153        // let the catcher decide whether to display the message below level 4
154        COUT(4) << exception.getFullDescription() << std::endl;
155        return exception;
156    }
157
158#define ThrowException(type, description) \
159    throw InternalHandleException(SpecificException<Exception::type>(description, __LINE__, __FILE__, __FUNCTIONNAME__))
160
161    // define an assert macro that can display a message
162#ifndef NDEBUG
163#define OrxAssert(assertion, errorMessage) \
164    assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \
165    assert(assertion)
166#else
167#define OrxAssert(condition, errorMessage)  ((void)0)
168#endif
169
170}
171
172#endif /* _Exception_H__ */
Note: See TracBrowser for help on using the repository browser.