microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
snnn-patch-1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

include/ext_status.h

105lines · modeblame

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