2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* 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 COPYING 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.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
#ifndef XBASE_H
|
|
|
|
#define XBASE_H
|
|
|
|
|
|
|
|
#include "CString.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
/*
|
2002-06-01 19:26:11 +00:00
|
|
|
#include "stdpre.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include <exception>
|
2002-06-01 19:26:11 +00:00
|
|
|
#include "stdpost.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
*/
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-26 18:28:18 +00:00
|
|
|
//! Exception base class
|
|
|
|
/*!
|
|
|
|
This is the base class of most exception types.
|
|
|
|
*/
|
2002-09-14 21:31:35 +00:00
|
|
|
class XBase /*: public std::exception*/ {
|
2002-04-29 14:40:01 +00:00
|
|
|
public:
|
2002-07-26 18:28:18 +00:00
|
|
|
//! Use getWhat() as the result of what()
|
2001-10-06 14:13:28 +00:00
|
|
|
XBase();
|
2002-07-26 18:28:18 +00:00
|
|
|
//! Use \c msg as the result of what()
|
2001-10-06 14:13:28 +00:00
|
|
|
XBase(const CString& msg);
|
|
|
|
virtual ~XBase();
|
|
|
|
|
|
|
|
// std::exception overrides
|
|
|
|
virtual const char* what() const;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
protected:
|
2002-07-26 18:28:18 +00:00
|
|
|
//! Get a human readable string describing the exception
|
2001-10-06 14:13:28 +00:00
|
|
|
virtual CString getWhat() const throw() = 0;
|
|
|
|
|
2002-07-26 18:28:18 +00:00
|
|
|
//! 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.
|
|
|
|
*/
|
2001-10-06 14:13:28 +00:00
|
|
|
virtual CString format(const char* id,
|
2002-06-10 22:06:45 +00:00
|
|
|
const char* defaultFormat, ...) const throw();
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
private:
|
2001-10-06 14:13:28 +00:00
|
|
|
mutable CString m_what;
|
|
|
|
};
|
|
|
|
|
2002-07-26 18:28:18 +00:00
|
|
|
/*!
|
2003-01-04 22:01:32 +00:00
|
|
|
\def XBASE_SUBCLASS
|
|
|
|
Convenience macro to subclass from XBase (or a subclass of it),
|
|
|
|
providing the c'tor taking a const CString&. getWhat() is not
|
|
|
|
declared.
|
2002-07-26 18:28:18 +00:00
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
#define XBASE_SUBCLASS(name_, super_) \
|
|
|
|
class name_ : public super_ { \
|
|
|
|
public: \
|
|
|
|
name_() : super_() { } \
|
|
|
|
name_(const CString& msg) : super_(msg) { } \
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
/*!
|
|
|
|
\def XBASE_SUBCLASS
|
|
|
|
Convenience macro to subclass from XBase (or a subclass of it),
|
|
|
|
providing the c'tor taking a const CString&. getWhat() must be
|
|
|
|
implemented.
|
|
|
|
*/
|
|
|
|
#define XBASE_SUBCLASS_WHAT(name_, super_) \
|
|
|
|
class name_ : public super_ { \
|
|
|
|
public: \
|
|
|
|
name_() : super_() { } \
|
|
|
|
name_(const CString& msg) : super_(msg) { } \
|
|
|
|
\
|
|
|
|
protected: \
|
|
|
|
virtual CString getWhat() const throw(); \
|
|
|
|
}
|
2002-07-26 18:28:18 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
/*!
|
|
|
|
\def XBASE_SUBCLASS_FORMAT
|
|
|
|
Convenience macro to subclass from XBase (or a subclass of it),
|
|
|
|
providing the c'tor taking a const CString&. 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 CString& msg) : super_(msg), m_state(kFirst) { } \
|
|
|
|
\
|
|
|
|
virtual const char* what() const \
|
|
|
|
{ \
|
|
|
|
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 CString getWhat() const throw(); \
|
|
|
|
\
|
|
|
|
private: \
|
|
|
|
mutable EState m_state; \
|
|
|
|
mutable std::string m_formatted; \
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
#endif
|