OgreException.h 13 KB

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