diff options
| author | Linus Jahn <lnj@kaidan.im> | 2022-09-06 20:05:34 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2022-09-09 22:49:06 +0200 |
| commit | b113ae300efe5dc17f148a06df09062546bf54ee (patch) | |
| tree | a7ada2bf208329687a7c4d47d74579551d9257a9 /src/base/QXmppError.h | |
| parent | 74cc77670b009ef98d681477ac430210d02c4cc4 (diff) | |
| download | qxmpp-b113ae300efe5dc17f148a06df09062546bf54ee.tar.gz | |
Add QXmppError holding a description and std::any
Diffstat (limited to 'src/base/QXmppError.h')
| -rw-r--r-- | src/base/QXmppError.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/base/QXmppError.h b/src/base/QXmppError.h new file mode 100644 index 00000000..a646b6e5 --- /dev/null +++ b/src/base/QXmppError.h @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPERROR_H +#define QXMPPERROR_H + +#include "QXmppGlobal.h" + +#include <any> +#include <optional> + +struct QXMPP_EXPORT QXmppError +{ + QString description; + std::any error; + + bool isFileError() const; + bool isNetworkError() const; + bool isStanzaError() const; + + template<typename T> + bool holdsType() const + { + return error.type().hash_code() == typeid(T).hash_code(); + } + template<typename T> + std::optional<T> value() const + { + // any_cast always checks this, to avoid an additional check we use exceptions + try { + return std::any_cast<T>(error); + } catch (std::bad_any_cast) { + return {}; + } + } + template<typename T> + std::optional<T> takeValue() + { + // we can't use unchecked any_cast with moving because we can't access the error after a + // failed any_cast + if (error.type().hash_code() == typeid(T).hash_code()) { + auto value = std::any_cast<T>(std::move(error)); + error = std::any(); + return value; + } + return {}; + } +}; + +#endif // QXMPPERROR_H |
