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 | namespace orxonox |
---|
46 | { |
---|
47 | class _CoreExport Exception : public std::exception |
---|
48 | { |
---|
49 | public: |
---|
50 | enum ExceptionType |
---|
51 | { |
---|
52 | General, |
---|
53 | FileNotFound, |
---|
54 | PluginsNotFound, |
---|
55 | InitialisationFailed, |
---|
56 | NotImplemented |
---|
57 | }; |
---|
58 | |
---|
59 | Exception(const std::string& description, int lineNumber, |
---|
60 | const char* fileName, const char* functionName); |
---|
61 | Exception(const std::string& description); |
---|
62 | |
---|
63 | /// Needed for compatibility with std::exception (from Ogre::Exception) |
---|
64 | virtual ~Exception() throw() { } |
---|
65 | |
---|
66 | virtual std::string getFullDescription() const; |
---|
67 | virtual ExceptionType getType() const = 0; |
---|
68 | virtual std::string getTypeName() const = 0; |
---|
69 | virtual const std::string& getDescription() const { return this->description_; } |
---|
70 | virtual const int getLineNumber() const { return this->lineNumber_; } |
---|
71 | virtual const std::string& getFunctionName() const { return this->functionName_; } |
---|
72 | |
---|
73 | /// Override std::exception::what (from Ogre::Exception) |
---|
74 | const char* what() const throw() { return getFullDescription().c_str(); } |
---|
75 | |
---|
76 | protected: |
---|
77 | std::string description_; |
---|
78 | int lineNumber_; |
---|
79 | std::string functionName_; |
---|
80 | std::string fileName_; |
---|
81 | }; |
---|
82 | |
---|
83 | |
---|
84 | template <Exception::ExceptionType Type> |
---|
85 | class SpecificException : public Exception |
---|
86 | { |
---|
87 | public: |
---|
88 | SpecificException(const std::string& description, int lineNumber, |
---|
89 | const char* fileName, const char* functionName) |
---|
90 | : Exception(description, lineNumber, fileName, functionName) |
---|
91 | { |
---|
92 | // let the catcher decide whether to display the message or not |
---|
93 | //COUT(1) << this->getFullDescription() << std::endl; |
---|
94 | } |
---|
95 | |
---|
96 | SpecificException(const std::string& description) |
---|
97 | : Exception(description) |
---|
98 | { |
---|
99 | // let the catcher decide whether to display the message or not |
---|
100 | //COUT(1) << this->getFullDescription() << std::endl; |
---|
101 | } |
---|
102 | |
---|
103 | ~SpecificException() throw() { } |
---|
104 | |
---|
105 | ExceptionType getType() const { return Type; } |
---|
106 | std::string getTypeName() const |
---|
107 | { |
---|
108 | // note: break is not necessary due to the return. Keep in mind! |
---|
109 | switch (Type) |
---|
110 | { |
---|
111 | case Exception::General: |
---|
112 | return "General"; |
---|
113 | case Exception::FileNotFound: |
---|
114 | return "FileNotFound"; |
---|
115 | case Exception::PluginsNotFound: |
---|
116 | return "PluginsNotFound"; |
---|
117 | case Exception::InitialisationFailed: |
---|
118 | return "InitialisationFailed"; |
---|
119 | case Exception::NotImplemented: |
---|
120 | return "NotImplemented"; |
---|
121 | default: |
---|
122 | return ""; |
---|
123 | } |
---|
124 | } |
---|
125 | }; |
---|
126 | |
---|
127 | typedef SpecificException<Exception::General> GeneralException; |
---|
128 | typedef SpecificException<Exception::FileNotFound> FileNotFoundException; |
---|
129 | typedef SpecificException<Exception::PluginsNotFound> PluginsNotFoundException; |
---|
130 | typedef SpecificException<Exception::InitialisationFailed> InitialisationFailedException; |
---|
131 | typedef SpecificException<Exception::NotImplemented> NotImplementedException; |
---|
132 | |
---|
133 | #define ThrowException(type, description) \ |
---|
134 | throw SpecificException<Exception::type>(description, __LINE__, __FILE__, __FUNCTIONNAME__) |
---|
135 | |
---|
136 | // define an assert macro that can display a message |
---|
137 | #ifndef NDEBUG |
---|
138 | #define OrxAssert(condition, errorMessage) \ |
---|
139 | condition ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \ |
---|
140 | assert(condition) |
---|
141 | #else |
---|
142 | #define OrxAssert(condition, errorMessage) ((void)0) |
---|
143 | #endif |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | #endif /* _Exception_H__ */ |
---|