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