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