Debian Codegraph @

include/base/derror.h

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #ifndef DERROR_H
6 #define DERROR_H
7 #include "dtkcore_global.h"
8 #include <QString>
9 #include <QDebug>
10
11 DCORE_BEGIN_NAMESPACE
12 /**
13 * @brief 对于错误的包装类
14 */
15 class DError
16 {
17 public:
18 /*!
19 * @brief 默认构造函数
20 * @attention 错误代码默认为-1,错误信息默认为空
21 */
22 DError() noexcept
23 : m_code(-1)
24 , m_msg()
25 {
26 }
27
28 /*!
29 * @brief 拷贝构造函数
30 */
31 DError(const DError &e) noexcept
32 : m_code(e.m_code)
33 , m_msg(e.m_msg)
34 {
35 }
36
37 /*!
38 * @brief 移动构造函数
39 * @attention 移动后原对象不可用
40 */
41 DError(DError &&e) noexcept
42 : m_code(e.m_code)
43 , m_msg(std::move(e).m_msg)
44 {
45 }
46
47 /*!
48 * @brief 构造函数
49 * @param[in] code 错误代码
50 * @param[in] msg 错误信息
51 */
52 DError(qint64 code, const QString &msg) noexcept
53 : m_code(code)
54 , m_msg(msg)
55 {
56 }
57
58 /*!
59 * @brief 构造函数
60 * @param[in] code 错误代码
61 * @param[in] msg 错误信息
62 * @attention 使用此构造函数后原错误信息不可用
63 */
64 DError(qint64 code, QString &&msg) noexcept
65 : m_code(code)
66 , m_msg(std::move(msg))
67 {
68 }
69
70 /*!
71 * @brief 重载拷贝赋值运算符
72 */
73 DError &operator=(const DError &e)
74 {
75 m_code = e.m_code;
76 m_msg = e.m_msg;
77 return *this;
78 }
79
80 /*!
81 * @brief 重载移动赋值运算符
82 * @attention 赋值后原对象不可用
83 */
84 DError &operator=(DError &&e)
85 {
86 m_code = e.m_code;
87 m_msg = std::move(e).m_msg;
88 return *this;
89 }
90
91 /*!
92 * @brief 默认析构函数
93 */
94 ~DError() = default;
95
96 /*!
97 * @brief 获取错误代码
98 * @return 错误代码
99 */
100 qint64 getErrorCode() const noexcept { return m_code; }
101
102 /*!
103 * @brief 设置错误代码
104 * @param[in] code 错误代码
105 */
106 void setErrorCode(qint64 code) &noexcept { m_code = code; }
107
108 /*!
109 * @brief 获取错误信息
110 * @return 错误信息的const引用
111 */
112 const QString &getErrorMessage() const & { return m_msg; }
113
114 /*!
115 * @brief 获取错误信息
116 * @attention 函数返回错误信息后,原信息不可用
117 * @return 错误信息
118 */
119 QString getErrorMessage() const && { return std::move(m_msg); }
120
121 /*!
122 * @brief 设置错误信息
123 * @param[in] msg 错误信息
124 */
125 void setErrorMessage(const QString &msg) & { m_msg = msg; }
126
127 /*!
128 * @brief 重载相等运算符
129 */
130 friend bool operator==(const DError &x, const DError &y) noexcept { return x.m_code == y.m_code and x.m_msg == y.m_msg; }
131
132 /*!
133 * @brief 重载不等运算符
134 */
135 friend bool operator!=(const DError &x, const DError &y) noexcept { return !(x == y); }
136
137 /*!
138 * @brief 重载输出运算符
139 */
140 friend QDebug operator<<(QDebug debug, const DError &e)
141 {
142 debug << "Error Code:" << e.m_code << "Message:" << e.m_msg;
143 return debug;
144 }
145
146 private:
147 qint64 m_code;
148 QString m_msg;
149 };
150 DCORE_END_NAMESPACE
151 #endif