Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/Exception.h @ 2776

Last change on this file since 2776 was 2710, checked in by rgrieder, 16 years ago

Merged buildsystem3 containing buildsystem2 containing Adi's buildsystem branch back to the trunk.
Please update the media directory if you were not using buildsystem3 before.

  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/lodfinal/src/util/Exception.hmergedeligible
    /code/branches/buildsystem/src/util/Exception.h1874-2276,​2278-2400
    /code/branches/buildsystem2/src/util/Exception.h2506-2658
    /code/branches/buildsystem3/src/util/Exception.h2662-2708
    /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/network/src/util/Exception.h2356
    /code/branches/network64/src/util/Exception.h2210-2355
    /code/branches/objecthierarchy/src/util/Exception.h1911-2085,​2100,​2110-2169
    /code/branches/objecthierarchy2/src/util/Exception.h2171-2479
    /code/branches/overlay/src/util/Exception.h2117-2385
    /code/branches/physics/src/util/Exception.h1912-2055,​2107-2439
    /code/branches/physics_merge/src/util/Exception.h2436-2457
    /code/branches/pickups/src/util/Exception.h1926-2086,​2127
    /code/branches/pickups2/src/util/Exception.h2107-2497
    /code/branches/presentation/src/util/Exception.h2369-2652,​2654-2660
    /code/branches/questsystem/src/util/Exception.h1894-2088
    /code/branches/questsystem2/src/util/Exception.h2107-2259
    /code/branches/weapon/src/util/Exception.h1925-2094
    /code/branches/weapon2/src/util/Exception.h2107-2488
File size: 5.1 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
45namespace orxonox
46{
47    class _UtilExport Exception : public std::exception
48    {
49    public:
50
51        Exception(const std::string& description, int lineNumber,
52                  const char* filename, const char* functionName);
53        Exception(const std::string& description);
54
55        /// Needed for  compatibility with std::exception (from Ogre::Exception)
56        virtual ~Exception() throw() { }
57
58        virtual const std::string& getFullDescription() const;
59        virtual std::string        getTypeName()        const = 0;
60        virtual const std::string& getDescription()     const { return this->description_; }
61        virtual const int          getLineNumber()      const { return this->lineNumber_; }
62        virtual const std::string& getFunctionName()    const { return this->functionName_; }
63
64        /// Override std::exception::what (from Ogre::Exception)
65        const char* what() const throw() { return getFullDescription().c_str(); }
66
67    protected:
68        std::string description_;
69        int lineNumber_;
70        std::string functionName_;
71        std::string filename_;
72        // mutable because "what()" is a const method
73        mutable std::string fullDescription_;
74    };
75
76
77#define CREATE_ORXONOX_EXCEPTION(ExceptionName)                                     \
78    class ExceptionName##Exception : public Exception                               \
79    {                                                                               \
80    public:                                                                         \
81        ExceptionName##Exception(const std::string& description, int lineNumber,    \
82                  const char* filename, const char* functionName)                   \
83                  : Exception(description, lineNumber, filename, functionName)      \
84        { }                                                                         \
85                                                                                    \
86        ExceptionName##Exception(const std::string& description)                    \
87                  : Exception(description)                                          \
88        { }                                                                         \
89                                                                                    \
90        ~ExceptionName##Exception() throw() { }                                     \
91                                                                                    \
92        std::string getTypeName() const { return #ExceptionName; }                  \
93    };
94
95    // Creates all possible exception types.
96    // If you want to add a new type, simply copy and adjust a new line here.
97    CREATE_ORXONOX_EXCEPTION(General);
98    CREATE_ORXONOX_EXCEPTION(FileNotFound);
99    CREATE_ORXONOX_EXCEPTION(Argument);
100    CREATE_ORXONOX_EXCEPTION(PhysicsViolation);
101    CREATE_ORXONOX_EXCEPTION(ParseError);
102    CREATE_ORXONOX_EXCEPTION(PluginsNotFound);
103    CREATE_ORXONOX_EXCEPTION(InitialisationFailed);
104    CREATE_ORXONOX_EXCEPTION(NotImplemented);
105    CREATE_ORXONOX_EXCEPTION(GameState);
106    CREATE_ORXONOX_EXCEPTION(NoGraphics);
107    CREATE_ORXONOX_EXCEPTION(AbortLoading);
108}
109
110    /**
111    @brief
112        Helper function that creates an exception, displays the message, but doesn't throw it.
113    */
114    template <class T>
115    inline const T& InternalHandleException(const T& exception)
116    {
117        // let the catcher decide whether to display the message below level 4
118        COUT(4) << exception.getFullDescription() << std::endl;
119        return exception;
120    }
121
122#define ThrowException(type, description) \
123    throw InternalHandleException(type##Exception(description, __LINE__, __FILE__, __FUNCTIONNAME__))
124
125    // define an assert macro that can display a message
126#ifndef NDEBUG
127#define OrxAssert(Assertion, ErrorMessage) \
128    Assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << ErrorMessage << std::endl); \
129    assert(Assertion)
130#else
131#define OrxAssert(condition, errorMessage)  ((void)0)
132#endif
133
134#endif /* _Exception_H__ */
Note: See TracBrowser for help on using the repository browser.