Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/Exception.h @ 1660

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

GameState class added. Tested and working, but now comes the hard part: Implementing the actual states…

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