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