///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IEXBASEEXC_H37#define INCLUDED_IEXBASEEXC_H383940//----------------------------------------------------------41//42// A general exception base class, and a few43// useful exceptions derived from the base class.44//45//----------------------------------------------------------4647#include <string>48#include <exception>49#include <sstream>5051namespace Iex {5253#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER54// Tell MS VC++ to suppress exception specification warnings55#pragma warning(disable:4290)56#endif5758//-------------------------------59// Our most basic exception class60//-------------------------------6162class BaseExc: public std::string, public std::exception63{64public:6566//----------------------------67// Constructors and destructor68//----------------------------6970BaseExc (const char *s = 0) throw(); // std::string (s)71BaseExc (const std::string &s) throw(); // std::string (s)72BaseExc (std::stringstream &s) throw(); // std::string (s.str())7374BaseExc (const BaseExc &be) throw();75virtual ~BaseExc () throw ();7677//--------------------------------------------78// what() method -- e.what() returns e.c_str()79//--------------------------------------------8081virtual const char * what () const throw ();828384//--------------------------------------------------85// Convenient methods to change the exception's text86//--------------------------------------------------8788BaseExc & assign (std::stringstream &s); // assign (s.str())89BaseExc & operator = (std::stringstream &s);9091BaseExc & append (std::stringstream &s); // append (s.str())92BaseExc & operator += (std::stringstream &s);939495//--------------------------------------------------96// These methods from the base class get obscured by97// the definitions above.98//--------------------------------------------------99100BaseExc & assign (const char *s);101BaseExc & operator = (const char *s);102103BaseExc & append (const char *s);104BaseExc & operator += (const char *s);105106107//--------------------------------------------------108// Stack trace for the point at which the exception109// was thrown. The stack trace will be an empty110// string unless a working stack-tracing routine111// has been installed (see below, setStackTracer()).112//--------------------------------------------------113114const std::string & stackTrace () const;115116private:117118std::string _stackTrace;119};120121122//-----------------------------------------------------123// A macro to save typing when declararing an exception124// class derived directly or indirectly from BaseExc:125//-----------------------------------------------------126127#define DEFINE_EXC(name, base) \128class name: public base \129{ \130public: \131name (const char* text=0) throw(): base (text) {} \132name (const std::string &text) throw(): base (text) {} \133name (std::stringstream &text) throw(): base (text) {} \134};135136137//--------------------------------------------------------138// Some exceptions which should be useful in most programs139//--------------------------------------------------------140141DEFINE_EXC (ArgExc, BaseExc) // Invalid arguments to a function call142143DEFINE_EXC (LogicExc, BaseExc) // General error in a program's logic,144// for example, a function was called145// in a context where the call does146// not make sense.147148DEFINE_EXC (InputExc, BaseExc) // Invalid input data, e.g. from a file149150DEFINE_EXC (IoExc, BaseExc) // Input or output operation failed151152DEFINE_EXC (MathExc, BaseExc) // Arithmetic exception; more specific153// exceptions derived from this class154// are defined in ExcMath.h155156DEFINE_EXC (ErrnoExc, BaseExc) // Base class for exceptions corresponding157// to errno values (see errno.h); more158// specific exceptions derived from this159// class are defined in ExcErrno.h160161DEFINE_EXC (NoImplExc, BaseExc) // Missing method exception e.g. from a162// call to a method that is only partially163// or not at all implemented. A reminder164// to lazy software people to get back165// to work.166167DEFINE_EXC (NullExc, BaseExc) // A pointer is inappropriately null.168169DEFINE_EXC (TypeExc, BaseExc) // An object is an inappropriate type,170// i.e. a dynamnic_cast failed.171172173//----------------------------------------------------------------------174// Stack-tracing support:175//176// setStackTracer(st)177//178// installs a stack-tracing routine, st, which will be called from179// class BaseExc's constructor every time an exception derived from180// BaseExc is thrown. The stack-tracing routine should return a181// string that contains a printable representation of the program's182// current call stack. This string will be stored in the BaseExc183// object; the string is accesible via the BaseExc::stackTrace()184// method.185//186// setStackTracer(0)187//188// removes the current stack tracing routine. When an exception189// derived from BaseExc is thrown, the stack trace string stored190// in the BaseExc object will be empty.191//192// stackTracer()193//194// returns a pointer to the current stack-tracing routine, or 0195// if there is no current stack stack-tracing routine.196//197//----------------------------------------------------------------------198199typedef std::string (* StackTracer) ();200201void setStackTracer (StackTracer stackTracer);202StackTracer stackTracer ();203204205//-----------------206// Inline functions207//-----------------208209inline BaseExc &210BaseExc::operator = (std::stringstream &s)211{212return assign (s);213}214215216inline BaseExc &217BaseExc::operator += (std::stringstream &s)218{219return append (s);220}221222223inline BaseExc &224BaseExc::assign (const char *s)225{226std::string::assign(s);227return *this;228}229230231inline BaseExc &232BaseExc::operator = (const char *s)233{234return assign(s);235}236237238inline BaseExc &239BaseExc::append (const char *s)240{241std::string::append(s);242return *this;243}244245246inline BaseExc &247BaseExc::operator += (const char *s)248{249return append(s);250}251252253inline const std::string &254BaseExc::stackTrace () const255{256return _stackTrace;257}258259#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER260#pragma warning(default:4290)261#endif262263} // namespace Iex264265#endif266267268