summaryrefslogtreecommitdiff
path: root/Source/OpenEXR/Iex/IexBaseExc.h
blob: 9d8ab2428671d2019fd36d8dbc8a181825c769b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Industrial Light & Magic nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////



#ifndef INCLUDED_IEXBASEEXC_H
#define INCLUDED_IEXBASEEXC_H


//----------------------------------------------------------
//
//	A general exception base class, and a few
//	useful exceptions derived from the base class.
//
//----------------------------------------------------------

#include <string>
#include <exception>
#include <sstream>

namespace Iex {

#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
// Tell MS VC++ to suppress exception specification warnings
#pragma warning(disable:4290)
#endif

//-------------------------------
// Our most basic exception class
//-------------------------------

class BaseExc: public std::string, public std::exception
{
  public:

    //----------------------------
    // Constructors and destructor
    //----------------------------

    BaseExc (const char *s = 0) throw();	// std::string (s)
    BaseExc (const std::string &s) throw();	// std::string (s)
    BaseExc (std::stringstream &s) throw();	// std::string (s.str())

    BaseExc (const BaseExc &be) throw();
    virtual ~BaseExc () throw ();

    //--------------------------------------------
    // what() method -- e.what() returns e.c_str()
    //--------------------------------------------

    virtual const char * what () const throw ();


    //--------------------------------------------------
    // Convenient methods to change the exception's text
    //--------------------------------------------------

    BaseExc &		assign (std::stringstream &s);	// assign (s.str())
    BaseExc &		operator = (std::stringstream &s);

    BaseExc &		append (std::stringstream &s);	// append (s.str())
    BaseExc &		operator += (std::stringstream &s);


    //--------------------------------------------------
    // These methods from the base class get obscured by
    // the definitions above.
    //--------------------------------------------------

    BaseExc &		assign (const char *s);
    BaseExc &		operator = (const char *s);

    BaseExc &		append (const char *s);
    BaseExc &		operator += (const char *s);


    //--------------------------------------------------
    // Stack trace for the point at which the exception
    // was thrown.  The stack trace will be an empty
    // string unless a working stack-tracing routine
    // has been installed (see below, setStackTracer()).
    //--------------------------------------------------

    const std::string &	stackTrace () const;

  private:

    std::string		_stackTrace;
};


//-----------------------------------------------------
// A macro to save typing when declararing an exception
// class derived directly or indirectly from BaseExc:
//-----------------------------------------------------

#define DEFINE_EXC(name, base)				        \
    class name: public base				        \
    {							        \
      public:                                                   \
	name (const char* text=0)      throw(): base (text) {}	\
	name (const std::string &text) throw(): base (text) {}	\
	name (std::stringstream &text) throw(): base (text) {}	\
    };


//--------------------------------------------------------
// Some exceptions which should be useful in most programs
//--------------------------------------------------------

DEFINE_EXC (ArgExc,   BaseExc) 	 // Invalid arguments to a function call

DEFINE_EXC (LogicExc, BaseExc) 	 // General error in a program's logic,
				 // for example, a function was called
				 // in a context where the call does
				 // not make sense.

DEFINE_EXC (InputExc, BaseExc) 	 // Invalid input data, e.g. from a file

DEFINE_EXC (IoExc, BaseExc) 	 // Input or output operation failed

DEFINE_EXC (MathExc,  BaseExc) 	 // Arithmetic exception; more specific
				 // exceptions derived from this class
				 // are defined in ExcMath.h

DEFINE_EXC (ErrnoExc, BaseExc) 	 // Base class for exceptions corresponding
				 // to errno values (see errno.h); more
				 // specific exceptions derived from this
				 // class are defined in ExcErrno.h

DEFINE_EXC (NoImplExc, BaseExc)  // Missing method exception e.g. from a
				 // call to a method that is only partially
				 // or not at all implemented. A reminder
				 // to lazy software people to get back
				 // to work.

DEFINE_EXC (NullExc, BaseExc) 	 // A pointer is inappropriately null.

DEFINE_EXC (TypeExc, BaseExc) 	 // An object is an inappropriate type,
				 // i.e. a dynamnic_cast failed.


//----------------------------------------------------------------------
// Stack-tracing support:
// 
// setStackTracer(st)
//
//	installs a stack-tracing routine, st, which will be called from
//	class BaseExc's constructor every time an exception derived from
//	BaseExc is thrown.  The stack-tracing routine should return a
//	string that contains a printable representation of the program's
//	current call stack.  This string will be stored in the BaseExc
//	object; the string is accesible via the BaseExc::stackTrace()
//	method.
//
// setStackTracer(0)
//
//	removes the current stack tracing routine.  When an exception
//	derived from BaseExc is thrown, the stack trace string stored
//	in the BaseExc object will be empty.
//
// stackTracer()
//
//	returns a pointer to the current stack-tracing routine, or 0
//	if there is no current stack stack-tracing routine.
// 
//----------------------------------------------------------------------

typedef std::string (* StackTracer) ();

void		setStackTracer (StackTracer stackTracer);
StackTracer	stackTracer ();


//-----------------
// Inline functions
//-----------------

inline BaseExc &
BaseExc::operator = (std::stringstream &s)
{
    return assign (s);
}


inline BaseExc &
BaseExc::operator += (std::stringstream &s)
{
    return append (s);
}


inline BaseExc &
BaseExc::assign (const char *s)
{
    std::string::assign(s);
    return *this;
}


inline BaseExc &
BaseExc::operator = (const char *s)
{
    return assign(s);
}


inline BaseExc &
BaseExc::append (const char *s)
{
    std::string::append(s);
    return *this;
}


inline BaseExc &
BaseExc::operator += (const char *s)
{
    return append(s);
}


inline const std::string &
BaseExc::stackTrace () const
{
    return _stackTrace;
}

#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
#pragma warning(default:4290)
#endif

} // namespace Iex

#endif