Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:42:44

0001 // Copyright (c) ONNX Project Contributors
0002 
0003 /*
0004  * SPDX-License-Identifier: Apache-2.0
0005  */
0006 
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <ostream>
0011 #include <string>
0012 #include <utility>
0013 
0014 namespace ONNX_NAMESPACE {
0015 namespace Common {
0016 
0017 enum StatusCategory {
0018   NONE = 0,
0019   CHECKER = 1,
0020   OPTIMIZER = 2,
0021 };
0022 
0023 enum StatusCode {
0024   OK = 0,
0025   FAIL = 1,
0026   INVALID_ARGUMENT = 2,
0027   INVALID_PROTOBUF = 3,
0028 };
0029 
0030 class Status {
0031  public:
0032   Status() noexcept {}
0033 
0034   Status(StatusCategory category, int code, const std::string& msg);
0035 
0036   Status(StatusCategory category, int code);
0037 
0038   Status(const Status& other) {
0039     *this = other;
0040   }
0041 
0042   void operator=(const Status& other) {
0043     if (&other != this) {
0044       if (nullptr == other.state_) {
0045         state_.reset();
0046       } else if (state_ != other.state_) {
0047         state_.reset(new State(*other.state_));
0048       }
0049     }
0050   }
0051 
0052   Status(Status&&) = default;
0053   Status& operator=(Status&&) = default;
0054   ~Status() = default;
0055 
0056   bool IsOK() const noexcept;
0057 
0058   int Code() const noexcept;
0059 
0060   StatusCategory Category() const noexcept;
0061 
0062   const std::string& ErrorMessage() const;
0063 
0064   std::string ToString() const;
0065 
0066   bool operator==(const Status& other) const {
0067     return (this->state_ == other.state_) || (ToString() == other.ToString());
0068   }
0069 
0070   bool operator!=(const Status& other) const {
0071     return !(*this == other);
0072   }
0073 
0074   static const Status& OK() noexcept;
0075 
0076  private:
0077   struct State {
0078     State(StatusCategory cat_, int code_, std::string msg_) : category(cat_), code(code_), msg(std::move(msg_)) {}
0079 
0080     StatusCategory category = StatusCategory::NONE;
0081     int code = 0;
0082     std::string msg;
0083   };
0084 
0085   static const std::string& EmptyString();
0086 
0087   // state_ == nullptr when if status code is OK.
0088   std::unique_ptr<State> state_;
0089 };
0090 
0091 inline std::ostream& operator<<(std::ostream& out, const Status& status) {
0092   return out << status.ToString();
0093 }
0094 
0095 } // namespace Common
0096 } // namespace ONNX_NAMESPACE