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