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

source code of include/OpenEXR/IexBaseExc.h