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