CmException.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "CmString.h"
  28. #include <exception>
  29. namespace CamelotEngine {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup General
  34. * @{
  35. */
  36. /** When thrown, provides information about an error that has occurred inside the engine.
  37. @remarks
  38. OGRE never uses return values to indicate errors. Instead, if an
  39. error occurs, an exception is thrown, and this is the object that
  40. encapsulates the detail of the problem. The application using
  41. OGRE should always ensure that the exceptions are caught, so all
  42. OGRE engine functions should occur within a
  43. try{} catch(CamelotEngine::Exception& e) {} block.
  44. @par
  45. The user application should never create any instances of this
  46. object unless it wishes to unify its error handling using the
  47. same object.
  48. */
  49. class CM_EXPORT Exception : public std::exception
  50. {
  51. protected:
  52. long line;
  53. int number;
  54. String typeName;
  55. String description;
  56. String source;
  57. String file;
  58. mutable String fullDesc;
  59. public:
  60. /** Static definitions of error codes.
  61. @todo
  62. Add many more exception codes, since we want the user to be able
  63. to catch most of them.
  64. */
  65. enum ExceptionCodes {
  66. ERR_CANNOT_WRITE_TO_FILE,
  67. ERR_INVALID_STATE,
  68. ERR_INVALIDPARAMS,
  69. ERR_RENDERINGAPI_ERROR,
  70. ERR_DUPLICATE_ITEM,
  71. ERR_ITEM_NOT_FOUND,
  72. ERR_FILE_NOT_FOUND,
  73. ERR_INTERNAL_ERROR,
  74. ERR_RT_ASSERTION_FAILED,
  75. ERR_NOT_IMPLEMENTED
  76. };
  77. /** Default constructor.
  78. */
  79. Exception( int number, const String& description, const String& source );
  80. /** Advanced constructor.
  81. */
  82. Exception( int number, const String& description, const String& source, const char* type, const char* file, long line );
  83. /** Copy constructor.
  84. */
  85. Exception(const Exception& rhs);
  86. /// Needed for compatibility with std::exception
  87. ~Exception() throw() {}
  88. /** Assignment operator.
  89. */
  90. void operator = (const Exception& rhs);
  91. /** Returns a string with the full description of this error.
  92. @remarks
  93. The description contains the error number, the description
  94. supplied by the thrower, what routine threw the exception,
  95. and will also supply extra platform-specific information
  96. where applicable. For example - in the case of a rendering
  97. library error, the description of the error will include both
  98. the place in which OGRE found the problem, and a text
  99. description from the 3D rendering library, if available.
  100. */
  101. virtual const String& getFullDescription(void) const;
  102. /** Gets the error code.
  103. */
  104. virtual int getNumber(void) const throw();
  105. /** Gets the source function.
  106. */
  107. virtual const String &getSource() const { return source; }
  108. /** Gets source file name.
  109. */
  110. virtual const String &getFile() const { return file; }
  111. /** Gets line number.
  112. */
  113. virtual long getLine() const { return line; }
  114. /** Returns a string with only the 'description' field of this exception. Use
  115. getFullDescriptionto get a full description of the error including line number,
  116. error number and what function threw the exception.
  117. */
  118. virtual const String &getDescription(void) const { return description; }
  119. /// Override std::exception::what
  120. const char* what() const throw() { return getFullDescription().c_str(); }
  121. };
  122. /** Template struct which creates a distinct type for each exception code.
  123. @note
  124. This is useful because it allows us to create an overloaded method
  125. for returning different exception types by value without ambiguity.
  126. From 'Modern C++ Design' (Alexandrescu 2001).
  127. */
  128. template <int num>
  129. struct ExceptionCodeType
  130. {
  131. enum { number = num };
  132. };
  133. // Specialised exceptions allowing each to be caught specifically
  134. // backwards-compatible since exception codes still used
  135. class CM_EXPORT UnimplementedException : public Exception
  136. {
  137. public:
  138. UnimplementedException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  139. : Exception(inNumber, inDescription, inSource, "UnimplementedException", inFile, inLine) {}
  140. };
  141. class CM_EXPORT FileNotFoundException : public Exception
  142. {
  143. public:
  144. FileNotFoundException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  145. : Exception(inNumber, inDescription, inSource, "FileNotFoundException", inFile, inLine) {}
  146. };
  147. class CM_EXPORT IOException : public Exception
  148. {
  149. public:
  150. IOException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  151. : Exception(inNumber, inDescription, inSource, "IOException", inFile, inLine) {}
  152. };
  153. class CM_EXPORT InvalidStateException : public Exception
  154. {
  155. public:
  156. InvalidStateException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  157. : Exception(inNumber, inDescription, inSource, "InvalidStateException", inFile, inLine) {}
  158. };
  159. class CM_EXPORT InvalidParametersException : public Exception
  160. {
  161. public:
  162. InvalidParametersException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  163. : Exception(inNumber, inDescription, inSource, "InvalidParametersException", inFile, inLine) {}
  164. };
  165. class CM_EXPORT ItemIdentityException : public Exception
  166. {
  167. public:
  168. ItemIdentityException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  169. : Exception(inNumber, inDescription, inSource, "ItemIdentityException", inFile, inLine) {}
  170. };
  171. class CM_EXPORT InternalErrorException : public Exception
  172. {
  173. public:
  174. InternalErrorException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  175. : Exception(inNumber, inDescription, inSource, "InternalErrorException", inFile, inLine) {}
  176. };
  177. class CM_EXPORT RenderingAPIException : public Exception
  178. {
  179. public:
  180. RenderingAPIException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  181. : Exception(inNumber, inDescription, inSource, "RenderingAPIException", inFile, inLine) {}
  182. };
  183. class CM_EXPORT RuntimeAssertionException : public Exception
  184. {
  185. public:
  186. RuntimeAssertionException(int inNumber, const String& inDescription, const String& inSource, const char* inFile, long inLine)
  187. : Exception(inNumber, inDescription, inSource, "RuntimeAssertionException", inFile, inLine) {}
  188. };
  189. /** Class implementing dispatch methods in order to construct by-value
  190. exceptions of a derived type based just on an exception code.
  191. @remarks
  192. This nicely handles construction of derived Exceptions by value (needed
  193. for throwing) without suffering from ambiguity - each code is turned into
  194. a distinct type so that methods can be overloaded. This allows OGRE_EXCEPT
  195. to stay small in implementation (desirable since it is embedded) whilst
  196. still performing rich code-to-type mapping.
  197. */
  198. class ExceptionFactory
  199. {
  200. private:
  201. /// Private constructor, no construction
  202. ExceptionFactory() {}
  203. public:
  204. static UnimplementedException create(
  205. ExceptionCodeType<Exception::ERR_NOT_IMPLEMENTED> code,
  206. const String& desc,
  207. const String& src, const char* file, long line)
  208. {
  209. return UnimplementedException(code.number, desc, src, file, line);
  210. }
  211. static FileNotFoundException create(
  212. ExceptionCodeType<Exception::ERR_FILE_NOT_FOUND> code,
  213. const String& desc,
  214. const String& src, const char* file, long line)
  215. {
  216. return FileNotFoundException(code.number, desc, src, file, line);
  217. }
  218. static IOException create(
  219. ExceptionCodeType<Exception::ERR_CANNOT_WRITE_TO_FILE> code,
  220. const String& desc,
  221. const String& src, const char* file, long line)
  222. {
  223. return IOException(code.number, desc, src, file, line);
  224. }
  225. static InvalidStateException create(
  226. ExceptionCodeType<Exception::ERR_INVALID_STATE> code,
  227. const String& desc,
  228. const String& src, const char* file, long line)
  229. {
  230. return InvalidStateException(code.number, desc, src, file, line);
  231. }
  232. static InvalidParametersException create(
  233. ExceptionCodeType<Exception::ERR_INVALIDPARAMS> code,
  234. const String& desc,
  235. const String& src, const char* file, long line)
  236. {
  237. return InvalidParametersException(code.number, desc, src, file, line);
  238. }
  239. static ItemIdentityException create(
  240. ExceptionCodeType<Exception::ERR_ITEM_NOT_FOUND> code,
  241. const String& desc,
  242. const String& src, const char* file, long line)
  243. {
  244. return ItemIdentityException(code.number, desc, src, file, line);
  245. }
  246. static ItemIdentityException create(
  247. ExceptionCodeType<Exception::ERR_DUPLICATE_ITEM> code,
  248. const String& desc,
  249. const String& src, const char* file, long line)
  250. {
  251. return ItemIdentityException(code.number, desc, src, file, line);
  252. }
  253. static InternalErrorException create(
  254. ExceptionCodeType<Exception::ERR_INTERNAL_ERROR> code,
  255. const String& desc,
  256. const String& src, const char* file, long line)
  257. {
  258. return InternalErrorException(code.number, desc, src, file, line);
  259. }
  260. static RenderingAPIException create(
  261. ExceptionCodeType<Exception::ERR_RENDERINGAPI_ERROR> code,
  262. const String& desc,
  263. const String& src, const char* file, long line)
  264. {
  265. return RenderingAPIException(code.number, desc, src, file, line);
  266. }
  267. static RuntimeAssertionException create(
  268. ExceptionCodeType<Exception::ERR_RT_ASSERTION_FAILED> code,
  269. const String& desc,
  270. const String& src, const char* file, long line)
  271. {
  272. return RuntimeAssertionException(code.number, desc, src, file, line);
  273. }
  274. };
  275. #ifndef OGRE_EXCEPT
  276. #define OGRE_EXCEPT(num, desc, src) throw CamelotEngine::ExceptionFactory::create( \
  277. CamelotEngine::ExceptionCodeType<num>(), desc, src, __FILE__, __LINE__ );
  278. #endif
  279. /** @} */
  280. /** @} */
  281. } // namespace CamelotEngine
  282. #endif