-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.h
86 lines (62 loc) · 2.1 KB
/
status.h
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
#pragma once
#include <memory>
#include <ostream>
#include <string>
namespace mlperf_bench {
enum StatusCategory {
NONE = 0,
SYSTEM = 1,
RUNTIME = 2,
};
enum StatusCode {
OK = static_cast<unsigned int>(0),
FAIL = static_cast<unsigned int>(1),
};
class Status {
public:
Status() noexcept = default;
Status(StatusCategory category, int code, const std::string& msg);
Status(StatusCategory category, int code, const char* msg);
Status(StatusCategory category, int code);
Status(const Status& other)
: state_((other.state_ == nullptr) ? nullptr : std::make_unique<State>(
*other.state_)) {}
Status& operator=(const Status& other) {
if (state_ != other.state_) {
if (other.state_ == nullptr) {
state_.reset();
} else {
state_ = std::make_unique<State>(*other.state_);
}
}
return *this;
}
Status(Status&& other) = default;
Status& operator=(Status&& other) = default;
~Status() = default;
bool IsOK() const { return (state_ == nullptr); }
int Code() const noexcept;
StatusCategory Category() const noexcept;
const std::string& ErrorMessage() const noexcept;
std::string ToString() const;
bool operator==(const Status& other) const {
return (this->state_ == other.state_) ||
(ToString() == other.ToString());
}
bool operator!=(const Status& other) const { return !(*this == other); }
static Status OK() { return Status(); }
private:
static const std::string& EmptyString() noexcept;
struct State {
State(StatusCategory cat0, int code0, const std::string& msg0)
: category(cat0), code(code0), msg(msg0) {}
State(StatusCategory cat0, int code0, const char* msg0)
: category(cat0), code(code0), msg(msg0) {}
const StatusCategory category;
const int code;
const std::string msg;
};
// As long as Code() is OK, state_ == nullptr.
std::unique_ptr<State> state_;
};
}