Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2037 was 1810, checked in by rgrieder, 16 years ago

merged ceguilua branch back to trunk

File size: 5.1 KB
RevLine 
[1638]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
[1764]38#include "UtilPrereqs.h"
[1638]39
40#include <string>
41#include <exception>
[1642]42#include <cassert>
[1764]43#include "Debug.h"
[1638]44
[1660]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
[1638]52namespace orxonox
53{
[1764]54    class _UtilExport Exception : public std::exception
[1638]55    {
56    public:
57        enum ExceptionType
58        {
59            General,
60            FileNotFound,
[1663]61            Argument,
[1638]62            PluginsNotFound,
[1652]63            InitialisationFailed,
[1660]64            NotImplemented,
65            GameState
[1638]66        };
67
68        Exception(const std::string& description, int lineNumber,
69                  const char* fileName, const char* functionName);
70        Exception(const std::string& description);
71
72        /// Needed for  compatibility with std::exception (from Ogre::Exception)
73        virtual ~Exception() throw() { }
74
[1664]75        virtual const std::string& getFullDescription() const;
[1638]76        virtual ExceptionType      getType()            const = 0;
77        virtual std::string        getTypeName()        const = 0;
78        virtual const std::string& getDescription()     const { return this->description_; }
79        virtual const int          getLineNumber()      const { return this->lineNumber_; }
80        virtual const std::string& getFunctionName()    const { return this->functionName_; }
81
82        /// Override std::exception::what (from Ogre::Exception)
83        const char* what() const throw() { return getFullDescription().c_str(); }
84
85    protected:
86        std::string description_;
87        int lineNumber_;
88        std::string functionName_;
89        std::string fileName_;
[1670]90        // mutable because "what()" is a const method
[1664]91        mutable std::string fullDescription_;
[1638]92    };
93
94
95    template <Exception::ExceptionType Type>
96    class SpecificException : public Exception
97    {
98    public:
99        SpecificException(const std::string& description, int lineNumber,
100                  const char* fileName, const char* functionName)
101                  : Exception(description, lineNumber, fileName, functionName)
102        {
[1704]103            // let the catcher decide whether to display the message below level 4
104            COUT(4) << this->getFullDescription() << std::endl;
[1638]105        }
106
107        SpecificException(const std::string& description)
108            : Exception(description)
109        {
[1704]110            // let the catcher decide whether to display the message below level 4
111            COUT(4) << this->getFullDescription() << std::endl;
[1638]112        }
113
[1645]114        ~SpecificException() throw() { }
[1638]115
116        ExceptionType getType() const { return Type; }
117        std::string getTypeName() const
118        {
[1660]119            // note: break is not necessary due to the return in the macros.
[1638]120            switch (Type)
121            {
[1660]122            RETURN_EXCEPTION_CODE(General)
123            RETURN_EXCEPTION_CODE(FileNotFound);
[1663]124            RETURN_EXCEPTION_CODE(Argument);
[1660]125            RETURN_EXCEPTION_CODE(PluginsNotFound);
126            RETURN_EXCEPTION_CODE(InitialisationFailed);
127            RETURN_EXCEPTION_CODE(NotImplemented);
128            RETURN_EXCEPTION_CODE(GameState);
[1638]129            default:
130                return "";
131            }
132        }
133    };
134
[1660]135    // define the template spcialisations
136    CREATE_ORXONOX_EXCEPTION(General);
137    CREATE_ORXONOX_EXCEPTION(FileNotFound);
[1663]138    CREATE_ORXONOX_EXCEPTION(Argument);
[1660]139    CREATE_ORXONOX_EXCEPTION(PluginsNotFound);
140    CREATE_ORXONOX_EXCEPTION(InitialisationFailed);
141    CREATE_ORXONOX_EXCEPTION(NotImplemented);
142    CREATE_ORXONOX_EXCEPTION(GameState);
[1638]143
144#define ThrowException(type, description) \
145    throw SpecificException<Exception::type>(description, __LINE__, __FILE__, __FUNCTIONNAME__)
146
[1642]147    // define an assert macro that can display a message
148#ifndef NDEBUG
[1663]149#define OrxAssert(assertion, errorMessage) \
150    assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \
151    assert(assertion)
[1642]152#else
153#define OrxAssert(condition, errorMessage)  ((void)0)
154#endif
155
[1638]156}
157
158#endif /* _Exception_H__ */
Note: See TracBrowser for help on using the repository browser.