Qx v0.7
Qt Extensions Library
Loading...
Searching...
No Matches
qx-error.h
Go to the documentation of this file.
1#ifndef QX_ERROR_H
2#define QX_ERROR_H
3
4// Shared Lib Support
5#include "qx/core/qx_core_export.h"
6
7// Qt Includes
8#include <QHash>
9#include <QMetaType>
10#include <QTextStream>
11
12// Intra-component Includes
14
15// Private namespace for storing registered error adapters
16namespace QxErrorPrivate
17{
18 template<class ErrorAdaptable>
19 struct adapter_registry;
20}
21
22/* Forward declarations to support global scope operator<<() overload of QTextStream
23 * for Qx::Error. The operator must be in global scope in order for ADL to work correctly
24 * for types made convertible to Qx::Error via an Error Adapter, as neither QTextStream
25 * nor those types will be in the Qx namespace and therefore the Qx namespace is not
26 * checked for that overload when using those types. This would prevent implicit
27 * conversions of those types to Qx::Error when using them with a QTextStream, which is
28 * half the purpose of the adapters.
29 */
30namespace Qx { class Error; }
31QX_CORE_EXPORT QTextStream& operator<<(QTextStream& ts, const Qx::Error& e);
32
33namespace Qx
34{
35
36class Error;
37
38// Need to define here, needed later
39template<class E>
41
42}
43
45namespace QxErrorPrivate
46{
47
48template <typename T>
49inline constexpr bool variant_has_error_types = false;
50
51template <typename... Types>
52inline constexpr bool variant_has_error_types<std::variant<Types...>> =
54
55}
57
58namespace Qx
59{
60
61template <typename V>
62concept error_variant = QxErrorPrivate::variant_has_error_types<V>;
63
64class QX_CORE_EXPORT Error
65{
66//-Class Variables----------------------------------------------------------------------------------------------------------
67private:
68 static inline const QString DETAILED_INFO_HEADING = u"Details:\n--------"_s;
69
70 // Adapter Registry Alias
71 template <class K>
72 using AdapterType = typename QxErrorPrivate::adapter_registry<K>::adapter;
73
74public:
75 static constexpr quint16 TYPE_CODE = 0;
76 static constexpr QLatin1StringView TYPE_NAME{"Error"};
77
78//-Instance Variables----------------------------------------------------------------------------------------------------------
79private:
80 quint16 mTypeCode;
81 QLatin1StringView mTypeName;
82 quint32 mValue;
83 Severity mSeverity;
84 QString mCaption;
85 QString mPrimary;
86 QString mSecondary;
87 QString mDetails;
88
89//-Constructor----------------------------------------------------------------------------------------------------------
90public:
91 Error();
92
93 template<class ErrorType>
95 Error(const ErrorType& e) :
96 mTypeCode(ErrorType::TYPE_CODE),
97 mTypeName(ErrorType::TYPE_NAME)
98 {
99 const IError* base = static_cast<const IError*>(&e);
100 mValue = base->deriveValue();
101 mSeverity = base->deriveSeverity();
102
103 if(mValue != 0) // Ignore strings for invalid errors
104 {
105 mCaption = base->deriveCaption();
106 mPrimary = base->derivePrimary();
107 mSecondary = base->deriveSecondary();
108 mDetails = base->deriveDetails();
109 }
110 }
111
112 template<class EAble>
113 requires adapted_error<EAble>
114 Error(const EAble& adapted) : Error(AdapterType<EAble>(adapted))
115 {}
116
117 template<typename Errors>
118 requires error_variant<Errors>
119 Error(const Errors& e) : Error(std::visit([](auto&& arg){ return arg; }, e))
120 {}
121
122//-Instance Functions------------------------------------------------------------------------------------------------------
123public:
124 quint16 typeCode() const;
125 QLatin1StringView typeName() const;
126
127 quint32 value() const;
128 Severity severity() const;
129 QString severityString(bool uc = true) const;
130 QString caption() const;
131 QString primary() const;
132 QString secondary() const;
133 QString details() const;
134
135 bool isValid() const;
136 bool equivalent(const Error& other) const;
137 quint64 code() const;
138 QString hexCode() const;
139 QString toString() const;
140
141 Error& setSeverity(Severity sv);
142 Error withSeverity(Severity sv);
143
144
145//-Operators-------------------------------------------------------------------------------------------------------
146public:
147 bool operator==(const Error& other) const = default;
148 bool operator!=(const Error& other) const = default;
149 explicit operator bool() const;
150
151//-Friend Functions------------------------------------------------------------------------------------------------
152friend QTextStream& ::operator<<(QTextStream& ts, const Error& e);
153};
154
155} // namespace Qx
156
157//-Non-member/Related Functions------------------------------------------------------------------------------------
158QX_CORE_EXPORT QTextStream& operator<<(QTextStream& ts, const Qx::Error& e);
159
160//-Metatype declarations-------------------------------------------------------------------------------------------
161Q_DECLARE_METATYPE(Qx::Error);
162
163//-Macros----------------------------------------------------------------------------------------------------------
164#define QX_DECLARE_ERROR_ADAPTATION(Adaptable, Adapter) \
165 static_assert(Qx::error_adaptation<Adaptable, Adapter>, "Adapter must satisfy the 'error_adapter' concept " \
166 "and be constructable from Adaptable."); \
167 template<> \
168 struct QxErrorPrivate::adapter_registry<Adaptable> { typedef Adapter adapter; };
169
170#endif // QX_ERROR_H
The Error class acts as an interface for an extensible variety of error objects.
Definition qx-error.h:65
Error(const EAble &adapted)
Definition qx-error.h:114
Error(const ErrorType &e)
Definition qx-error.h:95
static constexpr QLatin1StringView TYPE_NAME
Definition qx-error.h:76
bool operator==(const Error &other) const =default
bool operator!=(const Error &other) const =default
Error()
Definition qx-error.cpp:131
static constexpr quint16 TYPE_CODE
Definition qx-error.h:75
Error(const Errors &e)
Definition qx-error.h:119
IError defines the baseline inheritance interface for Qx error types.
Definition qx-abstracterror.h:28
virtual QString deriveCaption() const
Definition qx-abstracterror.cpp:102
virtual quint32 deriveValue() const
Definition qx-abstracterror.cpp:78
virtual QString deriveSecondary() const
Definition qx-abstracterror.cpp:124
virtual QString derivePrimary() const
Definition qx-abstracterror.cpp:113
virtual Severity deriveSeverity() const
Definition qx-abstracterror.cpp:87
virtual QString deriveDetails() const
Definition qx-abstracterror.cpp:135
Specifies that a non-error type has a registered error adapter.
Definition qx-error.h:40
Specifies that two types form a Qx error adaptation.
Definition qx-abstracterror.h:145
Specifies that a type is a Qx error type.
Definition qx-abstracterror.h:122
Specifies that a type is a Qx error variant.
Definition qx-error.h:62
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-abstracterror.cpp:13
Severity
Definition qx-global.h:11
The qx-abstracterror.h header file provides access to the base class from which custom error types sh...
QX_CORE_EXPORT QTextStream & operator<<(QTextStream &ts, const Qx::Error &e)
Definition qx-error.cpp:396