status.h
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// Modifications Copyright (c) Microsoft.
#pragma once
#include <memory>
#include <ostream>
#include <string>
#ifdef _WIN32
#include <winerror.h>
#endif
namespace onnxruntime {
namespace common {
enum StatusCategory {
NONE = 0,
SYSTEM = 1,
ONNXRUNTIME = 2,
};
/**
Error code for ONNXRuntime.
*/
enum StatusCode {
OK = 0,
FAIL = 1,
INVALID_ARGUMENT = 2,
NO_SUCHFILE = 3,
NO_MODEL = 4,
ENGINE_ERROR = 5,
RUNTIME_EXCEPTION = 6,
INVALID_PROTOBUF = 7,
MODEL_LOADED = 8,
NOT_IMPLEMENTED = 9,
INVALID_GRAPH = 10,
EP_FAIL = 11
};
inline const char* StatusCodeToString(StatusCode status) noexcept {
switch (status) {
case StatusCode::OK:
return "SUCCESS";
case StatusCode::FAIL:
return "FAIL";
case StatusCode::INVALID_ARGUMENT:
return "INVALID_ARGUMENT";
case StatusCode::NO_SUCHFILE:
return "NO_SUCHFILE";
case StatusCode::NO_MODEL:
return "NO_MODEL";
case StatusCode::ENGINE_ERROR:
return "ENGINE_ERROR";
case StatusCode::RUNTIME_EXCEPTION:
return "RUNTIME_EXCEPTION";
case StatusCode::INVALID_PROTOBUF:
return "INVALID_PROTOBUF";
case StatusCode::MODEL_LOADED:
return "MODEL_LOADED";
case StatusCode::NOT_IMPLEMENTED:
return "NOT_IMPLEMENTED";
case StatusCode::INVALID_GRAPH:
return "INVALID_GRAPH";
case StatusCode::EP_FAIL:
return "EP_FAIL";
default:
return "GENERAL ERROR";
}
}
#ifdef _WIN32
inline HRESULT StatusCodeToHRESULT(StatusCode status) noexcept {
switch (status)
{
case StatusCode::OK:
return S_OK;
case StatusCode::FAIL:
return E_FAIL;
case StatusCode::INVALID_ARGUMENT:
return E_INVALIDARG;
case StatusCode::NO_SUCHFILE:
return __HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
case StatusCode::NO_MODEL:
return __HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
case StatusCode::ENGINE_ERROR:
return E_FAIL;
case StatusCode::RUNTIME_EXCEPTION:
return E_FAIL;
case StatusCode::INVALID_PROTOBUF:
return __HRESULT_FROM_WIN32(ERROR_FILE_CORRUPT);
case StatusCode::MODEL_LOADED:
return __HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR);
case StatusCode::NOT_IMPLEMENTED:
return E_NOTIMPL;
case StatusCode::INVALID_GRAPH:
return __HRESULT_FROM_WIN32(ERROR_FILE_CORRUPT);
case StatusCode::EP_FAIL:
return __HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR);
default:
return E_FAIL;
}
}
#endif
class Status {
public:
Status() noexcept = default;
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)
: state_((other.state_ == nullptr) ? nullptr : new State(*other.state_)) {}
Status& operator=(const Status& other) {
if (state_ != other.state_) {
if (other.state_ == nullptr) {
state_.reset();
} else {
state_.reset(new State(*other.state_));
}
}
return *this;
}
Status(Status&&) = default;
Status& operator=(Status&&) = default;
~Status() = default;
bool IsOK() const {
return (state_ == nullptr);
}
int Code() const noexcept;
StatusCategory Category() const noexcept;
const std::string& ErrorMessage() const noexcept;
std::string ToString() const;
bool operator==(const Status& other) const {
return (this->state_ == other.state_) || (ToString() == other.ToString());
}
bool operator!=(const Status& other) const {
return !(*this == other);
}
static Status OK() {
return Status();
}
private:
static const std::string& EmptyString() noexcept;
struct State {
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;
};
// As long as Code() is OK, state_ == nullptr.
std::unique_ptr<State> state_;
};
inline std::ostream& operator<<(std::ostream& out, const Status& status) {
return out << status.ToString();
}
} // namespace common
} // namespace onnxruntime