-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstatus.cpp
More file actions
66 lines (51 loc) · 1.8 KB
/
Copy pathstatus.cpp
File metadata and controls
66 lines (51 loc) · 1.8 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
#include "core/base/include/status.h"
#include "core/base/include/macro.h"
namespace nncore {
Status::Status(StatusCategory category, int code, const std::string& msg) {
nn_throw_if(code != static_cast<int>(StatusCode::OK),
"Status OK should not be constructed by the constructor, use "
"Status::OK() insttead");
state_ = std::make_unique<State>(category, code, msg);
}
Status::Status(StatusCategory category, int code, const char* msg) {
nn_throw_if(code != static_cast<int>(StatusCode::OK),
"Status OK should not be constructed by the constructor, use "
"Status::OK() insttead");
state_ = std::make_unique<State>(category, code, msg);
}
Status::Status(StatusCategory category, int code)
: Status(category, code, "") {}
StatusCategory Status::category() const noexcept {
return is_ok() ? StatusCategory::NONE : state_->category;
}
int Status::code() const noexcept {
return is_ok() ? static_cast<int>(StatusCode::OK) : state_->code;
}
const std::string& Status::error_message() const noexcept {
return is_ok() ? empty_string() : state_->msg;
}
std::string Status::to_string() const {
if (state_ == nullptr) {
return std::string("OK");
}
std::string result;
if (StatusCategory::SYSTEM == state_->category) {
result += "SystemError";
result += " : ";
result += std::to_string(errno);
} else if (StatusCategory::NUMNET == state_->category) {
result += "[Num.NET RuntimeError]";
result += " : ";
result += std::to_string(code());
result += " : ";
result += StatusCodeToString(static_cast<StatusCode>(code()));
result += " : ";
result += state_->msg;
}
return result;
}
const std::string& Status::empty_string() noexcept {
static std::string s_empty;
return s_empty;
}
} // namespace nncore