Qx v0.5.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>
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; }
31
32QX_CORE_EXPORT QTextStream& operator<<(QTextStream& ts, const Qx::Error& e);
33
34namespace Qx
35{
36
37class QX_CORE_EXPORT Error
38{
39//-Class Variables----------------------------------------------------------------------------------------------------------
40private:
41 static inline const QString DETAILED_INFO_HEADING = u"Details:\n--------"_s;
42
43 // Adapter Registry Alias
44 template <class K>
46
47public:
48 static constexpr quint16 TYPE_CODE = 0;
49 static constexpr QLatin1StringView TYPE_NAME{"Error"};
50
51//-Instance Variables----------------------------------------------------------------------------------------------------------
52private:
53 quint16 mTypeCode;
54 QLatin1StringView mTypeName;
55 quint32 mValue;
56 Severity mSeverity;
57 QString mCaption;
58 QString mPrimary;
59 QString mSecondary;
60 QString mDetails;
61
62//-Constructor----------------------------------------------------------------------------------------------------------
63public:
64 Error();
65
66 template<class ErrorType>
68 Error(const ErrorType& e) :
69 mTypeCode(ErrorType::TYPE_CODE),
70 mTypeName(ErrorType::TYPE_NAME)
71 {
72 const IError* base = static_cast<const IError*>(&e);
73 mValue = base->deriveValue();
74 mSeverity = base->deriveSeverity();
75
76 if(mValue != 0) // Ignore strings for invalid errors
77 {
78 mCaption = base->deriveCaption();
79 mPrimary = base->derivePrimary();
80 mSecondary = base->deriveSecondary();
81 mDetails = base->deriveDetails();
82 }
83 }
84
85 template<class EAble, class EAter = typename AdapterRegistry<EAble>::adapter>
87 Error(const EAble& adapted) : Error(EAter(adapted))
88 {}
89
90//-Instance Functions------------------------------------------------------------------------------------------------------
91public:
92 quint16 typeCode() const;
93 QLatin1StringView typeName() const;
94
95 quint32 value() const;
96 Severity severity() const;
97 QString severityString(bool uc = true) const;
98 QString caption() const;
99 QString primary() const;
100 QString secondary() const;
101 QString details() const;
102
103 bool isValid() const;
104 bool equivalent(const Error& other) const;
105 quint64 code() const;
106 QString hexCode() const;
107 QString toString() const;
108
109 Error& setSeverity(Severity sv);
110 Error withSeverity(Severity sv);
111
112
113//-Operators-------------------------------------------------------------------------------------------------------
114public:
115 bool operator==(const Error& other) const = default;
116 bool operator!=(const Error& other) const = default;
117 operator bool() const;
118
119//-Friend Functions------------------------------------------------------------------------------------------------
120friend QTextStream& ::operator<<(QTextStream& ts, const Error& e);
121};
122
123} // namespace Qx
124
125//-Non-member/Related Functions------------------------------------------------------------------------------------
126QX_CORE_EXPORT QTextStream& operator<<(QTextStream& ts, const Qx::Error& e);
127
128//-Metatype declarations-------------------------------------------------------------------------------------------
129Q_DECLARE_METATYPE(Qx::Error);
130
131//-Macros----------------------------------------------------------------------------------------------------------
132#define QX_DECLARE_ERROR_ADAPTATION(Adaptable, Adapter) \
133 static_assert(Qx::error_adaptation<Adaptable, Adapter>, "Adapter must satisfy the 'error_adapter' concept " \
134 "and be constructable from Adaptable."); \
135 template<> \
136 struct QxErrorPrivate::adapter_registry<Adaptable> { typedef Adapter adapter; };
137
138#endif // QX_ERROR_H
The Error class acts as an interface for an extensible variety of error objects.
Definition qx-error.h:38
Error(const EAble &adapted)
Definition qx-error.h:87
bool operator==(const Error &other) const =default
bool operator!=(const Error &other) const =default
Error(const ErrorType &e)
Definition qx-error.h:68
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 two types form a Qx error adaptation.
Definition qx-abstracterror.h:159
Specifies that a type is a Qx error type.
Definition qx-abstracterror.h:148
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5
QString severityString(Severity sv, bool uc)
Definition qx-global.cpp:51
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:376
Definition qx-error.h:19