2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#pragma once
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-03-20 23:35:21 +00:00
|
|
|
#include <stdexcept>
|
2025-01-06 00:55:07 +00:00
|
|
|
#include <string>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//! Exception base class
|
|
|
|
|
/*!
|
|
|
|
|
This is the base class of most exception types.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
class XBase : public std::runtime_error
|
|
|
|
|
{
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-07-02 19:07:06 +00:00
|
|
|
//! Use getWhat() as the result of what()
|
|
|
|
|
XBase();
|
|
|
|
|
//! Use \c msg as the result of what()
|
2025-01-06 00:55:07 +00:00
|
|
|
XBase(const std::string &msg);
|
2025-05-01 14:48:13 +00:00
|
|
|
~XBase() throw() override = default;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
//! Reason for exception
|
2025-04-28 01:27:48 +00:00
|
|
|
const char *what() const throw() override;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
protected:
|
2024-07-02 19:07:06 +00:00
|
|
|
//! Get a human readable string describing the exception
|
2025-01-06 00:55:07 +00:00
|
|
|
virtual std::string getWhat() const throw()
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2024-07-02 19:07:06 +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.
|
|
|
|
|
*/
|
2025-01-06 00:55:07 +00:00
|
|
|
virtual std::string format(const char *id, const char *defaultFormat, ...) const throw();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2014-03-31 13:56:41 +00:00
|
|
|
private:
|
2025-01-06 00:55:07 +00:00
|
|
|
mutable std::string m_what;
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\def XBASE_SUBCLASS
|
|
|
|
|
Convenience macro to subclass from XBase (or a subclass of it),
|
2025-01-06 00:55:07 +00:00
|
|
|
providing the c'tor taking a const std::string&. getWhat() is not
|
2012-06-10 16:50:54 +00:00
|
|
|
declared.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
#define XBASE_SUBCLASS(name_, super_) \
|
|
|
|
|
class name_ : public super_ \
|
|
|
|
|
{ \
|
|
|
|
|
public: \
|
|
|
|
|
name_() : super_() \
|
|
|
|
|
{ \
|
|
|
|
|
} \
|
2025-01-06 00:55:07 +00:00
|
|
|
name_(const std::string &msg) : super_(msg) \
|
2024-10-17 18:04:35 +00:00
|
|
|
{ \
|
|
|
|
|
} \
|
2025-03-20 23:35:21 +00:00
|
|
|
virtual ~name_() throw() \
|
2024-10-17 18:04:35 +00:00
|
|
|
{ \
|
|
|
|
|
} \
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\def XBASE_SUBCLASS
|
|
|
|
|
Convenience macro to subclass from XBase (or a subclass of it),
|
2025-01-06 00:55:07 +00:00
|
|
|
providing the c'tor taking a const std::string&. getWhat() must be
|
2012-06-10 16:50:54 +00:00
|
|
|
implemented.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
#define XBASE_SUBCLASS_WHAT(name_, super_) \
|
|
|
|
|
class name_ : public super_ \
|
|
|
|
|
{ \
|
|
|
|
|
public: \
|
|
|
|
|
name_() : super_() \
|
|
|
|
|
{ \
|
|
|
|
|
} \
|
2025-01-06 00:55:07 +00:00
|
|
|
name_(const std::string &msg) : super_(msg) \
|
2024-10-17 18:04:35 +00:00
|
|
|
{ \
|
|
|
|
|
} \
|
2025-03-20 23:35:21 +00:00
|
|
|
virtual ~name_() throw() \
|
2024-10-17 18:04:35 +00:00
|
|
|
{ \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
protected: \
|
2025-01-06 00:55:07 +00:00
|
|
|
virtual std::string getWhat() const throw(); \
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\def XBASE_SUBCLASS_FORMAT
|
|
|
|
|
Convenience macro to subclass from XBase (or a subclass of it),
|
2025-01-06 00:55:07 +00:00
|
|
|
providing the c'tor taking a const std::string&. what() is overridden
|
2012-06-10 16:50:54 +00:00
|
|
|
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.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
#define XBASE_SUBCLASS_FORMAT(name_, super_) \
|
|
|
|
|
class name_ : public super_ \
|
|
|
|
|
{ \
|
|
|
|
|
private: \
|
|
|
|
|
enum EState \
|
|
|
|
|
{ \
|
|
|
|
|
kFirst, \
|
|
|
|
|
kFormat, \
|
|
|
|
|
kDone \
|
|
|
|
|
}; \
|
|
|
|
|
\
|
|
|
|
|
public: \
|
|
|
|
|
name_() : super_(), m_state(kDone) \
|
|
|
|
|
{ \
|
|
|
|
|
} \
|
2025-01-06 00:55:07 +00:00
|
|
|
name_(const std::string &msg) : super_(msg), m_state(kFirst) \
|
2024-10-17 18:04:35 +00:00
|
|
|
{ \
|
|
|
|
|
} \
|
2025-03-20 23:35:21 +00:00
|
|
|
virtual ~name_() throw() \
|
2024-10-17 18:04:35 +00:00
|
|
|
{ \
|
|
|
|
|
} \
|
|
|
|
|
\
|
2025-03-20 23:35:21 +00:00
|
|
|
virtual const char *what() const throw() \
|
2024-10-17 18:04:35 +00:00
|
|
|
{ \
|
|
|
|
|
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: \
|
2025-01-06 00:55:07 +00:00
|
|
|
virtual std::string getWhat() const throw(); \
|
2024-10-17 18:04:35 +00:00
|
|
|
\
|
|
|
|
|
private: \
|
|
|
|
|
mutable EState m_state; \
|
|
|
|
|
mutable std::string m_formatted; \
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|