1 // This file is part of the ustl library, an STL implementation. 2 // 3 // Copyright (C) 2005 by Mike Sharov <msharov (at) users.sourceforge.net> 4 // This file is free software, distributed under the MIT License. 5 // 6 // uexception.h 7 // 8 // This file contains stuff from \<exception\>. 9 // The standard C++ headers are duplicated because uSTL is intended 10 // to completely replace all C++ standard library functions. 11 // 12 13 #ifndef UEXCEPTION_H_18DE3EF55C4F00673268F0D66546AF5D 14 #define UEXCEPTION_H_18DE3EF55C4F00673268F0D66546AF5D 15 16 #include "utypes.h" 17 #ifndef WITHOUT_LIBSTDCPP 18 #include <exception> 19 #include <new> 20 #endif 21 #include "bktrace.h" 22 23 #ifdef WITHOUT_LIBSTDCPP // This code is copied from <exception> 24 namespace std { 25 /// If you write a replacement terminate handler, it must be of this type. 26 typedef void (*terminate_handler) (void); 27 /// If you write a replacement unexpected handler, it must be of this type. 28 typedef void (*unexpected_handler) (void); 29 /// Takes a new handler function as an argument, returns the old function. 30 terminate_handler set_terminate (terminate_handler pHandler) throw(); 31 /// The runtime will call this function if exception handling must be 32 /// abandoned for any reason. It can also be called by the user. 33 void terminate (void) __attribute__ ((__noreturn__)); 34 /// Takes a new handler function as an argument, returns the old function. 35 unexpected_handler set_unexpected (unexpected_handler pHandler) throw(); 36 /// The runtime will call this function if an exception is thrown which 37 /// violates the function's exception specification. 38 void unexpected (void) __attribute__ ((__noreturn__)); 39 /// Returns true when the caught exception violates the throw specification. 40 bool uncaught_exception() throw(); 41 } // namespace std 42 #endif 43 44 namespace ustl { 45 46 class string; 47 48 typedef uint32_t xfmt_t; 49 50 enum { 51 xfmt_Exception, 52 xfmt_BadAlloc, 53 xfmt_LibcException = 12, 54 xfmt_FileException = 13, 55 xfmt_StreamBoundsException = 14 56 }; 57 58 /// \class exception uexception.h ustl.h 59 /// \ingroup Exceptions 60 /// 61 /// \brief Base class for exceptions, equivalent to std::exception. 62 /// 63 #ifdef WITHOUT_LIBSTDCPP 64 class exception { 65 #else 66 class exception : public std::exception { 67 #endif 68 public: 69 typedef const CBacktrace& rcbktrace_t; 70 public: 71 inline exception (void) throw() : m_Format (xfmt_Exception) {} 72 inline virtual ~exception (void) throw() {} 73 inline virtual const char* what (void) const throw() { return ("error"); } 74 virtual void info (string& msgbuf, const char* fmt = NULL) const throw(); 75 virtual void read (istream& is); 76 virtual void write (ostream& os) const; 77 void text_write (ostringstream& os) const; 78 inline virtual size_t stream_size (void) const { return (sizeof(m_Format) + sizeof(uint32_t) + m_Backtrace.stream_size()); } 79 /// Format of the exception is used to lookup exception::info format string. 80 /// Another common use is the instantiation of serialized exceptions, used 81 /// by the error handler node chain to troubleshoot specific errors. 82 inline xfmt_t format (void) const { return (m_Format); } 83 inline rcbktrace_t backtrace (void) const { return (m_Backtrace); } 84 protected: 85 inline void set_format (xfmt_t fmt) { m_Format = fmt; } 86 private: 87 CBacktrace m_Backtrace; ///< Backtrace of the throw point. 88 xfmt_t m_Format; ///< Format of the exception's data. 89 }; 90 91 /// \class bad_cast uexception.h ustl.h 92 /// \ingroup Exceptions 93 /// 94 /// \brief Thrown to indicate a bad dynamic_cast usage. 95 /// 96 class bad_cast : public exception { 97 public: 98 inline explicit bad_cast (void) throw() : exception() {} 99 inline virtual const char* what (void) const throw() { return ("bad cast"); } 100 }; 101 102 //---------------------------------------------------------------------- 103 104 /// \class bad_alloc uexception.h ustl.h 105 /// \ingroup Exceptions 106 /// 107 /// \brief Exception thrown on memory allocation failure by memblock::reserve. 108 /// 109 #ifdef WITHOUT_LIBSTDCPP 110 class bad_alloc : public exception { 111 #else 112 class bad_alloc : public std::bad_alloc, public exception { 113 #endif 114 public: 115 explicit bad_alloc (size_t nBytes = 0) throw(); 116 inline virtual const char* what (void) const throw() { return ("memory allocation failed"); } 117 virtual void info (string& msgbuf, const char* fmt = NULL) const throw(); 118 virtual void read (istream& is); 119 virtual void write (ostream& os) const; 120 virtual size_t stream_size (void) const; 121 protected: 122 size_t m_nBytesRequested; ///< Number of bytes requested by the failed allocation. 123 }; 124 125 /// \class libc_exception uexception.h ustl.h 126 /// \ingroup Exceptions 127 /// 128 /// \brief Thrown when a libc function returns an error. 129 /// 130 /// Contains an errno and description. This is a uSTL extension. 131 /// 132 class libc_exception : public exception { 133 public: 134 explicit libc_exception (const char* operation) throw(); 135 libc_exception (const libc_exception& v) throw(); 136 const libc_exception& operator= (const libc_exception& v); 137 inline virtual const char* what (void) const throw() { return ("libc function failed"); } 138 virtual void info (string& msgbuf, const char* fmt = NULL) const throw(); 139 virtual void read (istream& is); 140 virtual void write (ostream& os) const; 141 virtual size_t stream_size (void) const; 142 protected: 143 intptr_t m_Errno; ///< Error code returned by the failed operation. 144 const char* m_Operation; ///< Name of the failed operation. 145 }; 146 147 /// \class file_exception uexception.h ustl.h 148 /// \ingroup Exceptions 149 /// 150 /// \brief File-related exceptions. 151 /// 152 /// Contains the file name. This is a uSTL extension. 153 /// 154 class file_exception : public libc_exception { 155 public: 156 file_exception (const char* operation, const char* filename) throw(); 157 inline virtual const char* what (void) const throw() { return ("file error"); } 158 virtual void info (string& msgbuf, const char* fmt = NULL) const throw(); 159 virtual void read (istream& is); 160 virtual void write (ostream& os) const; 161 virtual size_t stream_size (void) const; 162 protected: 163 char m_Filename [PATH_MAX]; ///< Name of the file causing the error. 164 }; 165 166 /// \class stream_bounds_exception uexception.h ustl.h 167 /// \ingroup Exceptions 168 /// 169 /// \brief Stream bounds checking. 170 /// 171 /// Only thrown in debug builds unless you say otherwise in config.h 172 /// This is a uSTL extension. 173 /// 174 class stream_bounds_exception : public libc_exception { 175 public: 176 stream_bounds_exception (const char* operation, const char* type, uoff_t offset, size_t expected, size_t remaining) throw(); 177 inline virtual const char* what (void) const throw() { return ("stream bounds exception"); } 178 virtual void info (string& msgbuf, const char* fmt = NULL) const throw(); 179 virtual void read (istream& is); 180 virtual void write (ostream& os) const; 181 virtual size_t stream_size (void) const; 182 protected: 183 const char* m_TypeName; 184 uoff_t m_Offset; 185 size_t m_Expected; 186 size_t m_Remaining; 187 }; 188 189 const char* demangle_type_name (char* buf, size_t bufSize, size_t* pdmSize = NULL); 190 191 } // namespace ustl 192 193 #endif 194 195