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  {} \
119  exp name::name (const char* text) : base (text) \
120  {} \
121  exp name::name (const std::string& text) : base (text) \
122  {} \
123  exp name::name (std::string&& text) : base (std::move (text)) \
124  {} \
125  exp name::name (std::stringstream& text) : base (text) \
126  {} \
127  exp name::name (const name& other) : base (other) \
128  {} \
129  exp name::name (name&& other) noexcept : base (other) \
130  {} \
131  exp name& name::operator= (name& other) \
132  { \
133  base::operator= (other); \
134  return *this; \
135  } \
136  exp name& name::operator= (name&& other) noexcept \
137  { \
138  base::operator= (other); \
139  return *this; \
140  } \
141  exp name::~name () noexcept \
142  {}
143 
144 // For backward compatibility.
145 #define DEFINE_EXC(name, base) DEFINE_EXC_EXP (, name, base)
146 
147 //--------------------------------------------------------
148 // Some exceptions which should be useful in most programs
149 //--------------------------------------------------------
151  IEX_EXPORT, ArgExc, BaseExc) // Invalid arguments to a function call
152 
154  IEX_EXPORT, LogicExc, BaseExc) // General error in a program's logic,
155  // for example, a function was called
156  // in a context where the call does
157  // not make sense.
158 
160  IEX_EXPORT, InputExc, BaseExc) // Invalid input data, e.g. from a file
161 
162 DEFINE_EXC_EXP (IEX_EXPORT, IoExc, BaseExc) // Input or output operation failed
163 
165  IEX_EXPORT, MathExc, BaseExc) // Arithmetic exception; more specific
166  // exceptions derived from this class
167  // are defined in ExcMath.h
168 
170  IEX_EXPORT, ErrnoExc, BaseExc) // Base class for exceptions corresponding
171  // to errno values (see errno.h); more
172  // specific exceptions derived from this
173  // class are defined in ExcErrno.h
174 
176  IEX_EXPORT, NoImplExc, BaseExc) // Missing method exception e.g. from a
177  // call to a method that is only partially
178  // or not at all implemented. A reminder
179  // to lazy software people to get back
180  // to work.
181 
183  IEX_EXPORT, NullExc, BaseExc) // A pointer is inappropriately null.
184 
186  IEX_EXPORT, TypeExc, BaseExc) // An object is an inappropriate type,
187  // i.e. a dynamnic_cast failed.
188 
189 //----------------------------------------------------------------------
190 // Stack-tracing support:
191 //
192 // setStackTracer(st)
193 //
194 // installs a stack-tracing routine, st, which will be called from
195 // class BaseExc's constructor every time an exception derived from
196 // BaseExc is thrown. The stack-tracing routine should return a
197 // string that contains a printable representation of the program's
198 // current call stack. This string will be stored in the BaseExc
199 // object; the string is accessible via the BaseExc::stackTrace()
200 // method.
201 //
202 // setStackTracer(0)
203 //
204 // removes the current stack tracing routine. When an exception
205 // derived from BaseExc is thrown, the stack trace string stored
206 // in the BaseExc object will be empty.
207 //
208 // stackTracer()
209 //
210 // returns a pointer to the current stack-tracing routine, or 0
211 // if there is no current stack stack-tracing routine.
212 //
213 //----------------------------------------------------------------------
214 
215 typedef std::string (*StackTracer) ();
216 
217 IEX_EXPORT void setStackTracer (StackTracer stackTracer);
218 IEX_EXPORT StackTracer stackTracer ();
219 
221 
222 #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
#define IEX_EXPORT
Definition: IexExport.h:30
GLdouble s
Definition: glad.h:3009
LogicExc
Definition: IexBaseExc.h:154
OIIO_FORCEINLINE const vint4 & operator+=(vint4 &a, const vint4 &b)
Definition: simd.h:4512
BaseExc BaseExc ErrnoExc
Definition: IexBaseExc.h:170
BaseExc BaseExc BaseExc NullExc
Definition: IexBaseExc.h:183
BaseExc IoExc
Definition: IexBaseExc.h:162
#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