PBRT
|
00001 00002 // 00003 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas 00004 // Digital Ltd. LLC 00005 // 00006 // All rights reserved. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are 00010 // met: 00011 // * Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // * Redistributions in binary form must reproduce the above 00014 // copyright notice, this list of conditions and the following disclaimer 00015 // in the documentation and/or other materials provided with the 00016 // distribution. 00017 // * Neither the name of Industrial Light & Magic nor the names of 00018 // its contributors may be used to endorse or promote products derived 00019 // from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00034 00035 00036 00037 #ifndef INCLUDED_IEXBASEEXC_H 00038 #define INCLUDED_IEXBASEEXC_H 00039 00040 00041 //---------------------------------------------------------- 00042 // 00043 // A general exception base class, and a few 00044 // useful exceptions derived from the base class. 00045 // 00046 //---------------------------------------------------------- 00047 00048 #include <string> 00049 #include <exception> 00050 #include <sstream> 00051 00052 namespace Iex { 00053 00054 #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER 00055 // Tell MS VC++ to suppress exception specification warnings 00056 #pragma warning(disable:4290) 00057 #endif 00058 00059 //------------------------------- 00060 // Our most basic exception class 00061 //------------------------------- 00062 00063 class BaseExc: public std::string, public std::exception 00064 { 00065 public: 00066 00067 //---------------------------- 00068 // Constructors and destructor 00069 //---------------------------- 00070 00071 BaseExc (const char *s = 0) throw(); // std::string (s) 00072 BaseExc (const std::string &s) throw(); // std::string (s) 00073 BaseExc (std::stringstream &s) throw(); // std::string (s.str()) 00074 00075 BaseExc (const BaseExc &be) throw(); 00076 virtual ~BaseExc () throw (); 00077 00078 //-------------------------------------------- 00079 // what() method -- e.what() returns e.c_str() 00080 //-------------------------------------------- 00081 00082 virtual const char * what () const throw (); 00083 00084 00085 //-------------------------------------------------- 00086 // Convenient methods to change the exception's text 00087 //-------------------------------------------------- 00088 00089 BaseExc & assign (std::stringstream &s); // assign (s.str()) 00090 BaseExc & operator = (std::stringstream &s); 00091 00092 BaseExc & append (std::stringstream &s); // append (s.str()) 00093 BaseExc & operator += (std::stringstream &s); 00094 00095 00096 //-------------------------------------------------- 00097 // These methods from the base class get obscured by 00098 // the definitions above. 00099 //-------------------------------------------------- 00100 00101 BaseExc & assign (const char *s); 00102 BaseExc & operator = (const char *s); 00103 00104 BaseExc & append (const char *s); 00105 BaseExc & operator += (const char *s); 00106 00107 00108 //-------------------------------------------------- 00109 // Stack trace for the point at which the exception 00110 // was thrown. The stack trace will be an empty 00111 // string unless a working stack-tracing routine 00112 // has been installed (see below, setStackTracer()). 00113 //-------------------------------------------------- 00114 00115 const std::string & stackTrace () const; 00116 00117 private: 00118 00119 std::string _stackTrace; 00120 }; 00121 00122 00123 //----------------------------------------------------- 00124 // A macro to save typing when declararing an exception 00125 // class derived directly or indirectly from BaseExc: 00126 //----------------------------------------------------- 00127 00128 #define DEFINE_EXC(name, base) \ 00129 class name: public base \ 00130 { \ 00131 public: \ 00132 name (const char* text=0) throw(): base (text) {} \ 00133 name (const std::string &text) throw(): base (text) {} \ 00134 name (std::stringstream &text) throw(): base (text) {} \ 00135 }; 00136 00137 00138 //-------------------------------------------------------- 00139 // Some exceptions which should be useful in most programs 00140 //-------------------------------------------------------- 00141 00142 DEFINE_EXC (ArgExc, BaseExc) // Invalid arguments to a function call 00143 00144 DEFINE_EXC (LogicExc, BaseExc) // General error in a program's logic, 00145 // for example, a function was called 00146 // in a context where the call does 00147 // not make sense. 00148 00149 DEFINE_EXC (InputExc, BaseExc) // Invalid input data, e.g. from a file 00150 00151 DEFINE_EXC (IoExc, BaseExc) // Input or output operation failed 00152 00153 DEFINE_EXC (MathExc, BaseExc) // Arithmetic exception; more specific 00154 // exceptions derived from this class 00155 // are defined in ExcMath.h 00156 00157 DEFINE_EXC (ErrnoExc, BaseExc) // Base class for exceptions corresponding 00158 // to errno values (see errno.h); more 00159 // specific exceptions derived from this 00160 // class are defined in ExcErrno.h 00161 00162 DEFINE_EXC (NoImplExc, BaseExc) // Missing method exception e.g. from a 00163 // call to a method that is only partially 00164 // or not at all implemented. A reminder 00165 // to lazy software people to get back 00166 // to work. 00167 00168 DEFINE_EXC (NullExc, BaseExc) // A pointer is inappropriately null. 00169 00170 DEFINE_EXC (TypeExc, BaseExc) // An object is an inappropriate type, 00171 // i.e. a dynamnic_cast failed. 00172 00173 00174 //---------------------------------------------------------------------- 00175 // Stack-tracing support: 00176 // 00177 // setStackTracer(st) 00178 // 00179 // installs a stack-tracing routine, st, which will be called from 00180 // class BaseExc's constructor every time an exception derived from 00181 // BaseExc is thrown. The stack-tracing routine should return a 00182 // string that contains a printable representation of the program's 00183 // current call stack. This string will be stored in the BaseExc 00184 // object; the string is accesible via the BaseExc::stackTrace() 00185 // method. 00186 // 00187 // setStackTracer(0) 00188 // 00189 // removes the current stack tracing routine. When an exception 00190 // derived from BaseExc is thrown, the stack trace string stored 00191 // in the BaseExc object will be empty. 00192 // 00193 // stackTracer() 00194 // 00195 // returns a pointer to the current stack-tracing routine, or 0 00196 // if there is no current stack stack-tracing routine. 00197 // 00198 //---------------------------------------------------------------------- 00199 00200 typedef std::string (* StackTracer) (); 00201 00202 void setStackTracer (StackTracer stackTracer); 00203 StackTracer stackTracer (); 00204 00205 00206 //----------------- 00207 // Inline functions 00208 //----------------- 00209 00210 inline BaseExc & 00211 BaseExc::operator = (std::stringstream &s) 00212 { 00213 return assign (s); 00214 } 00215 00216 00217 inline BaseExc & 00218 BaseExc::operator += (std::stringstream &s) 00219 { 00220 return append (s); 00221 } 00222 00223 00224 inline BaseExc & 00225 BaseExc::assign (const char *s) 00226 { 00227 std::string::assign(s); 00228 return *this; 00229 } 00230 00231 00232 inline BaseExc & 00233 BaseExc::operator = (const char *s) 00234 { 00235 return assign(s); 00236 } 00237 00238 00239 inline BaseExc & 00240 BaseExc::append (const char *s) 00241 { 00242 std::string::append(s); 00243 return *this; 00244 } 00245 00246 00247 inline BaseExc & 00248 BaseExc::operator += (const char *s) 00249 { 00250 return append(s); 00251 } 00252 00253 00254 inline const std::string & 00255 BaseExc::stackTrace () const 00256 { 00257 return _stackTrace; 00258 } 00259 00260 #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER 00261 #pragma warning(default:4290) 00262 #endif 00263 00264 } // namespace Iex 00265 00266 #endif