OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [cxx/] [ustl/] [current/] [src/] [uexception.cpp] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.