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