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 #include "core/common/gsl.h"
23 namespace onnxruntime {
24 namespace common {
25 
27  NONE = 0,
28  SYSTEM = 1,
30 };
31 
32 /**
33  Error code for ONNXRuntime.
34 */
35 enum StatusCode {
36  OK = 0,
37  FAIL = 1,
40  NO_MODEL = 4,
47  EP_FAIL = 11
48 };
49 
50 constexpr const char* StatusCodeToString(StatusCode status) noexcept {
51  switch (status) {
52  case StatusCode::OK:
53  return "SUCCESS";
54  case StatusCode::FAIL:
55  return "FAIL";
57  return "INVALID_ARGUMENT";
59  return "NO_SUCHFILE";
61  return "NO_MODEL";
63  return "ENGINE_ERROR";
65  return "RUNTIME_EXCEPTION";
67  return "INVALID_PROTOBUF";
69  return "MODEL_LOADED";
71  return "NOT_IMPLEMENTED";
73  return "INVALID_GRAPH";
75  return "EP_FAIL";
76  default:
77  return "GENERAL ERROR";
78  }
79 }
80 
81 #ifdef _WIN32
82 constexpr HRESULT StatusCodeToHRESULT(StatusCode status) noexcept {
83  switch (status) {
84  case StatusCode::OK:
85  return S_OK;
86  case StatusCode::FAIL:
87  return E_FAIL;
89  return E_INVALIDARG;
91  return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
93  return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
95  return E_FAIL;
97  return E_FAIL;
99  return HRESULT_FROM_WIN32(ERROR_FILE_CORRUPT);
101  return HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR);
103  return E_NOTIMPL;
105  return HRESULT_FROM_WIN32(ERROR_FILE_CORRUPT);
106  case StatusCode::EP_FAIL:
107  return HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR);
108  default:
109  return E_FAIL;
110  }
111 }
112 #endif
113 
114 class [[nodiscard]] Status {
115  public:
116  Status() noexcept = default;
117 
118  Status(StatusCategory category, int code, const std::string& msg);
119 
120  Status(StatusCategory category, int code, const char* msg);
121 
122  Status(StatusCategory category, int code);
123 
124  GSL_SUPPRESS(r.11)
126  : state_((other.state_ == nullptr) ? nullptr : new State(*other.state_)) {}
127  GSL_SUPPRESS(r.11)
128  Status& operator=(const Status& other) {
129  if (state_ != other.state_) {
130  if (other.state_ == nullptr) {
131  state_.reset();
132  } else {
133  state_.reset(new State(*other.state_));
134  }
135  }
136  return *this;
137  }
138 
139  Status(Status&&) = default;
140  Status& operator=(Status&&) = default;
141  ~Status() = default;
142 
143  bool IsOK() const {
144  return (state_ == nullptr);
145  }
146 
147  int Code() const noexcept;
148 
149  StatusCategory Category() const noexcept;
150 
151  const std::string& ErrorMessage() const noexcept;
152 
153  std::string ToString() const;
154 
155  bool operator==(const Status& other) const {
156  return (this->state_ == other.state_) || (ToString() == other.ToString());
157  }
158 
159  bool operator!=(const Status& other) const {
160  return !(*this == other);
161  }
162 
163  static Status OK() {
164  return Status();
165  }
166 
167  private:
168  static const std::string& EmptyString() noexcept;
169 
170  struct State {
171  State(StatusCategory cat0, int code0, const std::string& msg0)
172  : category(cat0), code(code0), msg(msg0) {}
173 
174  State(StatusCategory cat0, int code0, const char* msg0)
175  : category(cat0), code(code0), msg(msg0) {}
176 
177  const StatusCategory category;
178  const int code;
179  const std::string msg;
180  };
181 
182  // As long as Code() is OK, state_ == nullptr.
183  std::unique_ptr<State> state_;
184 };
185 
186 inline std::ostream& operator<<(std::ostream& out, const Status& status) {
187  return out << status.ToString();
188 }
189 
190 } // namespace common
191 
192 // make Status directly available in the onnxruntime namespace as it is widely used
193 using common::Status;
194 
195 } // namespace onnxruntime
bool IsOK() const
Definition: status.h:143
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
constexpr const char * StatusCodeToString(StatusCode status) noexcept
Definition: status.h:50
std::string ToString() const
static Status OK()
Definition: status.h:163
bool operator!=(const Status &other) const
Definition: status.h:159
std::ostream & operator<<(std::ostream &out, const Status &status)
Definition: status.h:186
GLboolean r
Definition: glcorearb.h:1222
#define const
Definition: zconf.h:214