1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2002 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #ifndef XBASE_H
|
---|
16 | #define XBASE_H
|
---|
17 |
|
---|
18 | #include "CString.h"
|
---|
19 |
|
---|
20 | //! Exception base class
|
---|
21 | /*!
|
---|
22 | This is the base class of most exception types.
|
---|
23 | */
|
---|
24 | class XBase {
|
---|
25 | public:
|
---|
26 | //! Use getWhat() as the result of what()
|
---|
27 | XBase();
|
---|
28 | //! Use \c msg as the result of what()
|
---|
29 | XBase(const CString& msg);
|
---|
30 | virtual ~XBase();
|
---|
31 |
|
---|
32 | //! Reason for exception
|
---|
33 | virtual const char* what() const;
|
---|
34 |
|
---|
35 | protected:
|
---|
36 | //! Get a human readable string describing the exception
|
---|
37 | virtual CString getWhat() const throw() = 0;
|
---|
38 |
|
---|
39 | //! Format a string
|
---|
40 | /*!
|
---|
41 | Looks up a message format using \c id, using \c defaultFormat if
|
---|
42 | no format can be found, then replaces positional parameters in
|
---|
43 | the format string and returns the result.
|
---|
44 | */
|
---|
45 | virtual CString format(const char* id,
|
---|
46 | const char* defaultFormat, ...) const throw();
|
---|
47 |
|
---|
48 | private:
|
---|
49 | mutable CString m_what;
|
---|
50 | };
|
---|
51 |
|
---|
52 | /*!
|
---|
53 | \def XBASE_SUBCLASS
|
---|
54 | Convenience macro to subclass from XBase (or a subclass of it),
|
---|
55 | providing the c'tor taking a const CString&. getWhat() is not
|
---|
56 | declared.
|
---|
57 | */
|
---|
58 | #define XBASE_SUBCLASS(name_, super_) \
|
---|
59 | class name_ : public super_ { \
|
---|
60 | public: \
|
---|
61 | name_() : super_() { } \
|
---|
62 | name_(const CString& msg) : super_(msg) { } \
|
---|
63 | }
|
---|
64 |
|
---|
65 | /*!
|
---|
66 | \def XBASE_SUBCLASS
|
---|
67 | Convenience macro to subclass from XBase (or a subclass of it),
|
---|
68 | providing the c'tor taking a const CString&. getWhat() must be
|
---|
69 | implemented.
|
---|
70 | */
|
---|
71 | #define XBASE_SUBCLASS_WHAT(name_, super_) \
|
---|
72 | class name_ : public super_ { \
|
---|
73 | public: \
|
---|
74 | name_() : super_() { } \
|
---|
75 | name_(const CString& msg) : super_(msg) { } \
|
---|
76 | \
|
---|
77 | protected: \
|
---|
78 | virtual CString getWhat() const throw(); \
|
---|
79 | }
|
---|
80 |
|
---|
81 | /*!
|
---|
82 | \def XBASE_SUBCLASS_FORMAT
|
---|
83 | Convenience macro to subclass from XBase (or a subclass of it),
|
---|
84 | providing the c'tor taking a const CString&. what() is overridden
|
---|
85 | to call getWhat() when first called; getWhat() can format the
|
---|
86 | error message and can call what() to get the message passed to the
|
---|
87 | c'tor.
|
---|
88 | */
|
---|
89 | #define XBASE_SUBCLASS_FORMAT(name_, super_) \
|
---|
90 | class name_ : public super_ { \
|
---|
91 | private: \
|
---|
92 | enum EState { kFirst, kFormat, kDone }; \
|
---|
93 | \
|
---|
94 | public: \
|
---|
95 | name_() : super_(), m_state(kDone) { } \
|
---|
96 | name_(const CString& msg) : super_(msg), m_state(kFirst) { } \
|
---|
97 | \
|
---|
98 | virtual const char* what() const \
|
---|
99 | { \
|
---|
100 | if (m_state == kFirst) { \
|
---|
101 | m_state = kFormat; \
|
---|
102 | m_formatted = getWhat(); \
|
---|
103 | m_state = kDone; \
|
---|
104 | } \
|
---|
105 | if (m_state == kDone) { \
|
---|
106 | return m_formatted.c_str(); \
|
---|
107 | } \
|
---|
108 | else { \
|
---|
109 | return super_::what(); \
|
---|
110 | } \
|
---|
111 | } \
|
---|
112 | \
|
---|
113 | protected: \
|
---|
114 | virtual CString getWhat() const throw(); \
|
---|
115 | \
|
---|
116 | private: \
|
---|
117 | mutable EState m_state; \
|
---|
118 | mutable std::string m_formatted; \
|
---|
119 | }
|
---|
120 |
|
---|
121 | #endif
|
---|