summaryrefslogtreecommitdiffstats
path: root/src/lib/base/XBase.h
blob: 3064b6c0a88042589c5157831643d676630cd56b (plain) (blame)
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
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2002 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 * 
 * This package 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "base/String.h"
#include "common/stdexcept.h"

//! Exception base class
/*!
This is the base class of most exception types.
*/
class XBase : public std::runtime_error {
public:
    //! Use getWhat() as the result of what()
    XBase();
    //! Use \c msg as the result of what()
    XBase(const String& msg);
    virtual ~XBase() _NOEXCEPT;

    //! Reason for exception
    virtual const char* what() const _NOEXCEPT;

protected:
    //! Get a human readable string describing the exception
    virtual String        getWhat() const throw() { return ""; }

    //! Format a string
    /*!
    Looks up a message format using \c id, using \c defaultFormat if
    no format can be found, then replaces positional parameters in
    the format string and returns the result.
    */
    virtual String        format(const char* id,
                            const char* defaultFormat, ...) const throw();
private:
    mutable String        m_what;
};

/*!
\def XBASE_SUBCLASS
Convenience macro to subclass from XBase (or a subclass of it),
providing the c'tor taking a const String&.  getWhat() is not
declared.
*/
#define XBASE_SUBCLASS(name_, super_)                                    \
class name_ : public super_ {                                            \
public:                                                                    \
    name_() : super_() { }                                                \
    name_(const String& msg) : super_(msg) { }                            \
    virtual ~name_() _NOEXCEPT { }                                        \
}

/*!
\def XBASE_SUBCLASS
Convenience macro to subclass from XBase (or a subclass of it),
providing the c'tor taking a const String&.  getWhat() must be
implemented.
*/
#define XBASE_SUBCLASS_WHAT(name_, super_)                                \
class name_ : public super_ {                                            \
public:                                                                    \
    name_() : super_() { }                                                \
    name_(const String& msg) : super_(msg) { }                            \
    virtual ~name_() _NOEXCEPT { }                                        \
                                                                        \
protected:                                                                \
    virtual String        getWhat() const throw();                        \
}

/*!
\def XBASE_SUBCLASS_FORMAT
Convenience macro to subclass from XBase (or a subclass of it),
providing the c'tor taking a const String&.  what() is overridden
to call getWhat() when first called;  getWhat() can format the
error message and can call what() to get the message passed to the
c'tor.
*/
#define XBASE_SUBCLASS_FORMAT(name_, super_)                            \
class name_ : public super_ {                                            \
private:                                                                \
    enum EState { kFirst, kFormat, kDone };                                \
                                                                        \
public:                                                                    \
    name_() : super_(), m_state(kDone) { }                                \
    name_(const String& msg) : super_(msg), m_state(kFirst) { }        \
    virtual ~name_() _NOEXCEPT { }                                        \
                                                                        \
    virtual const char*    what() const _NOEXCEPT                            \
    {                                                                    \
        if (m_state == kFirst) {                                        \
            m_state = kFormat;                                            \
            m_formatted = getWhat();                                    \
            m_state = kDone;                                            \
        }                                                                \
        if (m_state == kDone) {                                            \
            return m_formatted.c_str();                                    \
        }                                                                \
        else {                                                            \
            return super_::what();                                        \
        }                                                                \
    }                                                                    \
                                                                        \
protected:                                                                \
    virtual String        getWhat() const throw();                        \
                                                                        \
private:                                                                \
    mutable EState                m_state;                                \
    mutable std::string            m_formatted;                            \
}