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
136 lines (111 loc) · 3.29 KB
/
Copy pathCStatus.h
File metadata and controls
136 lines (111 loc) · 3.29 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
/***************************
@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
/**
* 说明:
* 返回值为0,表示正常逻辑
* 返回值为负整数,表示error逻辑,程序终止执行
* 自定义返回值,请务必遵守以上约定
*/
static const int STATUS_OK = 0; /** 正常流程返回值 */
static const int STATUS_ERR = -1; /** 异常流程返回值 */
static const int STATUS_CRASH = -996; /** 异常流程返回值 */
static const char* STATUS_ERROR_INFO_CONNECTOR = " && "; /** 多异常信息连接符号 */
class CSTATUS {
public:
explicit CSTATUS() = default;
explicit CSTATUS(const std::string &errorInfo) {
this->error_code_ = STATUS_ERR; // 默认的error code信息
this->error_info_ = errorInfo;
}
explicit CSTATUS(int errorCode, const std::string &errorInfo) {
this->error_code_ = errorCode;
this->error_info_ = errorInfo;
}
CSTATUS(const CSTATUS &status) {
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
}
CSTATUS(const CSTATUS &&status) noexcept {
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
}
CSTATUS& operator=(const CSTATUS& status) = default;
CSTATUS& operator+=(const CSTATUS& cur) {
if (this->isOK() && cur.isOK()) {
return (*this);
}
error_info_ = this->isOK()
? cur.error_info_
: (cur.isOK()
? error_info_
: (error_info_ + STATUS_ERROR_INFO_CONNECTOR + cur.error_info_));
error_code_ = STATUS_ERR;
return (*this);
}
void setStatus(const std::string& info) {
error_code_ = STATUS_ERR;
error_info_ = info;
}
void setStatus(int code, const std::string& info) {
error_code_ = code;
error_info_ = info;
}
/**
* 获取异常值信息
* @return
*/
int getCode() const {
return this->error_code_;
}
/**
* 获取异常信息
* @return
*/
const std::string& getInfo() const {
return this->error_info_;
}
/**
* 恢复数据
*/
void reset() {
error_code_ = STATUS_OK;
error_info_ = CGRAPH_EMPTY;
}
/**
* 判断当前状态是否可行
* @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_;
}
private:
int error_code_ = STATUS_OK; // 错误码信息
std::string error_info_; // 错误信息描述
};
CGRAPH_NAMESPACE_END
#endif //CGRAPH_CSTATUS_H