00001 ////////////////////////////////////////////////////////////// 00002 // $Id: EoaCore.hxx,v 1.5 2010/02/01 17:39:16 mcgrew Exp $ 00003 // 00004 #ifndef EoaCore_HXX_SEEN 00005 #define EoaCore_HXX_SEEN 00006 00007 #include <exception> 00008 #include <string> 00009 00010 00011 class EoaCore; 00012 00013 00014 /// The root exception for all exceptions explicitly thrown by the oaCore 00015 /// library. All exceptions declared by this library should be derived from 00016 /// this class, and not std::exception. New exceptions can be derived from 00017 /// EoaCore by creating a new class 00018 /// 00019 /// \code 00020 /// class EoaChild :public EoaCore { 00021 /// public: 00022 /// EoaChild() {AppendWhat("EoaChild");}; 00023 /// }; 00024 /// \endcode 00025 /// 00026 /// But the prefered way to create derived exception classes is to use the 00027 /// OA_EXCEPTION macro. This is used as follows 00028 /// 00029 /// \code 00030 /// OA_EXCEPTION(EChild,EoaCore); 00031 /// OA_EXCEPTION(EGrandChild,EChild); 00032 /// ... 00033 /// try { 00034 /// throw EChild(); 00035 /// } 00036 /// catch (EoaCore& ex) { 00037 /// std::cout << ex.what << std::endl; 00038 /// } 00039 /// \endcode 00040 /// 00041 /// This can add a backtrace at the point of the exception. The number of 00042 /// backtrace frames to be shown is controlled with the 00043 /// EoaCore::gBacktraceSymbols static variable. 00044 /// 00045 class EoaCore :public std::exception { 00046 /// What exception generated this object. 00047 char fWhat[2048]; 00048 00049 public: 00050 /// The number of backtrace symbols to add to the "what" string. 00051 static unsigned int gBacktraceSymbols; 00052 00053 EoaCore(); 00054 virtual ~EoaCore() throw() {} 00055 00056 /// Inherited from exception to return the name of the exception as a null 00057 /// terminated string. 00058 const char* what(void) const throw() {return fWhat;} 00059 00060 /// Used in constructors of classes which inherit EoaCore to add text to 00061 /// the What string. Here is a comprehensive example of how to construct 00062 /// an exception derived from EoaCore: 00063 /// \verbatim 00064 /// class EoaChild : public EoaCore { 00065 /// public: 00066 /// EoaChild() {AppendWhat("EoaChild");} 00067 /// }; 00068 /// \endverbatim 00069 void AppendWhat(const char* child); 00070 }; 00071 00072 /// A macro to build an exception class __name that is derived from __parent. 00073 /// The __parent class must be derived from EoaCore which provides the 00074 /// AppendWhat method. This macro builds an exception class that can be used: 00075 /// \verbatim 00076 /// OA_EXCEPTION(EChild,EoaCore); 00077 /// ... 00078 /// try { 00079 /// throw EChild(); 00080 /// } 00081 /// catch (EoaCore& ex) { 00082 /// std::cout << ex.what << std::endl; 00083 /// } 00084 /// \endverbatim 00085 #ifndef OA_EXCEPTION 00086 #define OA_EXCEPTION(__name,__parent) \ 00087 class __name : public __parent { \ 00088 public: \ 00089 __name() {AppendWhat(#__name);} \ 00090 } 00091 #endif 00092 #endif