/src/FreeImage/Source/OpenEXR/Iex/IexBaseExc.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 266 lines · 92 code · 64 blank · 110 comment · 4 complexity · 0a685c3145a0beaaa1e8cb96e97bed7e MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IEXBASEEXC_H
  35. #define INCLUDED_IEXBASEEXC_H
  36. //----------------------------------------------------------
  37. //
  38. // A general exception base class, and a few
  39. // useful exceptions derived from the base class.
  40. //
  41. //----------------------------------------------------------
  42. #include <string>
  43. #include <exception>
  44. #include <sstream>
  45. namespace Iex {
  46. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  47. // Tell MS VC++ to suppress exception specification warnings
  48. #pragma warning(disable:4290)
  49. #endif
  50. //-------------------------------
  51. // Our most basic exception class
  52. //-------------------------------
  53. class BaseExc: public std::string, public std::exception
  54. {
  55. public:
  56. //----------------------------
  57. // Constructors and destructor
  58. //----------------------------
  59. BaseExc (const char *s = 0) throw(); // std::string (s)
  60. BaseExc (const std::string &s) throw(); // std::string (s)
  61. BaseExc (std::stringstream &s) throw(); // std::string (s.str())
  62. BaseExc (const BaseExc &be) throw();
  63. virtual ~BaseExc () throw ();
  64. //--------------------------------------------
  65. // what() method -- e.what() returns e.c_str()
  66. //--------------------------------------------
  67. virtual const char * what () const throw ();
  68. //--------------------------------------------------
  69. // Convenient methods to change the exception's text
  70. //--------------------------------------------------
  71. BaseExc & assign (std::stringstream &s); // assign (s.str())
  72. BaseExc & operator = (std::stringstream &s);
  73. BaseExc & append (std::stringstream &s); // append (s.str())
  74. BaseExc & operator += (std::stringstream &s);
  75. //--------------------------------------------------
  76. // These methods from the base class get obscured by
  77. // the definitions above.
  78. //--------------------------------------------------
  79. BaseExc & assign (const char *s);
  80. BaseExc & operator = (const char *s);
  81. BaseExc & append (const char *s);
  82. BaseExc & operator += (const char *s);
  83. //--------------------------------------------------
  84. // Stack trace for the point at which the exception
  85. // was thrown. The stack trace will be an empty
  86. // string unless a working stack-tracing routine
  87. // has been installed (see below, setStackTracer()).
  88. //--------------------------------------------------
  89. const std::string & stackTrace () const;
  90. private:
  91. std::string _stackTrace;
  92. };
  93. //-----------------------------------------------------
  94. // A macro to save typing when declararing an exception
  95. // class derived directly or indirectly from BaseExc:
  96. //-----------------------------------------------------
  97. #define DEFINE_EXC(name, base) \
  98. class name: public base \
  99. { \
  100. public: \
  101. name (const char* text=0) throw(): base (text) {} \
  102. name (const std::string &text) throw(): base (text) {} \
  103. name (std::stringstream &text) throw(): base (text) {} \
  104. };
  105. //--------------------------------------------------------
  106. // Some exceptions which should be useful in most programs
  107. //--------------------------------------------------------
  108. DEFINE_EXC (ArgExc, BaseExc) // Invalid arguments to a function call
  109. DEFINE_EXC (LogicExc, BaseExc) // General error in a program's logic,
  110. // for example, a function was called
  111. // in a context where the call does
  112. // not make sense.
  113. DEFINE_EXC (InputExc, BaseExc) // Invalid input data, e.g. from a file
  114. DEFINE_EXC (IoExc, BaseExc) // Input or output operation failed
  115. DEFINE_EXC (MathExc, BaseExc) // Arithmetic exception; more specific
  116. // exceptions derived from this class
  117. // are defined in ExcMath.h
  118. DEFINE_EXC (ErrnoExc, BaseExc) // Base class for exceptions corresponding
  119. // to errno values (see errno.h); more
  120. // specific exceptions derived from this
  121. // class are defined in ExcErrno.h
  122. DEFINE_EXC (NoImplExc, BaseExc) // Missing method exception e.g. from a
  123. // call to a method that is only partially
  124. // or not at all implemented. A reminder
  125. // to lazy software people to get back
  126. // to work.
  127. DEFINE_EXC (NullExc, BaseExc) // A pointer is inappropriately null.
  128. DEFINE_EXC (TypeExc, BaseExc) // An object is an inappropriate type,
  129. // i.e. a dynamnic_cast failed.
  130. //----------------------------------------------------------------------
  131. // Stack-tracing support:
  132. //
  133. // setStackTracer(st)
  134. //
  135. // installs a stack-tracing routine, st, which will be called from
  136. // class BaseExc's constructor every time an exception derived from
  137. // BaseExc is thrown. The stack-tracing routine should return a
  138. // string that contains a printable representation of the program's
  139. // current call stack. This string will be stored in the BaseExc
  140. // object; the string is accesible via the BaseExc::stackTrace()
  141. // method.
  142. //
  143. // setStackTracer(0)
  144. //
  145. // removes the current stack tracing routine. When an exception
  146. // derived from BaseExc is thrown, the stack trace string stored
  147. // in the BaseExc object will be empty.
  148. //
  149. // stackTracer()
  150. //
  151. // returns a pointer to the current stack-tracing routine, or 0
  152. // if there is no current stack stack-tracing routine.
  153. //
  154. //----------------------------------------------------------------------
  155. typedef std::string (* StackTracer) ();
  156. void setStackTracer (StackTracer stackTracer);
  157. StackTracer stackTracer ();
  158. //-----------------
  159. // Inline functions
  160. //-----------------
  161. inline BaseExc &
  162. BaseExc::operator = (std::stringstream &s)
  163. {
  164. return assign (s);
  165. }
  166. inline BaseExc &
  167. BaseExc::operator += (std::stringstream &s)
  168. {
  169. return append (s);
  170. }
  171. inline BaseExc &
  172. BaseExc::assign (const char *s)
  173. {
  174. std::string::assign(s);
  175. return *this;
  176. }
  177. inline BaseExc &
  178. BaseExc::operator = (const char *s)
  179. {
  180. return assign(s);
  181. }
  182. inline BaseExc &
  183. BaseExc::append (const char *s)
  184. {
  185. std::string::append(s);
  186. return *this;
  187. }
  188. inline BaseExc &
  189. BaseExc::operator += (const char *s)
  190. {
  191. return append(s);
  192. }
  193. inline const std::string &
  194. BaseExc::stackTrace () const
  195. {
  196. return _stackTrace;
  197. }
  198. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  199. #pragma warning(default:4290)
  200. #endif
  201. } // namespace Iex
  202. #endif