microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
base/status.cc
102lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #include "status.h" |
| 5 | #include "ort_c_to_cpp.h" |
| 6 | |
| 7 | struct OrtxStatus::Rep { |
| 8 | extError_t code{kOrtxOK}; |
| 9 | std::string error_message; |
| 10 | }; |
| 11 | |
| 12 | OrtxStatus::OrtxStatus() = default; |
| 13 | OrtxStatus::~OrtxStatus() = default; |
| 14 | |
| 15 | OrtxStatus::OrtxStatus(extError_t code, const std::string& error_message) |
| 16 | : rep_(new Rep) { |
| 17 | rep_->code = code; |
| 18 | rep_->error_message = std::string(error_message); |
| 19 | } |
| 20 | |
| 21 | OrtxStatus::OrtxStatus(const OrtxStatus& s) |
| 22 | : rep_((s.rep_ == nullptr) ? nullptr : new Rep(*s.rep_)) {} |
| 23 | |
| 24 | OrtxStatus& OrtxStatus::operator=(const OrtxStatus& s) { |
| 25 | if (rep_ != s.rep_) |
| 26 | rep_.reset((s.rep_ == nullptr) ? nullptr : new Rep(*s.rep_)); |
| 27 | |
| 28 | return *this; |
| 29 | } |
| 30 | |
| 31 | bool OrtxStatus::operator==(const OrtxStatus& s) const { return (rep_ == s.rep_); } |
| 32 | |
| 33 | bool OrtxStatus::operator!=(const OrtxStatus& s) const { return (rep_ != s.rep_); } |
| 34 | |
| 35 | const char* OrtxStatus::Message() const { |
| 36 | return IsOk() ? "" : rep_->error_message.c_str(); |
| 37 | } |
| 38 | |
| 39 | void OrtxStatus::SetErrorMessage(const char* str) { |
| 40 | if (rep_ == nullptr) |
| 41 | rep_ = std::make_unique<Rep>(); |
| 42 | rep_->error_message = str; |
| 43 | } |
| 44 | |
| 45 | extError_t OrtxStatus::Code() const { return IsOk() ? extError_t() : rep_->code; } |
| 46 | |
| 47 | OrtStatus* OrtxStatus::CreateOrtStatus() const { |
| 48 | if (IsOk()) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | OrtStatus* status = OrtW::CreateStatus(Message(), OrtErrorCode::ORT_RUNTIME_EXCEPTION); |
| 53 | return status; |
| 54 | } |
| 55 | |
| 56 | std::string OrtxStatus::ToString() const { |
| 57 | if (rep_ == nullptr) |
| 58 | return "OK"; |
| 59 | |
| 60 | std::string result; |
| 61 | switch (Code()) { |
| 62 | case extError_t::kOrtxOK: |
| 63 | result = "Success"; |
| 64 | break; |
| 65 | case extError_t::kOrtxErrorInvalidArgument: |
| 66 | result = "Invalid argument"; |
| 67 | break; |
| 68 | case extError_t::kOrtxErrorOutOfMemory: |
| 69 | result = "Out of Memory"; |
| 70 | break; |
| 71 | case extError_t::kOrtxErrorCorruptData: |
| 72 | result = "Corrupt data"; |
| 73 | break; |
| 74 | case extError_t::kOrtxErrorInvalidFile: |
| 75 | result = "Invalid data file"; |
| 76 | break; |
| 77 | case extError_t::kOrtxErrorNotFound: |
| 78 | result = "Not found"; |
| 79 | break; |
| 80 | case extError_t::kOrtxErrorAlreadyExists: |
| 81 | result = "Already exists"; |
| 82 | break; |
| 83 | case extError_t::kOrtxErrorOutOfRange: |
| 84 | result = "Out of range"; |
| 85 | break; |
| 86 | case extError_t::kOrtxErrorNotImplemented: |
| 87 | result = "Not implemented"; |
| 88 | break; |
| 89 | case extError_t::kOrtxErrorInternal: |
| 90 | result = "Internal"; |
| 91 | break; |
| 92 | case extError_t::kOrtxErrorUnknown: |
| 93 | result = "Unknown"; |
| 94 | break; |
| 95 | default: |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | result += ": "; |
| 100 | result += rep_->error_message; |
| 101 | return result; |
| 102 | } |
| 103 | |