forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCStatus.h
More file actions
183 lines (156 loc) · 4.71 KB
/
CStatus.h
File metadata and controls
183 lines (156 loc) · 4.71 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CStatus.h
@Time: 2021/12/17 10:32 下午
@Desc: 命名为 CSTATUS,直接对外提供的是 CStatus 类
***************************/
#ifndef CGRAPH_CSTATUS_H
#define CGRAPH_CSTATUS_H
#include <string>
#include "CBasicDefine.h"
#include "CStrDefine.h"
CGRAPH_NAMESPACE_BEGIN
CGRAPH_INTERNAL_NAMESPACE_BEGIN
/**
* 说明:
* 返回值为0,表示正常逻辑
* 返回值为负整数,表示error逻辑,程序终止执行
* 自定义返回值,请务必遵守以上约定
*/
static const int STATUS_OK = 0; /** 正常流程返回值 */
static const int STATUS_ERR = -1; /** 异常流程返回值 */
static const int STATUS_CRASH = -996; /** 异常流程返回值 */
class CSTATUS {
public:
explicit CSTATUS() = default;
explicit CSTATUS(const std::string &errorInfo,
const std::string &locateInfo = CGRAPH_EMPTY) {
this->error_code_ = STATUS_ERR; // 默认的error code信息
this->error_info_ = errorInfo;
this->error_locate_ = locateInfo;
}
explicit CSTATUS(int errorCode, const std::string &errorInfo,
const std::string &locateInfo = CGRAPH_EMPTY) {
this->error_code_ = errorCode;
this->error_info_ = errorInfo;
this->error_locate_ = locateInfo;
}
CSTATUS(const CSTATUS &status) {
if (status.error_code_ == error_code_) {
return;
}
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}
CSTATUS(const CSTATUS &&status) noexcept {
if (status.error_code_ == error_code_) {
return;
}
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}
CSTATUS& operator=(const CSTATUS& status) {
if (this->error_code_ != status.error_code_) {
// 如果status是正常的话,则所有数据保持不变
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}
return (*this);
}
CSTATUS& operator+=(const CSTATUS& cur) {
/**
* 如果当前状态已经异常,则不做改动
* 如果当前状态正常,并且传入的状态是异常的话,则返回异常
*/
if (!this->isErr() && cur.isErr()) {
this->error_code_ = cur.error_code_;
this->error_info_ = cur.error_info_;
this->error_locate_ = cur.error_locate_;
}
return (*this);
}
/**
* 恢复状态信息
*/
void reset() {
if (this->error_code_ != STATUS_OK) {
this->error_code_ = STATUS_OK;
this->error_info_.clear();
this->error_locate_.clear();
}
}
/**
* 获取异常值信息
* @return
*/
int getCode() const {
return this->error_code_;
}
/**
* 获取异常信息
* @return
*/
const std::string& getInfo() const {
return this->error_info_;
}
/**
* 获取报错位置
* @return
*/
const std::string& getLocate() const {
return this->error_locate_;
}
/**
* 判断当前状态是否可行
* @return
*/
bool isOK() const {
return STATUS_OK == error_code_;
}
/**
* 判断当前状态是否可行
* @return
*/
bool isErr() const {
return error_code_ < STATUS_OK; // 约定异常信息,均为负值
}
/**
* 判断当前状态是否是崩溃了
* @return
*/
bool isCrash() const {
return STATUS_CRASH == error_code_;
}
/**
* 设置异常信息
* @param code
* @param info
* @return
*/
CSTATUS* setInfo(int code, const std::string& info) {
error_code_ = code;
error_info_ = (STATUS_OK == error_code_) ? CGRAPH_EMPTY : info;
return this;
}
/**
* 设置异常信息
* @param info
* @return
*/
CSTATUS* setErrorInfo(const std::string& info) {
error_code_ = STATUS_ERR;
error_info_ = info;
return this;
}
private:
int error_code_ = STATUS_OK; // 错误码信息
std::string error_info_; // 错误信息描述
std::string error_locate_; // 错误发生的具体位置,形如:file|function|line
};
CGRAPH_INTERNAL_NAMESPACE_END
CGRAPH_NAMESPACE_END
#endif //CGRAPH_CSTATUS_H