1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
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.
13 #ifndef UEXCEPTION_H_18DE3EF55C4F00673268F0D66546AF5D
14 #define UEXCEPTION_H_18DE3EF55C4F00673268F0D66546AF5D
17 #ifndef WITHOUT_LIBSTDCPP
23 #ifdef WITHOUT_LIBSTDCPP // This code is copied from <exception>
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();
48 typedef uint32_t xfmt_t
;
53 xfmt_LibcException
= 12,
54 xfmt_FileException
= 13,
55 xfmt_StreamBoundsException
= 14
58 /// \class exception uexception.h ustl.h
59 /// \ingroup Exceptions
61 /// \brief Base class for exceptions, equivalent to std::exception.
63 #ifdef WITHOUT_LIBSTDCPP
66 class exception
: public std::exception
{
69 typedef const CBacktrace
& rcbktrace_t
;
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
); }
85 inline void set_format (xfmt_t fmt
) { m_Format
= fmt
; }
87 CBacktrace m_Backtrace
; ///< Backtrace of the throw point.
88 xfmt_t m_Format
; ///< Format of the exception's data.
91 /// \class bad_cast uexception.h ustl.h
92 /// \ingroup Exceptions
94 /// \brief Thrown to indicate a bad dynamic_cast usage.
96 class bad_cast
: public exception
{
98 inline explicit bad_cast (void) throw() : exception() {}
99 inline virtual const char* what (void) const throw() { return ("bad cast"); }
102 //----------------------------------------------------------------------
104 /// \class bad_alloc uexception.h ustl.h
105 /// \ingroup Exceptions
107 /// \brief Exception thrown on memory allocation failure by memblock::reserve.
109 #ifdef WITHOUT_LIBSTDCPP
110 class bad_alloc
: public exception
{
112 class bad_alloc
: public std::bad_alloc
, public exception
{
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;
122 size_t m_nBytesRequested
; ///< Number of bytes requested by the failed allocation.
125 /// \class libc_exception uexception.h ustl.h
126 /// \ingroup Exceptions
128 /// \brief Thrown when a libc function returns an error.
130 /// Contains an errno and description. This is a uSTL extension.
132 class libc_exception
: public exception
{
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;
143 intptr_t m_Errno
; ///< Error code returned by the failed operation.
144 const char* m_Operation
; ///< Name of the failed operation.
147 /// \class file_exception uexception.h ustl.h
148 /// \ingroup Exceptions
150 /// \brief File-related exceptions.
152 /// Contains the file name. This is a uSTL extension.
154 class file_exception
: public libc_exception
{
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;
163 char m_Filename
[PATH_MAX
]; ///< Name of the file causing the error.
166 /// \class stream_bounds_exception uexception.h ustl.h
167 /// \ingroup Exceptions
169 /// \brief Stream bounds checking.
171 /// Only thrown in debug builds unless you say otherwise in config.h
172 /// This is a uSTL extension.
174 class stream_bounds_exception
: public libc_exception
{
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;
183 const char* m_TypeName
;
189 const char* demangle_type_name (char* buf
, size_t bufSize
, size_t* pdmSize
= NULL
);