-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.hpp
85 lines (72 loc) · 1.46 KB
/
result.hpp
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
/**
* 此文件用作包装各种返回值,为返回值添加状态。目前未测试模板类参数重载是否存在bug
*/
#ifndef _RESULT_HPP_
#define _RESULT_HPP_
#include <string>
using namespace std;
enum ResultType
{
#ifdef ERROR
#undef ERROR //取消宏定义
#endif
SUCCESS, //有结果,无信息
ERROR, //无结果,有信息
WARN, //有结果,有信息
UNDEFINED //无结果,无信息
};
template <class T>
class Result
{
private:
T data;
string msg;
public:
ResultType type;
Result();
Result(ResultType type);
Result(string msg, ResultType type);
Result(ResultType type, T data);
Result(string msg, ResultType type, T data);
void dataOrDefault(T &data);
string message();
~Result();
};
template <class T>
Result<T>::Result(ResultType type)
{
this->type = type;
}
template <class T>
Result<T>::Result(string msg, ResultType type)
{
this->type = type;
this->msg = msg;
}
template <class T>
Result<T>::Result(ResultType type, T data)
{
this->type = type;
this->data = data;
}
template <class T>
Result<T>::Result(string msg, ResultType type, T data)
{
this->type = type;
this->data = data;
this->msg = msg;
}
template <class T>
void Result<T>::dataOrDefault(T &data)
{
if (this->type == ResultType::SUCCESS)
data = this->data;
}
template <class T>
string Result<T>::message()
{
return this->msg;
}
template <class T>
Result<T>::~Result() {}
#endif