source: vendor/synergy/current/lib/base/XBase.h

Last change on this file was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 3.5 KB
Line 
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/*!
22This is the base class of most exception types.
23*/
24class XBase {
25public:
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
35protected:
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
48private:
49 mutable CString m_what;
50};
51
52/*!
53\def XBASE_SUBCLASS
54Convenience macro to subclass from XBase (or a subclass of it),
55providing the c'tor taking a const CString&. getWhat() is not
56declared.
57*/
58#define XBASE_SUBCLASS(name_, super_) \
59class name_ : public super_ { \
60public: \
61 name_() : super_() { } \
62 name_(const CString& msg) : super_(msg) { } \
63}
64
65/*!
66\def XBASE_SUBCLASS
67Convenience macro to subclass from XBase (or a subclass of it),
68providing the c'tor taking a const CString&. getWhat() must be
69implemented.
70*/
71#define XBASE_SUBCLASS_WHAT(name_, super_) \
72class name_ : public super_ { \
73public: \
74 name_() : super_() { } \
75 name_(const CString& msg) : super_(msg) { } \
76 \
77protected: \
78 virtual CString getWhat() const throw(); \
79}
80
81/*!
82\def XBASE_SUBCLASS_FORMAT
83Convenience macro to subclass from XBase (or a subclass of it),
84providing the c'tor taking a const CString&. what() is overridden
85to call getWhat() when first called; getWhat() can format the
86error message and can call what() to get the message passed to the
87c'tor.
88*/
89#define XBASE_SUBCLASS_FORMAT(name_, super_) \
90class name_ : public super_ { \
91private: \
92 enum EState { kFirst, kFormat, kDone }; \
93 \
94public: \
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 \
113protected: \
114 virtual CString getWhat() const throw(); \
115 \
116private: \
117 mutable EState m_state; \
118 mutable std::string m_formatted; \
119}
120
121#endif
Note: See TracBrowser for help on using the repository browser.