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