Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreException.h @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

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