Added static_assert from Loki
[ustl.git] / uexception.cc
blob25efdb4edada65de4d0f2b89c6b47201be479af7
1 // This file is part of the ustl library, an STL implementation.
2 //
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // uexception.cc
7 //
9 #include "uexception.h"
10 #include "ustring.h"
11 #include "mistream.h"
12 #include "sostream.h"
13 #include "strmsize.h"
14 #include "uspecial.h"
15 #include <errno.h>
16 #if __GNUC__ >= 3
17 #include <cxxabi.h>
18 #endif
20 namespace ustl {
22 //----------------------------------------------------------------------
24 /// \brief Returns a descriptive error message. fmt="%s"
25 /// Overloads of this functions must set NULL as the default fmt
26 /// argument and handle that case to provide a default format string
27 /// in case the user does not have a localized one. The format
28 /// string should be shown in the documentation to not require
29 /// translators to look through code. Also, this function must
30 /// not throw anything, so you must wrap memory allocation routines
31 /// (like string::format, for instance) in a try{}catch(...){} block.
32 ///
33 void exception::info (string& msgbuf, const char*) const throw()
35 try { msgbuf.format ("%s", what()); } catch (...) { /* Ignore all exceptions */ }
38 /// Reads the exception from stream \p is.
39 void exception::read (istream& is)
41 uint32_t stmSize;
42 xfmt_t fmt;
43 is >> fmt >> stmSize >> m_Backtrace;
44 assert (fmt == m_Format && "The saved exception is of a different type.");
45 assert ((stmSize + 8) - exception::stream_size() <= is.remaining() && "The saved exception data is corrupt.");
46 m_Format = fmt;
49 /// Writes the exception into stream \p os as an IFF chunk.
50 void exception::write (ostream& os) const
52 os << m_Format << uint32_t(stream_size() - 8) << m_Backtrace;
55 /// Writes the exception as text into stream \p os.
56 void exception::text_write (ostringstream& os) const
58 try {
59 string buf;
60 info (buf);
61 os << buf;
62 } catch (...) {}
65 //----------------------------------------------------------------------
67 /// Initializes the empty object. \p nBytes is the size of the attempted allocation.
68 bad_alloc::bad_alloc (size_t nBytes) throw()
69 : ustl::exception(),
70 m_nBytesRequested (nBytes)
72 set_format (xfmt_BadAlloc);
75 /// Returns a descriptive error message. fmt="failed to allocate %d bytes"
76 void bad_alloc::info (string& msgbuf, const char* fmt) const throw()
78 if (!fmt) fmt = "failed to allocate %d bytes";
79 try { msgbuf.format (fmt, m_nBytesRequested); } catch (...) {}
82 /// Reads the exception from stream \p is.
83 void bad_alloc::read (istream& is)
85 ustl::exception::read (is);
86 is >> m_nBytesRequested;
89 /// Writes the exception into stream \p os.
90 void bad_alloc::write (ostream& os) const
92 ustl::exception::write (os);
93 os << m_nBytesRequested;
96 /// Returns the size of the written exception.
97 size_t bad_alloc::stream_size (void) const
99 return (ustl::exception::stream_size() + stream_size_of(m_nBytesRequested));
102 //----------------------------------------------------------------------
104 /// Initializes the empty object. \p operation is the function that returned the error code.
105 libc_exception::libc_exception (const char* operation) throw()
106 : exception(),
107 m_Errno (errno),
108 m_Operation (operation)
110 set_format (xfmt_LibcException);
113 /// Copies object \p v.
114 libc_exception::libc_exception (const libc_exception& v) throw()
115 : exception (v),
116 m_Errno (v.m_Errno),
117 m_Operation (v.m_Operation)
121 /// Copies object \p v.
122 const libc_exception& libc_exception::operator= (const libc_exception& v)
124 m_Errno = v.m_Errno;
125 m_Operation = v.m_Operation;
126 return (*this);
129 /// Returns a descriptive error message. fmt="%s: %m"
130 void libc_exception::info (string& msgbuf, const char* fmt) const throw()
132 if (!fmt) fmt = "%s: %m";
133 try { msgbuf.format (fmt, m_Operation, m_Errno, m_Errno); } catch (...) {}
136 /// Reads the exception from stream \p is.
137 void libc_exception::read (istream& is)
139 exception::read (is);
140 is >> m_Errno >> m_Operation;
143 /// Writes the exception into stream \p os.
144 void libc_exception::write (ostream& os) const
146 exception::write (os);
147 os << m_Errno << m_Operation;
150 /// Returns the size of the written exception.
151 size_t libc_exception::stream_size (void) const
153 return (exception::stream_size() +
154 stream_size_of(m_Errno) +
155 stream_size_of(m_Operation));
158 //----------------------------------------------------------------------
160 /// Initializes the empty object. \p operation is the function that returned the error code.
161 file_exception::file_exception (const char* operation, const char* filename) throw()
162 : libc_exception (operation)
164 memset (m_Filename, 0, VectorSize(m_Filename));
165 set_format (xfmt_FileException);
166 if (filename) {
167 strncpy (m_Filename, filename, VectorSize(m_Filename));
168 m_Filename [VectorSize(m_Filename) - 1] = 0;
172 /// Returns a descriptive error message. fmt="%s %s: %m"
173 void file_exception::info (string& msgbuf, const char* fmt) const throw()
175 if (!fmt) fmt = "%s %s: %m";
176 try { msgbuf.format (fmt, m_Operation, m_Filename, m_Errno, m_Errno); } catch (...) {}
179 /// Reads the exception from stream \p is.
180 void file_exception::read (istream& is)
182 libc_exception::read (is);
183 string filename;
184 is >> filename;
185 is.align (8);
186 filename.copyto (filename, VectorSize(m_Filename));
189 /// Writes the exception into stream \p os.
190 void file_exception::write (ostream& os) const
192 libc_exception::write (os);
193 os << string (m_Filename);
194 os.align (8);
197 /// Returns the size of the written exception.
198 size_t file_exception::stream_size (void) const
200 return (libc_exception::stream_size() +
201 Align (stream_size_of (string (m_Filename)), 8));
204 //----------------------------------------------------------------------
206 /// \brief Uses C++ ABI call, if available to demangle the contents of \p buf.
208 /// The result is written to \p buf, with the maximum size of \p bufSize, and
209 /// is zero-terminated. The return value is \p buf.
211 const char* demangle_type_name (char* buf, size_t bufSize, size_t* pdmSize)
213 size_t bl = strlen (buf);
214 #if __GNUC__ >= 3
215 char dmname [256];
216 size_t sz = VectorSize(dmname);
217 int bFailed;
218 abi::__cxa_demangle (buf, dmname, &sz, &bFailed);
219 if (!bFailed) {
220 bl = min (strlen (dmname), bufSize - 1);
221 memcpy (buf, dmname, bl);
222 buf[bl] = 0;
224 #else
225 bl = min (bl, bufSize);
226 #endif
227 if (pdmSize)
228 *pdmSize = bl;
229 return (buf);
232 //----------------------------------------------------------------------
234 /// Initializes the empty object. \p operation is the function that returned the error code.
235 stream_bounds_exception::stream_bounds_exception (const char* operation, const char* type, uoff_t offset, size_t expected, size_t remaining) throw()
236 : libc_exception (operation),
237 m_TypeName (type),
238 m_Offset (offset),
239 m_Expected (expected),
240 m_Remaining (remaining)
242 set_format (xfmt_StreamBoundsException);
245 /// Returns a descriptive error message. fmt="%s stream %s: @%u: expected %u, available %u";
246 void stream_bounds_exception::info (string& msgbuf, const char* fmt) const throw()
248 char typeName [256];
249 strncpy (typeName, m_TypeName, VectorSize(typeName));
250 typeName[VectorSize(typeName)-1] = 0;
251 if (!fmt) fmt = "%s stream %s: @0x%X: need %u bytes, have %u";
252 try { msgbuf.format (fmt, demangle_type_name (VectorBlock(typeName)), m_Operation, m_Offset, m_Expected, m_Remaining); } catch (...) {}
255 /// Reads the exception from stream \p is.
256 void stream_bounds_exception::read (istream& is)
258 libc_exception::read (is);
259 is >> m_TypeName >> m_Offset >> m_Expected >> m_Remaining;
262 /// Writes the exception into stream \p os.
263 void stream_bounds_exception::write (ostream& os) const
265 libc_exception::write (os);
266 os << m_TypeName << m_Offset << m_Expected << m_Remaining;
269 /// Returns the size of the written exception.
270 size_t stream_bounds_exception::stream_size (void) const
272 return (libc_exception::stream_size() +
273 stream_size_of(m_TypeName) +
274 stream_size_of(m_Offset) +
275 stream_size_of(m_Expected) +
276 stream_size_of(m_Remaining));
279 } // namespace ustl