Mercurial > ecos
annotate packages/language/cxx/ustl/current/src/uexception.cpp @ 3152:4885e0722884
* include/*:
* src/*:
* tests/*:
* HISTORY:
* LICENSE: Updated to uSTL 1.6 sources.
| author | sergeig |
|---|---|
| date | Wed, 04 Apr 2012 18:51:57 +0000 |
| parents | 9e1cb7863bef |
| children |
| rev | line source |
|---|---|
| 2904 | 1 // This file is part of the uSTL library, an STL implementation. |
| 2 // | |
| 3152 | 3 // Copyright (c) 2005 by Mike Sharov <msharov@users.sourceforge.net> |
| 2904 | 4 // This file is free software, distributed under the MIT License. |
| 5 | |
| 6 #include "uexception.h" | |
| 7 #include "ustring.h" | |
| 8 #include "mistream.h" | |
| 9 #include "sostream.h" | |
| 10 #include "strmsize.h" | |
| 11 #include "uspecial.h" | |
| 12 #include <errno.h> | |
| 3152 | 13 |
| 2904 | 14 #if HAVE_CXXABI_H && WANT_NAME_DEMANGLING |
| 3152 | 15 extern "C" char* __cxa_demangle (const char* mangled_name, char* output_buffer, size_t* length, int* status); |
| 2904 | 16 #endif |
| 17 | |
| 18 namespace ustl { | |
| 19 | |
| 20 //---------------------------------------------------------------------- | |
| 21 | |
| 22 /// \brief Returns a descriptive error message. fmt="%s" | |
| 23 /// Overloads of this functions must set NULL as the default fmt | |
| 24 /// argument and handle that case to provide a default format string | |
| 25 /// in case the user does not have a localized one. The format | |
| 26 /// string should be shown in the documentation to not require | |
| 27 /// translators to look through code. Also, this function must | |
| 28 /// not throw anything, so you must wrap memory allocation routines | |
| 29 /// (like string::format, for instance) in a try{}catch(...){} block. | |
| 30 /// | |
| 31 void exception::info (string& msgbuf, const char*) const throw() | |
| 32 { | |
| 33 USTL_TRY { msgbuf.format ("%s", what()); } USTL_CATCH_ALL; | |
| 34 } | |
| 35 | |
| 36 /// Reads the exception from stream \p is. | |
| 37 void exception::read (istream& is) | |
| 38 { | |
|
2910
9e1cb7863bef
* src/bktrace.cpp: prevent inclusion of host header <execinfo.h> (eCos builds
jld
parents:
2904
diff
changeset
|
39 uint32_t stmSize = 0; |
| 2904 | 40 xfmt_t fmt = xfmt_Exception; |
| 41 is >> fmt >> stmSize >> m_Backtrace; | |
| 42 assert (fmt == m_Format && "The saved exception is of a different type."); | |
| 43 assert ((stmSize + 8) - exception::stream_size() <= is.remaining() && "The saved exception data is corrupt."); | |
| 44 m_Format = fmt; | |
| 45 } | |
| 46 | |
| 47 /// Writes the exception into stream \p os as an IFF chunk. | |
| 48 void exception::write (ostream& os) const | |
| 49 { | |
| 50 os << m_Format << uint32_t(stream_size() - 8) << m_Backtrace; | |
| 51 } | |
| 52 | |
| 53 /// Writes the exception as text into stream \p os. | |
| 54 void exception::text_write (ostringstream& os) const | |
| 55 { | |
| 56 USTL_TRY { | |
| 57 string buf; | |
| 58 info (buf); | |
| 59 os << buf; | |
| 60 } USTL_CATCH_ALL | |
| 61 } | |
| 62 | |
| 63 //---------------------------------------------------------------------- | |
| 64 | |
| 65 /// Initializes the empty object. \p nBytes is the size of the attempted allocation. | |
| 66 bad_alloc::bad_alloc (size_t nBytes) throw() | |
| 67 : ustl::exception(), | |
| 68 m_nBytesRequested (nBytes) | |
| 69 { | |
| 70 set_format (xfmt_BadAlloc); | |
| 71 } | |
| 72 | |
| 73 /// Returns a descriptive error message. fmt="failed to allocate %d bytes" | |
| 74 void bad_alloc::info (string& msgbuf, const char* fmt) const throw() | |
| 75 { | |
| 76 if (!fmt) fmt = "failed to allocate %d bytes"; | |
| 77 USTL_TRY { msgbuf.format (fmt, m_nBytesRequested); } USTL_CATCH_ALL | |
| 78 } | |
| 79 | |
| 80 /// Reads the exception from stream \p is. | |
| 81 void bad_alloc::read (istream& is) | |
| 82 { | |
| 83 ustl::exception::read (is); | |
| 84 is >> m_nBytesRequested; | |
| 85 } | |
| 86 | |
| 87 /// Writes the exception into stream \p os. | |
| 88 void bad_alloc::write (ostream& os) const | |
| 89 { | |
| 90 ustl::exception::write (os); | |
| 91 os << m_nBytesRequested; | |
| 92 } | |
| 93 | |
| 94 /// Returns the size of the written exception. | |
| 95 size_t bad_alloc::stream_size (void) const | |
| 96 { | |
| 97 return (ustl::exception::stream_size() + stream_size_of(m_nBytesRequested)); | |
| 98 } | |
| 99 | |
| 100 //---------------------------------------------------------------------- | |
| 101 | |
| 102 /// Initializes the empty object. \p operation is the function that returned the error code. | |
| 103 libc_exception::libc_exception (const char* operation) throw() | |
| 104 : exception(), | |
| 105 m_Errno (errno), | |
| 106 m_Operation (operation) | |
| 107 { | |
| 108 set_format (xfmt_LibcException); | |
| 109 } | |
| 110 | |
| 111 /// Copies object \p v. | |
| 112 libc_exception::libc_exception (const libc_exception& v) throw() | |
| 113 : exception (v), | |
| 114 m_Errno (v.m_Errno), | |
| 115 m_Operation (v.m_Operation) | |
| 116 { | |
| 117 } | |
| 118 | |
| 119 /// Copies object \p v. | |
| 120 const libc_exception& libc_exception::operator= (const libc_exception& v) | |
| 121 { | |
| 122 m_Errno = v.m_Errno; | |
| 123 m_Operation = v.m_Operation; | |
| 124 return (*this); | |
| 125 } | |
| 126 | |
| 127 /// Returns a descriptive error message. fmt="%s: %m" | |
| 128 void libc_exception::info (string& msgbuf, const char* fmt) const throw() | |
| 129 { | |
| 130 if (!fmt) fmt = "%s: %d %s"; | |
| 131 USTL_TRY { msgbuf.format (fmt, m_Operation, m_Errno, strerror(m_Errno)); } USTL_CATCH_ALL | |
| 132 } | |
| 133 | |
| 134 /// Reads the exception from stream \p is. | |
| 135 void libc_exception::read (istream& is) | |
| 136 { | |
| 137 exception::read (is); | |
| 138 is >> m_Errno >> m_Operation; | |
| 139 } | |
| 140 | |
| 141 /// Writes the exception into stream \p os. | |
| 142 void libc_exception::write (ostream& os) const | |
| 143 { | |
| 144 exception::write (os); | |
| 145 os << m_Errno << m_Operation; | |
| 146 } | |
| 147 | |
| 148 /// Returns the size of the written exception. | |
| 149 size_t libc_exception::stream_size (void) const | |
| 150 { | |
| 151 return (exception::stream_size() + | |
| 152 stream_size_of(m_Errno) + | |
| 153 stream_size_of(m_Operation)); | |
| 154 } | |
| 155 | |
| 156 //---------------------------------------------------------------------- | |
| 157 | |
| 158 /// Initializes the empty object. \p operation is the function that returned the error code. | |
| 159 file_exception::file_exception (const char* operation, const char* filename) throw() | |
| 160 : libc_exception (operation) | |
| 161 { | |
| 162 memset (m_Filename, 0, VectorSize(m_Filename)); | |
| 163 set_format (xfmt_FileException); | |
| 164 if (filename) { | |
| 165 strncpy (m_Filename, filename, VectorSize(m_Filename)); | |
| 166 m_Filename [VectorSize(m_Filename) - 1] = 0; | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 /// Returns a descriptive error message. fmt="%s %s: %m" | |
| 171 void file_exception::info (string& msgbuf, const char* fmt) const throw() | |
| 172 { | |
| 173 if (!fmt) fmt = "%s %s: %d %s"; | |
| 174 USTL_TRY { msgbuf.format (fmt, m_Operation, m_Filename, m_Errno, strerror(m_Errno)); } USTL_CATCH_ALL | |
| 175 } | |
| 176 | |
| 177 /// Reads the exception from stream \p is. | |
| 178 void file_exception::read (istream& is) | |
| 179 { | |
| 180 libc_exception::read (is); | |
| 181 string filename; | |
| 182 is >> filename; | |
| 183 is.align (8); | |
| 184 filename.copyto (filename, VectorSize(m_Filename)); | |
| 185 } | |
| 186 | |
| 187 /// Writes the exception into stream \p os. | |
| 188 void file_exception::write (ostream& os) const | |
| 189 { | |
| 190 libc_exception::write (os); | |
| 191 os << string (m_Filename); | |
| 192 os.align (8); | |
| 193 } | |
| 194 | |
| 195 /// Returns the size of the written exception. | |
| 196 size_t file_exception::stream_size (void) const | |
| 197 { | |
| 198 return (libc_exception::stream_size() + | |
| 199 Align (stream_size_of (string (m_Filename)), 8)); | |
| 200 } | |
| 201 | |
| 202 //---------------------------------------------------------------------- | |
| 203 | |
| 204 /// \brief Uses C++ ABI call, if available to demangle the contents of \p buf. | |
| 205 /// | |
| 206 /// The result is written to \p buf, with the maximum size of \p bufSize, and | |
| 207 /// is zero-terminated. The return value is \p buf. | |
| 208 /// | |
| 209 const char* demangle_type_name (char* buf, size_t bufSize, size_t* pdmSize) | |
| 210 { | |
| 211 size_t bl = strlen (buf); | |
| 212 #if HAVE_CXXABI_H && WANT_NAME_DEMANGLING | |
| 213 char dmname [256]; | |
| 214 size_t sz = VectorSize(dmname); | |
| 215 int bFailed; | |
| 3152 | 216 __cxa_demangle (buf, dmname, &sz, &bFailed); |
| 2904 | 217 if (!bFailed) { |
| 218 bl = min (strlen (dmname), bufSize - 1); | |
| 219 memcpy (buf, dmname, bl); | |
| 220 buf[bl] = 0; | |
| 221 } | |
| 222 #else | |
| 223 bl = min (bl, bufSize); | |
| 224 #endif | |
| 225 if (pdmSize) | |
| 226 *pdmSize = bl; | |
| 227 return (buf); | |
| 228 } | |
| 229 | |
| 230 //---------------------------------------------------------------------- | |
| 231 | |
| 232 /// Initializes the empty object. \p operation is the function that returned the error code. | |
| 233 stream_bounds_exception::stream_bounds_exception (const char* operation, const char* type, uoff_t offset, size_t expected, size_t remaining) throw() | |
| 234 : libc_exception (operation), | |
| 235 m_TypeName (type), | |
| 236 m_Offset (offset), | |
| 237 m_Expected (expected), | |
| 238 m_Remaining (remaining) | |
| 239 { | |
| 240 set_format (xfmt_StreamBoundsException); | |
| 241 } | |
| 242 | |
| 243 /// Returns a descriptive error message. fmt="%s stream %s: @%u: expected %u, available %u"; | |
| 244 void stream_bounds_exception::info (string& msgbuf, const char* fmt) const throw() | |
| 245 { | |
| 246 char typeName [256]; | |
| 247 strncpy (typeName, m_TypeName, VectorSize(typeName)); | |
| 248 typeName[VectorSize(typeName)-1] = 0; | |
| 249 if (!fmt) fmt = "%s stream %s: @0x%X: need %u bytes, have %u"; | |
| 250 USTL_TRY { msgbuf.format (fmt, demangle_type_name (VectorBlock(typeName)), m_Operation, m_Offset, m_Expected, m_Remaining); } USTL_CATCH_ALL | |
| 251 } | |
| 252 | |
| 253 /// Reads the exception from stream \p is. | |
| 254 void stream_bounds_exception::read (istream& is) | |
| 255 { | |
| 256 libc_exception::read (is); | |
| 257 is >> m_TypeName >> m_Offset >> m_Expected >> m_Remaining; | |
| 258 } | |
| 259 | |
| 260 /// Writes the exception into stream \p os. | |
| 261 void stream_bounds_exception::write (ostream& os) const | |
| 262 { | |
| 263 libc_exception::write (os); | |
| 264 os << m_TypeName << m_Offset << m_Expected << m_Remaining; | |
| 265 } | |
| 266 | |
| 267 /// Returns the size of the written exception. | |
| 268 size_t stream_bounds_exception::stream_size (void) const | |
| 269 { | |
| 270 return (libc_exception::stream_size() + | |
| 271 stream_size_of(m_TypeName) + | |
| 272 stream_size_of(m_Offset) + | |
| 273 stream_size_of(m_Expected) + | |
| 274 stream_size_of(m_Remaining)); | |
| 275 } | |
| 276 | |
| 277 } // namespace ustl |
