Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* 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
 
#include "core/session/onnxruntime_error_code.h"
 
namespace onnxruntime {
namespace common {
 
enum StatusCategory {
  NONE = 0,
  SYSTEM = 1,
  ONNXRUNTIME = 2,
};
 
/**
 * Error code for ONNXRuntime.
 *
 * The values are derived from the public C API OrtErrorCode enum so the enum values will match.
 * If a new OrtErrorCode value is introduced, a corresponding value should also be added here.
 */
enum StatusCode {
  OK = ORT_OK,
  FAIL = ORT_FAIL,
  INVALID_ARGUMENT = ORT_INVALID_ARGUMENT,
  NO_SUCHFILE = ORT_NO_SUCHFILE,
  NO_MODEL = ORT_NO_MODEL,
  ENGINE_ERROR = ORT_ENGINE_ERROR,
  RUNTIME_EXCEPTION = ORT_RUNTIME_EXCEPTION,
  INVALID_PROTOBUF = ORT_INVALID_PROTOBUF,
  MODEL_LOADED = ORT_MODEL_LOADED,
  NOT_IMPLEMENTED = ORT_NOT_IMPLEMENTED,
  INVALID_GRAPH = ORT_INVALID_GRAPH,
  EP_FAIL = ORT_EP_FAIL,
  MODEL_LOAD_CANCELED = ORT_MODEL_LOAD_CANCELED,
  MODEL_REQUIRES_COMPILATION = ORT_MODEL_REQUIRES_COMPILATION,
  NOT_FOUND = ORT_NOT_FOUND,
  DEVICE_RESET = ORT_DEVICE_RESET,
};
 
constexpr 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";
    case StatusCode::MODEL_LOAD_CANCELED:
      return "MODEL_LOAD_CANCELED";
    case StatusCode::MODEL_REQUIRES_COMPILATION:
      return "MODEL_REQUIRES_COMPILATION";
    case StatusCode::NOT_FOUND:
      return "NOT_FOUND";
    case StatusCode::DEVICE_RESET:
      return "DEVICE_RESET";
    default:
      return "GENERAL ERROR";
  }
}
 
#ifdef _WIN32
constexpr 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);
    case StatusCode::MODEL_LOAD_CANCELED:
      return HRESULT_FROM_WIN32(ERROR_CANCELLED);
    case StatusCode::MODEL_REQUIRES_COMPILATION:
      return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
    case StatusCode::NOT_FOUND:
      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
    case StatusCode::DEVICE_RESET:
      return E_FAIL;
    default:
      return E_FAIL;
  }
}
#endif
 
class [[nodiscard]] 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
 
// make Status directly available in the onnxruntime namespace as it is widely used
using common::Status;
 
}  // namespace onnxruntime