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