microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
base/status.cc
60lines · 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, std::string_view error_message) |
| 16 | : rep_(new Rep) { |
| 17 | rep_->code = code; |
| 18 | rep_->error_message = std::string(error_message); |
| 19 | } |
| 20 | |
| 21 | OrtxStatus::OrtxStatus(extError_t code, const std::string& error_message) |
| 22 | : rep_(new Rep) { |
| 23 | rep_->code = code; |
| 24 | rep_->error_message = std::string(error_message); |
| 25 | } |
| 26 | |
| 27 | OrtxStatus::OrtxStatus(const OrtxStatus& s) |
| 28 | : rep_((s.rep_ == nullptr) ? nullptr : new Rep(*s.rep_)) {} |
| 29 | |
| 30 | OrtxStatus& OrtxStatus::operator=(const OrtxStatus& s) { |
| 31 | if (rep_ != s.rep_) |
| 32 | rep_.reset((s.rep_ == nullptr) ? nullptr : new Rep(*s.rep_)); |
| 33 | |
| 34 | return *this; |
| 35 | } |
| 36 | |
| 37 | bool OrtxStatus::operator==(const OrtxStatus& s) const { return (rep_ == s.rep_); } |
| 38 | |
| 39 | bool OrtxStatus::operator!=(const OrtxStatus& s) const { return (rep_ != s.rep_); } |
| 40 | |
| 41 | const char* OrtxStatus::Message() const { |
| 42 | return IsOk() ? "" : rep_->error_message.c_str(); |
| 43 | } |
| 44 | |
| 45 | void OrtxStatus::SetErrorMessage(const char* str) { |
| 46 | if (rep_ == nullptr) |
| 47 | rep_ = std::make_unique<Rep>(); |
| 48 | rep_->error_message = str; |
| 49 | } |
| 50 | |
| 51 | extError_t OrtxStatus::Code() const { return IsOk() ? extError_t() : rep_->code; } |
| 52 | |
| 53 | OrtStatus* OrtxStatus::CreateOrtStatus() const { |
| 54 | if (IsOk()) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | OrtStatus* status = OrtW::CreateStatus(Message(), OrtErrorCode::ORT_RUNTIME_EXCEPTION); |
| 59 | return status; |
| 60 | } |
| 61 | |