File: Exceptions.h

package info (click to toggle)
pcsx2 1.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,464 kB
  • sloc: cpp: 299,796; ansic: 23,973; lisp: 2,689; asm: 908; perl: 852; sh: 789; xml: 116; makefile: 60
file content (392 lines) | stat: -rw-r--r-- 16,682 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*  PCSX2 - PS2 Emulator for PCs
 *  Copyright (C) 2002-2010  PCSX2 Dev Team
 *
 *  PCSX2 is free software: you can redistribute it and/or modify it under the terms
 *  of the GNU Lesser General Public License as published by the Free Software Found-
 *  ation, either version 3 of the License, or (at your option) any later version.
 *
 *  PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE.  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with PCSX2.
 *  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "Assertions.h"
#include <memory>

// Because wxTrap isn't available on Linux builds of wxWidgets (non-Debug, typically)
void pxTrap();

// --------------------------------------------------------------------------------------
//  DESTRUCTOR_CATCHALL - safe destructor helper
// --------------------------------------------------------------------------------------
// In C++ destructors *really* need to be "nothrow" garaunteed, otherwise you can have
// disasterous nested exception throws during the unwinding process of an originating
// exception.  Use this macro to dispose of these dangerous exceptions, and generate a
// friendly error log in their wake.
//
// Note: Console can also fire an Exception::OutOfMemory
#define __DESTRUCTOR_CATCHALL(funcname)                                            \
    catch (BaseException & ex)                                                     \
    {                                                                              \
        try {                                                                      \
            Console.Error("Unhandled BaseException in %s (ignored!):", funcname);  \
            Console.Error(ex.FormatDiagnosticMessage());                           \
        } catch (...) {                                                            \
            fprintf(stderr, "ERROR: (out of memory?)\n");                          \
        }                                                                          \
    }                                                                              \
    catch (std::exception & ex)                                                    \
    {                                                                              \
        try {                                                                      \
            Console.Error("Unhandled std::exception in %s (ignored!):", funcname); \
            Console.Error(ex.what());                                              \
        } catch (...) {                                                            \
            fprintf(stderr, "ERROR: (out of memory?)\n");                          \
        }                                                                          \
    }                                                                              \
    catch (...)                                                                    \
    {                                                                              \
        /* Unreachable code */                                                     \
    }

#define DESTRUCTOR_CATCHALL __DESTRUCTOR_CATCHALL(__pxFUNCTION__)

namespace Exception
{
int MakeNewType();
BaseException *FromErrno(const wxString &streamname, int errcode);

// --------------------------------------------------------------------------------------
//  BaseException
// --------------------------------------------------------------------------------------
// std::exception sucks, and isn't entirely cross-platform reliable in its implementation,
// so I made a replacement.  The internal messages are non-const, which means that a
// catch clause can optionally modify them and then re-throw to a top-level handler.
//
// Note, this class is "abstract" which means you shouldn't use it directly like, ever.
// Use Exception::RuntimeError instead for generic exceptions.
//
// Because exceptions are the (only!) really useful example of multiple inheritance,
// this class has only a trivial constructor, and must be manually initialized using
// InitBaseEx() or by individual member assignments.  This is because C++ multiple inheritence
// is, by design, a lot of fail, especially when class initializers are mixed in.
//
// [TODO] : Add an InnerException component, and Clone() facility.
//
class BaseException
{
protected:
    wxString m_message_diag; // (untranslated) a "detailed" message of what disastrous thing has occurred!
    wxString m_message_user; // (translated) a "detailed" message of what disastrous thing has occurred!

public:
    virtual ~BaseException() = default;

    const wxString &DiagMsg() const { return m_message_diag; }
    const wxString &UserMsg() const { return m_message_user; }

    wxString &DiagMsg() { return m_message_diag; }
    wxString &UserMsg() { return m_message_user; }

    BaseException &SetBothMsgs(const wxChar *msg_diag);
    BaseException &SetDiagMsg(const wxString &msg_diag);
    BaseException &SetUserMsg(const wxString &msg_user);

    // Returns a message suitable for diagnostic / logging purposes.
    // This message is always in English, and includes a full stack trace.
    virtual wxString FormatDiagnosticMessage() const;

    // Returns a message suitable for end-user display.
    // This message is usually meant for display in a user popup or such.
    virtual wxString FormatDisplayMessage() const;

    virtual void Rethrow() const = 0;
    virtual BaseException *Clone() const = 0;
};

typedef std::unique_ptr<BaseException> ScopedExcept;

// --------------------------------------------------------------------------------------
//  Ps2Generic Exception
// --------------------------------------------------------------------------------------
// This class is used as a base exception for things tossed by PS2 cpus (EE, IOP, etc).
//
// Implementation note: does not derive from BaseException, so that we can use different
// catch block hierarchies to handle them (if needed).
//
// Translation Note: Currently these exceptions are never translated.  English/diagnostic
// format only. :)
//
class Ps2Generic
{
protected:
    wxString m_message; // a "detailed" message of what disastrous thing has occurred!

public:
    virtual ~Ps2Generic() = default;

    virtual u32 GetPc() const = 0;
    virtual bool IsDelaySlot() const = 0;
    virtual wxString &Message() { return m_message; }

    virtual void Rethrow() const = 0;
    virtual Ps2Generic *Clone() const = 0;
};

// Some helper macros for defining the standard constructors of internationalized constructors
// Parameters:
//  classname - Yeah, the name of this class being defined. :)
//
//  defmsg - default message (in english), which will be used for both english and i18n messages.
//     The text string will be passed through the translator, so if it's int he gettext database
//     it will be optionally translated.
//
// BUGZ??  I'd rather use 'classname' on the Clone() prototype, but for some reason it generates
// ambiguity errors on virtual inheritance (it really shouldn't!).  So I have to force it to the
// BaseException base class.  Not sure if this is Stupid Standard Tricks or Stupid MSVC Tricks. --air
//
// (update: web searches indicate it's MSVC specific -- happens in 2008, not sure about 2010).
//
#define DEFINE_EXCEPTION_COPYTORS(classname, parent) \
private:                                             \
    typedef parent _parent;                          \
                                                     \
public:                                              \
    virtual ~classname() = default;                  \
    virtual void Rethrow() const { throw * this; }   \
    virtual classname *Clone() const { return new classname(*this); }

#define DEFINE_EXCEPTION_MESSAGES(classname)        \
public:                                             \
    classname &SetBothMsgs(const wxChar *msg_diag)  \
    {                                               \
        BaseException::SetBothMsgs(msg_diag);       \
        return *this;                               \
    }                                               \
    classname &SetDiagMsg(const wxString &msg_diag) \
    {                                               \
        m_message_diag = msg_diag;                  \
        return *this;                               \
    }                                               \
    classname &SetUserMsg(const wxString &msg_user) \
    {                                               \
        m_message_user = msg_user;                  \
        return *this;                               \
    }

#define DEFINE_RUNTIME_EXCEPTION(classname, parent, message) \
    DEFINE_EXCEPTION_COPYTORS(classname, parent)             \
    classname() { SetDiagMsg(message); }                     \
    DEFINE_EXCEPTION_MESSAGES(classname)


// ---------------------------------------------------------------------------------------
//  RuntimeError - Generalized Exceptions with Recoverable Traits!
// ---------------------------------------------------------------------------------------

class RuntimeError : public BaseException
{
    DEFINE_EXCEPTION_COPYTORS(RuntimeError, BaseException)
    DEFINE_EXCEPTION_MESSAGES(RuntimeError)

public:
    bool IsSilent;

    RuntimeError() { IsSilent = false; }
    RuntimeError(const std::runtime_error &ex, const wxString &prefix = wxEmptyString);
    RuntimeError(const std::exception &ex, const wxString &prefix = wxEmptyString);
};

// --------------------------------------------------------------------------------------
//  CancelAppEvent  -  Exception for canceling an event in a non-verbose fashion
// --------------------------------------------------------------------------------------
// Typically the PCSX2 interface issues popup dialogs for runtime errors.  This exception
// instead issues a "silent" cancelation that is handled by the app gracefully (generates
// log, and resumes messages queue processing).
//
// I chose to have this exception derive from RuntimeError, since if one is thrown from outside
// an App message loop we'll still want it to be handled in a reasonably graceful manner.
class CancelEvent : public RuntimeError
{
    DEFINE_RUNTIME_EXCEPTION(CancelEvent, RuntimeError, pxLt("No reason given."))

public:
    explicit CancelEvent(const wxString &logmsg)
    {
        m_message_diag = logmsg;
        // overridden message formatters only use the diagnostic version...
    }

    virtual wxString FormatDisplayMessage() const;
    virtual wxString FormatDiagnosticMessage() const;
};

// ---------------------------------------------------------------------------------------
//  OutOfMemory
// ---------------------------------------------------------------------------------------
// This exception has a custom-formatted Diagnostic string.  The parameter give when constructing
// the exception is a block/alloc name, which is used as a formatting parameter in the diagnostic
// output.  The default diagnostic message is "Out of memory exception, while allocating the %s."
// where %s is filled in with the block name.
//
// The user string is not custom-formatted, and should contain *NO* %s tags.
//
class OutOfMemory : public RuntimeError
{
    DEFINE_RUNTIME_EXCEPTION(OutOfMemory, RuntimeError, wxEmptyString)

public:
    wxString AllocDescription;

public:
    OutOfMemory(const wxString &allocdesc);

    virtual wxString FormatDisplayMessage() const;
    virtual wxString FormatDiagnosticMessage() const;
};

class ParseError : public RuntimeError
{
    DEFINE_RUNTIME_EXCEPTION(ParseError, RuntimeError, pxL("Parse error"));
};

// ---------------------------------------------------------------------------------------
// Hardware/OS Exceptions:
//   HardwareDeficiency / VirtualMemoryMapConflict
// ---------------------------------------------------------------------------------------

// This exception is a specific type of OutOfMemory error that isn't "really" an out of
// memory error.  More likely it's caused by a plugin or driver reserving a range of memory
// we'd really like to have access to.
class VirtualMemoryMapConflict : public OutOfMemory
{
    DEFINE_RUNTIME_EXCEPTION(VirtualMemoryMapConflict, OutOfMemory, wxEmptyString)

    VirtualMemoryMapConflict(const wxString &allocdesc);

    virtual wxString FormatDisplayMessage() const;
    virtual wxString FormatDiagnosticMessage() const;
};

class HardwareDeficiency : public RuntimeError
{
public:
    DEFINE_RUNTIME_EXCEPTION(HardwareDeficiency, RuntimeError, pxL("Your machine's hardware is incapable of running PCSX2.  Sorry dood."));
};

// ---------------------------------------------------------------------------------------
// Streaming (file) Exceptions:
//   Stream / BadStream / CannotCreateStream / FileNotFound / AccessDenied / EndOfStream
// ---------------------------------------------------------------------------------------

#define DEFINE_STREAM_EXCEPTION_ACCESSORS(classname)       \
    virtual classname &SetStreamName(const wxString &name) \
    {                                                      \
        StreamName = name;                                 \
        return *this;                                      \
    }                                                      \
    virtual classname &SetStreamName(const char *name)     \
    {                                                      \
        StreamName = fromUTF8(name);                       \
        return *this;                                      \
    }

#define DEFINE_STREAM_EXCEPTION(classname, parent)             \
    DEFINE_RUNTIME_EXCEPTION(classname, parent, wxEmptyString) \
    classname(const wxString &filename)                        \
    {                                                          \
        StreamName = filename;                                 \
    }                                                          \
    DEFINE_STREAM_EXCEPTION_ACCESSORS(classname)

// A generic base error class for bad streams -- corrupted data, sudden closures, loss of
// connection, or anything else that would indicate a failure to open a stream or read the
// data after the stream was successfully opened.
//
class BadStream : public RuntimeError
{
    DEFINE_STREAM_EXCEPTION(BadStream, RuntimeError)

public:
    wxString StreamName; // name of the stream (if applicable)

    virtual wxString FormatDiagnosticMessage() const;
    virtual wxString FormatDisplayMessage() const;

protected:
    void _formatDiagMsg(FastFormatUnicode &dest) const;
    void _formatUserMsg(FastFormatUnicode &dest) const;
};

// A generic exception for odd-ball stream creation errors.
//
class CannotCreateStream : public BadStream
{
    DEFINE_STREAM_EXCEPTION(CannotCreateStream, BadStream)

    virtual wxString FormatDiagnosticMessage() const;
    virtual wxString FormatDisplayMessage() const;
};

// Exception thrown when an attempt to open a non-existent file is made.
// (this exception can also mean file permissions are invalid)
//
class FileNotFound : public CannotCreateStream
{
public:
    DEFINE_STREAM_EXCEPTION(FileNotFound, CannotCreateStream)

    virtual wxString FormatDiagnosticMessage() const;
    virtual wxString FormatDisplayMessage() const;
};

class AccessDenied : public CannotCreateStream
{
public:
    DEFINE_STREAM_EXCEPTION(AccessDenied, CannotCreateStream)

    virtual wxString FormatDiagnosticMessage() const;
    virtual wxString FormatDisplayMessage() const;
};

// EndOfStream can be used either as an error, or used just as a shortcut for manual
// feof checks.
//
class EndOfStream : public BadStream
{
public:
    DEFINE_STREAM_EXCEPTION(EndOfStream, BadStream)

    virtual wxString FormatDiagnosticMessage() const;
    virtual wxString FormatDisplayMessage() const;
};

#ifdef __WXMSW__
// --------------------------------------------------------------------------------------
//  Exception::WinApiError
// --------------------------------------------------------------------------------------
class WinApiError : public RuntimeError
{
    DEFINE_EXCEPTION_COPYTORS(WinApiError, RuntimeError)
    DEFINE_EXCEPTION_MESSAGES(WinApiError)

public:
    int ErrorId;

public:
    WinApiError();

    wxString GetMsgFromWindows() const;
    virtual wxString FormatDisplayMessage() const;
    virtual wxString FormatDiagnosticMessage() const;
};
#endif
}

using Exception::BaseException;
using Exception::ScopedExcept;