Status class optimizations (#824)

optimize onnxruntime::common::Status to reduce code size
This commit is contained in:
Tracy Sharpe 2019-04-11 21:57:01 -07:00 committed by GitHub
parent b6936e71cb
commit c55e2de593
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View file

@ -52,6 +52,8 @@ class Status {
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)
@ -72,7 +74,9 @@ class Status {
Status& operator=(Status&& other) = default;
~Status() = default;
bool IsOK() const noexcept;
bool IsOK() const {
return (state_ == nullptr);
}
int Code() const noexcept;
@ -90,7 +94,9 @@ class Status {
return !(*this == other);
}
static const Status& OK() noexcept;
static Status OK() {
return Status();
}
private:
static const std::string& EmptyString() noexcept;
@ -99,6 +105,9 @@ class Status {
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;

View file

@ -23,12 +23,15 @@ Status::Status(StatusCategory category, int code, const std::string& msg) {
state_ = std::make_unique<State>(category, code, msg);
}
Status::Status(StatusCategory category, int code)
: Status(category, code, EmptyString()) {
Status::Status(StatusCategory category, int code, const char* msg) {
// state_ will be allocated here causing the status to be treated as a failure
ORT_ENFORCE(code != static_cast<int>(MLStatus::OK));
state_ = std::make_unique<State>(category, code, msg);
}
bool Status::IsOK() const noexcept {
return (state_ == nullptr);
Status::Status(StatusCategory category, int code)
: Status(category, code, "") {
}
StatusCategory Status::Category() const noexcept {
@ -58,8 +61,6 @@ std::string Status::ToString() const {
result += "[ONNXRuntimeError]";
result += " : ";
result += std::to_string(Code());
std::string msg;
result += " : ";
result += MLStatusToString(static_cast<MLStatus>(Code()));
result += " : ";
@ -76,10 +77,6 @@ std::string Status::ToString() const {
#pragma warning(push)
#pragma warning(disable : 26426)
#endif
const Status& Status::OK() noexcept {
static Status s_ok;
return s_ok;
}
const std::string& Status::EmptyString() noexcept {
static std::string s_empty;