1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef __Exception_H_ |
---|
29 | #define __Exception_H_ |
---|
30 | |
---|
31 | // Precompiler options |
---|
32 | #include "OgrePrerequisites.h" |
---|
33 | #include "OgreString.h" |
---|
34 | #include <exception> |
---|
35 | #include "OgreHeaderPrefix.h" |
---|
36 | |
---|
37 | // Check for OGRE assert mode |
---|
38 | |
---|
39 | // RELEASE_EXCEPTIONS mode |
---|
40 | #if OGRE_ASSERT_MODE == 1 |
---|
41 | # if OGRE_DEBUG_MODE |
---|
42 | # define OgreAssert( a, b ) assert( (a) && (b) ) |
---|
43 | # else |
---|
44 | # if OGRE_COMP != OGRE_COMPILER_BORL |
---|
45 | # define OgreAssert( a, b ) if( !(a) ) OGRE_EXCEPT( Ogre::Exception::ERR_RT_ASSERTION_FAILED, (b), "no function info") |
---|
46 | # else |
---|
47 | # define OgreAssert( a, b ) if( !(a) ) OGRE_EXCEPT( Ogre::Exception::ERR_RT_ASSERTION_FAILED, (b), __FUNC__ ) |
---|
48 | # endif |
---|
49 | # endif |
---|
50 | |
---|
51 | // EXCEPTIONS mode |
---|
52 | #elif OGRE_ASSERT_MODE == 2 |
---|
53 | # if OGRE_COMP != OGRE_COMPILER_BORL |
---|
54 | # define OgreAssert( a, b ) if( !(a) ) OGRE_EXCEPT( Ogre::Exception::ERR_RT_ASSERTION_FAILED, (b), "no function info") |
---|
55 | # else |
---|
56 | # define OgreAssert( a, b ) if( !(a) ) OGRE_EXCEPT( Ogre::Exception::ERR_RT_ASSERTION_FAILED, (b), __FUNC__ ) |
---|
57 | # endif |
---|
58 | |
---|
59 | // STANDARD mode |
---|
60 | #else |
---|
61 | # define OgreAssert( a, b ) assert( (a) && (b) ) |
---|
62 | #endif |
---|
63 | |
---|
64 | namespace Ogre { |
---|
65 | /** \addtogroup Core |
---|
66 | * @{ |
---|
67 | */ |
---|
68 | /** \addtogroup General |
---|
69 | * @{ |
---|
70 | */ |
---|
71 | /** When thrown, provides information about an error that has occurred inside the engine. |
---|
72 | @remarks |
---|
73 | OGRE never uses return values to indicate errors. Instead, if an |
---|
74 | error occurs, an exception is thrown, and this is the object that |
---|
75 | encapsulates the detail of the problem. The application using |
---|
76 | OGRE should always ensure that the exceptions are caught, so all |
---|
77 | OGRE engine functions should occur within a |
---|
78 | try{} catch(Ogre::Exception& e) {} block. |
---|
79 | @par |
---|
80 | The user application should never create any instances of this |
---|
81 | object unless it wishes to unify its error handling using the |
---|
82 | same object. |
---|
83 | */ |
---|
84 | class _OgreExport Exception : public std::exception |
---|
85 | { |
---|
86 | protected: |
---|
87 | long line; |
---|
88 | int number; |
---|
89 | String typeName; |
---|
90 | String description; |
---|
91 | String source; |
---|
92 | String file; |
---|
93 | mutable String fullDesc; |
---|
94 | public: |
---|
95 | /** Static definitions of error codes. |
---|
96 | @todo |
---|
97 | Add many more exception codes, since we want the user to be able |
---|
98 | to catch most of them. |
---|
99 | */ |
---|
100 | enum ExceptionCodes { |
---|
101 | ERR_CANNOT_WRITE_TO_FILE, |
---|
102 | ERR_INVALID_STATE, |
---|
103 | ERR_INVALIDPARAMS, |
---|
104 | ERR_RENDERINGAPI_ERROR, |
---|
105 | ERR_DUPLICATE_ITEM, |
---|
106 | ERR_ITEM_NOT_FOUND, |
---|
107 | ERR_FILE_NOT_FOUND, |
---|
108 | ERR_INTERNAL_ERROR, |
---|
109 | ERR_RT_ASSERTION_FAILED, |
---|
110 | ERR_NOT_IMPLEMENTED |
---|
111 | }; |
---|
112 | |
---|
113 | /** Default constructor. |
---|
114 | */ |
---|
115 | Exception( int number, const String& description, const String& source ); |
---|
116 | |
---|
117 | /** Advanced constructor. |
---|
118 | */ |
---|
119 | Exception( int number, const String& description, const String& source, const char* type, const char* file, long line ); |
---|
120 | |
---|
121 | /** Copy constructor. |
---|
122 | */ |
---|
123 | Exception(const Exception& rhs); |
---|
124 | |
---|
125 | /// Needed for compatibility with std::exception |
---|
126 | ~Exception() throw() {} |
---|
127 | |
---|
128 | /** Assignment operator. |
---|
129 | */ |
---|
130 | void operator = (const Exception& rhs); |
---|
131 | |
---|
132 | /** Returns a string with the full description of this error. |
---|
133 | @remarks |
---|
134 | The description contains the error number, the description |
---|
135 | supplied by the thrower, what routine threw the exception, |
---|
136 | and will also supply extra platform-specific information |
---|
137 | where applicable. For example - in the case of a rendering |
---|
138 | library error, the description of the error will include both |
---|
139 | the place in which OGRE found the problem, and a text |
---|
140 | description from the 3D rendering library, if available. |
---|
141 | */ |
---|
142 | virtual const String& getFullDescription(void) const; |
---|
143 | |
---|
144 | /** Gets the error code. |
---|
145 | */ |
---|
146 | virtual int getNumber(void) const throw(); |
---|
147 | |
---|
148 | /** Gets the source function. |
---|
149 | */ |
---|
150 | virtual const String &getSource() const { return source; } |
---|
151 | |
---|
152 | /** Gets source file name. |
---|
153 | */ |
---|
154 | virtual const String &getFile() const { return file; } |
---|
155 | |
---|
156 | /** Gets line number. |
---|
157 | */ |
---|
158 | virtual long getLine() const { return line; } |
---|
159 | |
---|
160 | /** Returns a string with only the 'description' field of this exception. Use |
---|
161 | getFullDescriptionto get a full description of the error including line number, |
---|
162 | error number and what function threw the exception. |
---|
163 | */ |
---|
164 | virtual const String &getDescription(void) const { return description; } |
---|
165 | |
---|
166 | /// Override std::exception::what |
---|
167 | const char* what() const throw() { return getFullDescription().c_str(); } |
---|
168 | |
---|
169 | }; |
---|
170 | |
---|
171 | |
---|
172 | /** Template struct which creates a distinct type for each exception code. |
---|
173 | @note |
---|
174 | This is useful because it allows us to create an overloaded method |
---|
175 | for returning different exception types by value without ambiguity. |
---|
176 | From 'Modern C++ Design' (Alexandrescu 2001). |
---|
177 | */ |
---|
178 | template <int num> |
---|
179 | struct ExceptionCodeType |
---|
180 | { |
---|
181 | enum { number = num }; |
---|
182 | }; |
---|
183 | |
---|
184 | // Specialised exceptions allowing each to be caught specifically |
---|
185 | // backwards-compatible since exception codes still used |
---|
186 | |
---|
187 | class _OgreExport UnimplementedException : public Exception |
---|
188 | { |
---|
189 | public: |
---|
190 | UnimplementedException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
191 | : Exception(inNumber, inDescription, inSource, "UnimplementedException", inFile, inLine) {} |
---|
192 | }; |
---|
193 | class _OgreExport FileNotFoundException : public Exception |
---|
194 | { |
---|
195 | public: |
---|
196 | FileNotFoundException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
197 | : Exception(inNumber, inDescription, inSource, "FileNotFoundException", inFile, inLine) {} |
---|
198 | }; |
---|
199 | class _OgreExport IOException : public Exception |
---|
200 | { |
---|
201 | public: |
---|
202 | IOException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
203 | : Exception(inNumber, inDescription, inSource, "IOException", inFile, inLine) {} |
---|
204 | }; |
---|
205 | class _OgreExport InvalidStateException : public Exception |
---|
206 | { |
---|
207 | public: |
---|
208 | InvalidStateException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
209 | : Exception(inNumber, inDescription, inSource, "InvalidStateException", inFile, inLine) {} |
---|
210 | }; |
---|
211 | class _OgreExport InvalidParametersException : public Exception |
---|
212 | { |
---|
213 | public: |
---|
214 | InvalidParametersException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
215 | : Exception(inNumber, inDescription, inSource, "InvalidParametersException", inFile, inLine) {} |
---|
216 | }; |
---|
217 | class _OgreExport ItemIdentityException : public Exception |
---|
218 | { |
---|
219 | public: |
---|
220 | ItemIdentityException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
221 | : Exception(inNumber, inDescription, inSource, "ItemIdentityException", inFile, inLine) {} |
---|
222 | }; |
---|
223 | class _OgreExport InternalErrorException : public Exception |
---|
224 | { |
---|
225 | public: |
---|
226 | InternalErrorException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
227 | : Exception(inNumber, inDescription, inSource, "InternalErrorException", inFile, inLine) {} |
---|
228 | }; |
---|
229 | class _OgreExport RenderingAPIException : public Exception |
---|
230 | { |
---|
231 | public: |
---|
232 | RenderingAPIException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
233 | : Exception(inNumber, inDescription, inSource, "RenderingAPIException", inFile, inLine) {} |
---|
234 | }; |
---|
235 | class _OgreExport RuntimeAssertionException : public Exception |
---|
236 | { |
---|
237 | public: |
---|
238 | RuntimeAssertionException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine) |
---|
239 | : Exception(inNumber, inDescription, inSource, "RuntimeAssertionException", inFile, inLine) {} |
---|
240 | }; |
---|
241 | |
---|
242 | |
---|
243 | /** Class implementing dispatch methods in order to construct by-value |
---|
244 | exceptions of a derived type based just on an exception code. |
---|
245 | @remarks |
---|
246 | This nicely handles construction of derived Exceptions by value (needed |
---|
247 | for throwing) without suffering from ambiguity - each code is turned into |
---|
248 | a distinct type so that methods can be overloaded. This allows OGRE_EXCEPT |
---|
249 | to stay small in implementation (desirable since it is embedded) whilst |
---|
250 | still performing rich code-to-type mapping. |
---|
251 | */ |
---|
252 | class ExceptionFactory |
---|
253 | { |
---|
254 | private: |
---|
255 | /// Private constructor, no construction |
---|
256 | ExceptionFactory() {} |
---|
257 | public: |
---|
258 | static UnimplementedException create( |
---|
259 | ExceptionCodeType<Exception::ERR_NOT_IMPLEMENTED> code, |
---|
260 | const String& desc, |
---|
261 | const String& src, const char* file, long line) |
---|
262 | { |
---|
263 | return UnimplementedException(code.number, desc, src, file, line); |
---|
264 | } |
---|
265 | static FileNotFoundException create( |
---|
266 | ExceptionCodeType<Exception::ERR_FILE_NOT_FOUND> code, |
---|
267 | const String& desc, |
---|
268 | const String& src, const char* file, long line) |
---|
269 | { |
---|
270 | return FileNotFoundException(code.number, desc, src, file, line); |
---|
271 | } |
---|
272 | static IOException create( |
---|
273 | ExceptionCodeType<Exception::ERR_CANNOT_WRITE_TO_FILE> code, |
---|
274 | const String& desc, |
---|
275 | const String& src, const char* file, long line) |
---|
276 | { |
---|
277 | return IOException(code.number, desc, src, file, line); |
---|
278 | } |
---|
279 | static InvalidStateException create( |
---|
280 | ExceptionCodeType<Exception::ERR_INVALID_STATE> code, |
---|
281 | const String& desc, |
---|
282 | const String& src, const char* file, long line) |
---|
283 | { |
---|
284 | return InvalidStateException(code.number, desc, src, file, line); |
---|
285 | } |
---|
286 | static InvalidParametersException create( |
---|
287 | ExceptionCodeType<Exception::ERR_INVALIDPARAMS> code, |
---|
288 | const String& desc, |
---|
289 | const String& src, const char* file, long line) |
---|
290 | { |
---|
291 | return InvalidParametersException(code.number, desc, src, file, line); |
---|
292 | } |
---|
293 | static ItemIdentityException create( |
---|
294 | ExceptionCodeType<Exception::ERR_ITEM_NOT_FOUND> code, |
---|
295 | const String& desc, |
---|
296 | const String& src, const char* file, long line) |
---|
297 | { |
---|
298 | return ItemIdentityException(code.number, desc, src, file, line); |
---|
299 | } |
---|
300 | static ItemIdentityException create( |
---|
301 | ExceptionCodeType<Exception::ERR_DUPLICATE_ITEM> code, |
---|
302 | const String& desc, |
---|
303 | const String& src, const char* file, long line) |
---|
304 | { |
---|
305 | return ItemIdentityException(code.number, desc, src, file, line); |
---|
306 | } |
---|
307 | static InternalErrorException create( |
---|
308 | ExceptionCodeType<Exception::ERR_INTERNAL_ERROR> code, |
---|
309 | const String& desc, |
---|
310 | const String& src, const char* file, long line) |
---|
311 | { |
---|
312 | return InternalErrorException(code.number, desc, src, file, line); |
---|
313 | } |
---|
314 | static RenderingAPIException create( |
---|
315 | ExceptionCodeType<Exception::ERR_RENDERINGAPI_ERROR> code, |
---|
316 | const String& desc, |
---|
317 | const String& src, const char* file, long line) |
---|
318 | { |
---|
319 | return RenderingAPIException(code.number, desc, src, file, line); |
---|
320 | } |
---|
321 | static RuntimeAssertionException create( |
---|
322 | ExceptionCodeType<Exception::ERR_RT_ASSERTION_FAILED> code, |
---|
323 | const String& desc, |
---|
324 | const String& src, const char* file, long line) |
---|
325 | { |
---|
326 | return RuntimeAssertionException(code.number, desc, src, file, line); |
---|
327 | } |
---|
328 | |
---|
329 | }; |
---|
330 | |
---|
331 | |
---|
332 | |
---|
333 | #ifndef OGRE_EXCEPT |
---|
334 | #define OGRE_EXCEPT(num, desc, src) throw Ogre::ExceptionFactory::create( \ |
---|
335 | Ogre::ExceptionCodeType<num>(), desc, src, __FILE__, __LINE__ ); |
---|
336 | #endif |
---|
337 | /** @} */ |
---|
338 | /** @} */ |
---|
339 | |
---|
340 | } // Namespace Ogre |
---|
341 | |
---|
342 | #include "OgreHeaderSuffix.h" |
---|
343 | |
---|
344 | #endif |
---|