1 | /////////////////////////////////////////////////////////////////////////// |
2 | // |
3 | // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas |
4 | // Digital Ltd. LLC |
5 | // |
6 | // All rights reserved. |
7 | // |
8 | // Redistribution and use in source and binary forms, with or without |
9 | // modification, are permitted provided that the following conditions are |
10 | // met: |
11 | // * Redistributions of source code must retain the above copyright |
12 | // notice, this list of conditions and the following disclaimer. |
13 | // * Redistributions in binary form must reproduce the above |
14 | // copyright notice, this list of conditions and the following disclaimer |
15 | // in the documentation and/or other materials provided with the |
16 | // distribution. |
17 | // * Neither the name of Industrial Light & Magic nor the names of |
18 | // its contributors may be used to endorse or promote products derived |
19 | // from this software without specific prior written permission. |
20 | // |
21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | // |
33 | /////////////////////////////////////////////////////////////////////////// |
34 | |
35 | |
36 | #ifndef INCLUDED_IEXBASEEXC_H |
37 | #define INCLUDED_IEXBASEEXC_H |
38 | |
39 | #include "IexNamespace.h" |
40 | #include "IexExport.h" |
41 | |
42 | //---------------------------------------------------------- |
43 | // |
44 | // A general exception base class, and a few |
45 | // useful exceptions derived from the base class. |
46 | // |
47 | //---------------------------------------------------------- |
48 | |
49 | #include <string> |
50 | #include <exception> |
51 | #include <sstream> |
52 | |
53 | IEX_INTERNAL_NAMESPACE_HEADER_ENTER |
54 | |
55 | |
56 | //------------------------------- |
57 | // Our most basic exception class |
58 | //------------------------------- |
59 | |
60 | class BaseExc: public std::string, public std::exception |
61 | { |
62 | public: |
63 | |
64 | //---------------------------- |
65 | // Constructors and destructor |
66 | //---------------------------- |
67 | |
68 | IEX_EXPORT BaseExc (const char *s = 0) throw(); // std::string (s) |
69 | IEX_EXPORT BaseExc (const std::string &s) throw(); // std::string (s) |
70 | IEX_EXPORT BaseExc (std::stringstream &s) throw(); // std::string (s.str()) |
71 | |
72 | IEX_EXPORT BaseExc (const BaseExc &be) throw(); |
73 | IEX_EXPORT virtual ~BaseExc () throw (); |
74 | |
75 | //-------------------------------------------- |
76 | // what() method -- e.what() returns e.c_str() |
77 | //-------------------------------------------- |
78 | |
79 | IEX_EXPORT virtual const char * what () const throw (); |
80 | |
81 | |
82 | //-------------------------------------------------- |
83 | // Convenient methods to change the exception's text |
84 | //-------------------------------------------------- |
85 | |
86 | IEX_EXPORT BaseExc & assign (std::stringstream &s); // assign (s.str()) |
87 | IEX_EXPORT BaseExc & operator = (std::stringstream &s); |
88 | |
89 | IEX_EXPORT BaseExc & append (std::stringstream &s); // append (s.str()) |
90 | IEX_EXPORT BaseExc & operator += (std::stringstream &s); |
91 | |
92 | |
93 | //-------------------------------------------------- |
94 | // These methods from the base class get obscured by |
95 | // the definitions above. |
96 | //-------------------------------------------------- |
97 | |
98 | IEX_EXPORT BaseExc & assign (const char *s); |
99 | IEX_EXPORT BaseExc & operator = (const char *s); |
100 | |
101 | IEX_EXPORT BaseExc & append (const char *s); |
102 | IEX_EXPORT BaseExc & operator += (const char *s); |
103 | |
104 | |
105 | //-------------------------------------------------- |
106 | // Stack trace for the point at which the exception |
107 | // was thrown. The stack trace will be an empty |
108 | // string unless a working stack-tracing routine |
109 | // has been installed (see below, setStackTracer()). |
110 | //-------------------------------------------------- |
111 | |
112 | IEX_EXPORT const std::string & stackTrace () const; |
113 | |
114 | private: |
115 | |
116 | std::string _stackTrace; |
117 | }; |
118 | |
119 | |
120 | //----------------------------------------------------- |
121 | // A macro to save typing when declararing an exception |
122 | // class derived directly or indirectly from BaseExc: |
123 | //----------------------------------------------------- |
124 | |
125 | #define DEFINE_EXC_EXP(exp, name, base) \ |
126 | class exp name: public base \ |
127 | { \ |
128 | public: \ |
129 | name() throw(): base (0) {} \ |
130 | name (const char* text) throw(): base (text) {} \ |
131 | name (const std::string &text) throw(): base (text) {} \ |
132 | name (std::stringstream &text) throw(): base (text) {} \ |
133 | ~name() throw() { } \ |
134 | }; |
135 | |
136 | // For backward compatibility. |
137 | #define DEFINE_EXC(name, base) DEFINE_EXC_EXP(, name, base) |
138 | |
139 | |
140 | //-------------------------------------------------------- |
141 | // Some exceptions which should be useful in most programs |
142 | //-------------------------------------------------------- |
143 | DEFINE_EXC_EXP (IEX_EXPORT, ArgExc, BaseExc) // Invalid arguments to a function call |
144 | |
145 | DEFINE_EXC_EXP (IEX_EXPORT, LogicExc, BaseExc) // General error in a program's logic, |
146 | // for example, a function was called |
147 | // in a context where the call does |
148 | // not make sense. |
149 | |
150 | DEFINE_EXC_EXP (IEX_EXPORT, InputExc, BaseExc) // Invalid input data, e.g. from a file |
151 | |
152 | DEFINE_EXC_EXP (IEX_EXPORT, IoExc, BaseExc) // Input or output operation failed |
153 | |
154 | DEFINE_EXC_EXP (IEX_EXPORT, MathExc, BaseExc) // Arithmetic exception; more specific |
155 | // exceptions derived from this class |
156 | // are defined in ExcMath.h |
157 | |
158 | DEFINE_EXC_EXP (IEX_EXPORT, ErrnoExc, BaseExc) // Base class for exceptions corresponding |
159 | // to errno values (see errno.h); more |
160 | // specific exceptions derived from this |
161 | // class are defined in ExcErrno.h |
162 | |
163 | DEFINE_EXC_EXP (IEX_EXPORT, NoImplExc, BaseExc) // Missing method exception e.g. from a |
164 | // call to a method that is only partially |
165 | // or not at all implemented. A reminder |
166 | // to lazy software people to get back |
167 | // to work. |
168 | |
169 | DEFINE_EXC_EXP (IEX_EXPORT, NullExc, BaseExc) // A pointer is inappropriately null. |
170 | |
171 | DEFINE_EXC_EXP (IEX_EXPORT, TypeExc, BaseExc) // An object is an inappropriate type, |
172 | // i.e. a dynamnic_cast failed. |
173 | |
174 | |
175 | //---------------------------------------------------------------------- |
176 | // Stack-tracing support: |
177 | // |
178 | // setStackTracer(st) |
179 | // |
180 | // installs a stack-tracing routine, st, which will be called from |
181 | // class BaseExc's constructor every time an exception derived from |
182 | // BaseExc is thrown. The stack-tracing routine should return a |
183 | // string that contains a printable representation of the program's |
184 | // current call stack. This string will be stored in the BaseExc |
185 | // object; the string is accesible via the BaseExc::stackTrace() |
186 | // method. |
187 | // |
188 | // setStackTracer(0) |
189 | // |
190 | // removes the current stack tracing routine. When an exception |
191 | // derived from BaseExc is thrown, the stack trace string stored |
192 | // in the BaseExc object will be empty. |
193 | // |
194 | // stackTracer() |
195 | // |
196 | // returns a pointer to the current stack-tracing routine, or 0 |
197 | // if there is no current stack stack-tracing routine. |
198 | // |
199 | //---------------------------------------------------------------------- |
200 | |
201 | typedef std::string (* StackTracer) (); |
202 | |
203 | IEX_EXPORT void setStackTracer (StackTracer stackTracer); |
204 | IEX_EXPORT StackTracer stackTracer (); |
205 | |
206 | |
207 | //----------------- |
208 | // Inline functions |
209 | //----------------- |
210 | |
211 | inline BaseExc & |
212 | BaseExc::operator = (std::stringstream &s) |
213 | { |
214 | return assign (s); |
215 | } |
216 | |
217 | |
218 | inline BaseExc & |
219 | BaseExc::operator += (std::stringstream &s) |
220 | { |
221 | return append (s); |
222 | } |
223 | |
224 | |
225 | inline BaseExc & |
226 | BaseExc::assign (const char *s) |
227 | { |
228 | std::string::assign(s); |
229 | return *this; |
230 | } |
231 | |
232 | |
233 | inline BaseExc & |
234 | BaseExc::operator = (const char *s) |
235 | { |
236 | return assign(s); |
237 | } |
238 | |
239 | |
240 | inline BaseExc & |
241 | BaseExc::append (const char *s) |
242 | { |
243 | std::string::append(s); |
244 | return *this; |
245 | } |
246 | |
247 | |
248 | inline BaseExc & |
249 | BaseExc::operator += (const char *s) |
250 | { |
251 | return append(s); |
252 | } |
253 | |
254 | |
255 | inline const std::string & |
256 | BaseExc::stackTrace () const |
257 | { |
258 | return _stackTrace; |
259 | } |
260 | |
261 | |
262 | IEX_INTERNAL_NAMESPACE_HEADER_EXIT |
263 | |
264 | #endif // INCLUDED_IEXBASEEXC_H |
265 | |