File: derror.h

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (151 lines) | stat: -rw-r--r-- 3,296 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef DERROR_H
#define DERROR_H
#include "dtkcore_global.h"
#include <QString>
#include <QDebug>

DCORE_BEGIN_NAMESPACE
/**
 * @brief 对于错误的包装类
 */
class DError
{
public:
    /*!
     * @brief 默认构造函数
     * @attention 错误代码默认为-1,错误信息默认为空
     */
    DError() noexcept
        : m_code(-1)
        , m_msg()
    {
    }

    /*!
     * @brief 拷贝构造函数
     */
    DError(const DError &e) noexcept
        : m_code(e.m_code)
        , m_msg(e.m_msg)
    {
    }

    /*!
     * @brief 移动构造函数
     * @attention 移动后原对象不可用
     */
    DError(DError &&e) noexcept
        : m_code(e.m_code)
        , m_msg(std::move(e).m_msg)
    {
    }

    /*!
     * @brief 构造函数
     * @param[in] code 错误代码
     * @param[in] msg  错误信息
     */
    DError(qint64 code, const QString &msg) noexcept
        : m_code(code)
        , m_msg(msg)
    {
    }

    /*!
     * @brief 构造函数
     * @param[in] code 错误代码
     * @param[in] msg  错误信息
     * @attention 使用此构造函数后原错误信息不可用
     */
    DError(qint64 code, QString &&msg) noexcept
        : m_code(code)
        , m_msg(std::move(msg))
    {
    }

    /*!
     * @brief 重载拷贝赋值运算符
     */
    DError &operator=(const DError &e)
    {
        m_code = e.m_code;
        m_msg = e.m_msg;
        return *this;
    }

    /*!
     * @brief 重载移动赋值运算符
     * @attention 赋值后原对象不可用
     */
    DError &operator=(DError &&e)
    {
        m_code = e.m_code;
        m_msg = std::move(e).m_msg;
        return *this;
    }

    /*!
     * @brief 默认析构函数
     */
    ~DError() = default;

    /*!
     * @brief 获取错误代码
     * @return 错误代码
     */
    qint64 getErrorCode() const noexcept { return m_code; }

    /*!
     * @brief 设置错误代码
     * @param[in] code 错误代码
     */
    void setErrorCode(qint64 code) &noexcept { m_code = code; }

    /*!
     * @brief 获取错误信息
     * @return 错误信息的const引用
     */
    const QString &getErrorMessage() const & { return m_msg; }

    /*!
     * @brief 获取错误信息
     * @attention 函数返回错误信息后,原信息不可用
     * @return 错误信息
     */
    QString getErrorMessage() const && { return std::move(m_msg); }

    /*!
     * @brief 设置错误信息
     * @param[in] msg 错误信息
     */
    void setErrorMessage(const QString &msg) & { m_msg = msg; }

    /*!
     * @brief 重载相等运算符
     */
    friend bool operator==(const DError &x, const DError &y) noexcept { return x.m_code == y.m_code and x.m_msg == y.m_msg; }

    /*!
     * @brief 重载不等运算符
     */
    friend bool operator!=(const DError &x, const DError &y) noexcept { return !(x == y); }

    /*!
     * @brief 重载输出运算符
     */
    friend QDebug operator<<(QDebug debug, const DError &e)
    {
        debug << "Error Code:" << e.m_code << "Message:" << e.m_msg;
        return debug;
    }

private:
    qint64 m_code;
    QString m_msg;
};
DCORE_END_NAMESPACE
#endif