IexBaseExc.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IEXBASEEXC_H
  6. #define INCLUDED_IEXBASEEXC_H
  7. #include "IexNamespace.h"
  8. #include "IexExport.h"
  9. //----------------------------------------------------------
  10. //
  11. // A general exception base class, and a few
  12. // useful exceptions derived from the base class.
  13. //
  14. //----------------------------------------------------------
  15. #include <string>
  16. #include <exception>
  17. #include <sstream>
  18. IEX_INTERNAL_NAMESPACE_HEADER_ENTER
  19. //-------------------------------
  20. // Our most basic exception class
  21. //-------------------------------
  22. class IEX_EXPORT_TYPE BaseExc: public std::exception
  23. {
  24. public:
  25. //----------------------------
  26. // Constructors and destructor
  27. //----------------------------
  28. IEX_EXPORT BaseExc (const char *s = nullptr);
  29. IEX_EXPORT BaseExc (const std::string &s);
  30. IEX_EXPORT BaseExc (std::string &&s); // not noexcept because of stacktrace
  31. IEX_EXPORT BaseExc (std::stringstream &s);
  32. IEX_EXPORT BaseExc (const BaseExc &be);
  33. IEX_EXPORT BaseExc (BaseExc &&be) noexcept;
  34. IEX_EXPORT virtual ~BaseExc () noexcept;
  35. IEX_EXPORT BaseExc & operator = (const BaseExc& be);
  36. IEX_EXPORT BaseExc & operator = (BaseExc&& be) noexcept;
  37. //---------------------------------------------------
  38. // what() method -- e.what() returns _message.c_str()
  39. //---------------------------------------------------
  40. IEX_EXPORT virtual const char * what () const noexcept;
  41. //--------------------------------------------------
  42. // Convenient methods to change the exception's text
  43. //--------------------------------------------------
  44. IEX_EXPORT BaseExc & assign (std::stringstream &s); // assign (s.str())
  45. IEX_EXPORT BaseExc & operator = (std::stringstream &s);
  46. IEX_EXPORT BaseExc & append (std::stringstream &s); // append (s.str())
  47. IEX_EXPORT BaseExc & operator += (std::stringstream &s);
  48. //--------------------------------------------------
  49. // These methods from the base class get obscured by
  50. // the definitions above.
  51. //--------------------------------------------------
  52. IEX_EXPORT BaseExc & assign (const char *s);
  53. IEX_EXPORT BaseExc & operator = (const char *s);
  54. IEX_EXPORT BaseExc & append (const char *s);
  55. IEX_EXPORT BaseExc & operator += (const char *s);
  56. //---------------------------------------------------
  57. // Access to the string representation of the message
  58. //---------------------------------------------------
  59. IEX_EXPORT const std::string & message () const noexcept;
  60. //--------------------------------------------------
  61. // Stack trace for the point at which the exception
  62. // was thrown. The stack trace will be an empty
  63. // string unless a working stack-tracing routine
  64. // has been installed (see below, setStackTracer()).
  65. //--------------------------------------------------
  66. IEX_EXPORT const std::string & stackTrace () const noexcept;
  67. private:
  68. std::string _message;
  69. std::string _stackTrace;
  70. };
  71. //-----------------------------------------------------
  72. // A macro to save typing when declararing an exception
  73. // class derived directly or indirectly from BaseExc:
  74. //-----------------------------------------------------
  75. #define DEFINE_EXC_EXP(exp, name, base) \
  76. class IEX_EXPORT_TYPE name: public base \
  77. { \
  78. public: \
  79. exp name(); \
  80. exp name (const char* text); \
  81. exp name (const std::string &text); \
  82. exp name (std::string &&text); \
  83. exp name (std::stringstream &text); \
  84. exp name (const name &other); \
  85. exp name (name &&other) noexcept; \
  86. exp name& operator = (name &other); \
  87. exp name& operator = (name &&other) noexcept; \
  88. exp ~name() noexcept; \
  89. };
  90. #define DEFINE_EXC_EXP_IMPL(exp, name, base) \
  91. exp name::name () : base () {} \
  92. exp name::name (const char* text) : base (text) {} \
  93. exp name::name (const std::string& text) : base (text) {} \
  94. exp name::name (std::string&& text) : base (std::move (text)) {} \
  95. exp name::name (std::stringstream& text) : base (text) {} \
  96. exp name::name (const name &other) : base (other) {} \
  97. exp name::name (name &&other) noexcept : base (other) {} \
  98. exp name& name::operator = (name &other) { base::operator=(other); return *this; } \
  99. exp name& name::operator = (name &&other) noexcept { base::operator=(other); return *this; } \
  100. exp name::~name () noexcept {}
  101. // For backward compatibility.
  102. #define DEFINE_EXC(name, base) DEFINE_EXC_EXP(, name, base)
  103. //--------------------------------------------------------
  104. // Some exceptions which should be useful in most programs
  105. //--------------------------------------------------------
  106. DEFINE_EXC_EXP (IEX_EXPORT, ArgExc, BaseExc) // Invalid arguments to a function call
  107. DEFINE_EXC_EXP (IEX_EXPORT, LogicExc, BaseExc) // General error in a program's logic,
  108. // for example, a function was called
  109. // in a context where the call does
  110. // not make sense.
  111. DEFINE_EXC_EXP (IEX_EXPORT, InputExc, BaseExc) // Invalid input data, e.g. from a file
  112. DEFINE_EXC_EXP (IEX_EXPORT, IoExc, BaseExc) // Input or output operation failed
  113. DEFINE_EXC_EXP (IEX_EXPORT, MathExc, BaseExc) // Arithmetic exception; more specific
  114. // exceptions derived from this class
  115. // are defined in ExcMath.h
  116. DEFINE_EXC_EXP (IEX_EXPORT, ErrnoExc, BaseExc) // Base class for exceptions corresponding
  117. // to errno values (see errno.h); more
  118. // specific exceptions derived from this
  119. // class are defined in ExcErrno.h
  120. DEFINE_EXC_EXP (IEX_EXPORT, NoImplExc, BaseExc) // Missing method exception e.g. from a
  121. // call to a method that is only partially
  122. // or not at all implemented. A reminder
  123. // to lazy software people to get back
  124. // to work.
  125. DEFINE_EXC_EXP (IEX_EXPORT, NullExc, BaseExc) // A pointer is inappropriately null.
  126. DEFINE_EXC_EXP (IEX_EXPORT, TypeExc, BaseExc) // An object is an inappropriate type,
  127. // i.e. a dynamnic_cast failed.
  128. //----------------------------------------------------------------------
  129. // Stack-tracing support:
  130. //
  131. // setStackTracer(st)
  132. //
  133. // installs a stack-tracing routine, st, which will be called from
  134. // class BaseExc's constructor every time an exception derived from
  135. // BaseExc is thrown. The stack-tracing routine should return a
  136. // string that contains a printable representation of the program's
  137. // current call stack. This string will be stored in the BaseExc
  138. // object; the string is accessible via the BaseExc::stackTrace()
  139. // method.
  140. //
  141. // setStackTracer(0)
  142. //
  143. // removes the current stack tracing routine. When an exception
  144. // derived from BaseExc is thrown, the stack trace string stored
  145. // in the BaseExc object will be empty.
  146. //
  147. // stackTracer()
  148. //
  149. // returns a pointer to the current stack-tracing routine, or 0
  150. // if there is no current stack stack-tracing routine.
  151. //
  152. //----------------------------------------------------------------------
  153. typedef std::string (* StackTracer) ();
  154. IEX_EXPORT void setStackTracer (StackTracer stackTracer);
  155. IEX_EXPORT StackTracer stackTracer ();
  156. IEX_INTERNAL_NAMESPACE_HEADER_EXIT
  157. #endif // INCLUDED_IEXBASEEXC_H