nndeploy C++ API  0.2.0
nndeploy C++ API
status.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_BASE_STATUS_H_
3 #define _NNDEPLOY_BASE_STATUS_H_
4 
6 #include "nndeploy/base/log.h"
7 #include "nndeploy/base/macro.h"
8 
9 namespace nndeploy {
10 namespace base {
11 
12 enum StatusCode : int {
13  kStatusCodeOk = 0x0000,
14 
16 
25 
26  // device
46 
47  // op
49 
50  // inference
81 
82  //
85 };
86 
88  public:
89  Status();
90  Status(int code);
93 
94  Status(const Status &other);
95  Status &operator=(const Status &other);
96  Status &operator=(const StatusCode &other);
97  Status &operator=(int other);
98 
99  Status(Status &&other);
101 
102  bool operator==(const Status &other) const;
103  bool operator==(const StatusCode &other) const;
104  bool operator==(int other) const;
105 
106  bool operator!=(const Status &other) const;
107  bool operator!=(const StatusCode &other) const;
108  bool operator!=(int other) const;
109 
110  operator int() const;
111  operator bool() const;
112 
113  std::string desc() const;
114 
116 
117  Status operator+(const Status &other);
118 
119  static Status Ok();
120  static Status Error();
121 
122  private:
123  int code_ = kStatusCodeOk;
124 };
125 
127 
128 template <typename T>
130  public:
131  Maybe(const T &value, const Status &status)
132  : has_value_(true), value_(value), status_(status) {}
133  Maybe(const Status &status) : has_value_(false), status_(status) {}
134 
135  virtual ~Maybe() {};
136 
137  Maybe(const Maybe &other) = default;
138  Maybe &operator=(const Maybe &other) = default;
139 
140  bool hasValue() const { return has_value_; }
141  T value() const { return value_; }
142  Status status() const { return status_; }
143 
144  private:
145  bool has_value_ = false;
146  T value_;
147  Status status_;
148 };
149 
150 #define NNDEPLOY_ASSERT(x) \
151  { \
152  int res = (x); \
153  if (!res) { \
154  NNDEPLOY_LOGE("Error: assert failed\n"); \
155  assert(res); \
156  } \
157  }
158 #ifndef _DEBUG
159 #undef NNDEPLOY_LOGDT
160 #undef NNDEPLOY_LOGD
161 #define NNDEPLOY_LOGDT(fmt, tag, ...)
162 #define NNDEPLOY_LOGD(fmt, ...)
163 // #undef NNDEPLOY_ASSERT
164 // #define NNDEPLOY_ASSERT(x)
165 #endif // _DEBUG
166 
167 #define NNDEPLOY_RETURN_VALUE_ON_NEQ(status, expected, value, str) \
168  do { \
169  if (status != (expected)) { \
170  NNDEPLOY_LOGE("%s\n", str); \
171  return (value); \
172  } \
173  } while (0)
174 
175 #define NNDEPLOY_RETURN_VALUE_ON_EQ(status, expected, value, str) \
176  do { \
177  if (status == (expected)) { \
178  NNDEPLOY_LOGE("%s\n", str); \
179  return (value); \
180  } \
181  } while (0)
182 
183 #define NNDEPLOY_RETURN_ON_NEQ(status, expected, str) \
184  do { \
185  if (status != (expected)) { \
186  NNDEPLOY_LOGE("%s\n", str); \
187  return status; \
188  } \
189  } while (0)
190 
191 #define NNDEPLOY_RETURN_ON_EQ(status, expected, str) \
192  do { \
193  if (status == (expected)) { \
194  NNDEPLOY_LOGE("%s\n", str); \
195  return status; \
196  } \
197  } while (0)
198 
199 #define NNDEPLOY_CHECK_PARAM_NULL_RET_STATUS(param, str) \
200  do { \
201  if (!param) { \
202  NNDEPLOY_LOGE("%s\n", str); \
203  return nndeploy::base::kStatusCodeErrorNullParam; \
204  } \
205  } while (0)
206 
207 #define NNDEPLOY_CHECK_PARAM_NULL_RET_NULL(param, str) \
208  do { \
209  if (!param) { \
210  NNDEPLOY_LOGE("%s\n", str); \
211  return nullptr; \
212  } \
213  } while (0)
214 
215 } // namespace base
216 } // namespace nndeploy
217 
218 #endif // _NNDEPLOY_BASE_STATUS_H_
bool hasValue() const
Definition: status.h:140
T value() const
Definition: status.h:141
Maybe(const Maybe &other)=default
Status status() const
Definition: status.h:142
Maybe(const Status &status)
Definition: status.h:133
virtual ~Maybe()
Definition: status.h:135
Maybe & operator=(const Maybe &other)=default
Maybe(const T &value, const Status &status)
Definition: status.h:131
Status & operator=(Status &&other)
Status & operator=(const Status &other)
Status & operator=(const StatusCode &other)
Status(Status &&other)
bool operator!=(const StatusCode &other) const
Status(const Status &other)
bool operator==(const Status &other) const
StatusCode getStatusCode() const
bool operator!=(const Status &other) const
Status & operator=(int other)
std::string desc() const
bool operator==(int other) const
Status(StatusCode code)
static Status Ok()
Status operator+(const Status &other)
static Status Error()
bool operator!=(int other) const
bool operator==(const StatusCode &other) const
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
@ kStatusCodeErrorInferenceSnpe
Definition: status.h:65
@ kStatusCodeErrorInferenceTensorRt
Definition: status.h:53
@ kStatusCodeErrorInferenceLlamaCpp
Definition: status.h:74
@ kStatusCodeErrorInferencePaddleLite
Definition: status.h:61
@ kStatusCodeErrorDeviceCpu
Definition: status.h:27
@ kStatusCodeErrorOpAscendCL
Definition: status.h:48
@ kStatusCodeErrorDeviceMetal
Definition: status.h:36
@ kStatusCodeErrorInferenceOneDiff
Definition: status.h:77
@ kStatusCodeErrorInferenceLLM
Definition: status.h:75
@ kStatusCodeErrorUnknown
Definition: status.h:15
@ kStatusCodeErrorDeviceMtkNpu
Definition: status.h:44
@ kStatusCodeOk
Definition: status.h:13
@ kStatusCodeErrorInferenceTfLite
Definition: status.h:55
@ kStatusCodeErrorNullParam
Definition: status.h:22
@ kStatusCodeErrorInferenceNcnn
Definition: status.h:58
@ kStatusCodeErrorDeviceCuda
Definition: status.h:31
@ kStatusCodeErrorDeviceVulkan
Definition: status.h:37
@ kStatusCodeErrorInferenceMnn
Definition: status.h:60
@ kStatusCodeErrorInferenceRknn
Definition: status.h:62
@ kStatusCodeErrorInferenceTensorFlow
Definition: status.h:69
@ kStatusCodeErrorDeviceSyCL
Definition: status.h:33
@ kStatusCodeErrorInvalidParam
Definition: status.h:21
@ kStatusCodeErrorInferenceVllm
Definition: status.h:71
@ kStatusCodeErrorInferenceDiffusers
Definition: status.h:78
@ kStatusCodeErrorInferenceAscendCL
Definition: status.h:57
@ kStatusCodeErrorInferenceLmdeploy
Definition: status.h:73
@ kStatusCodeErrorInferenceTorch
Definition: status.h:68
@ kStatusCodeErrorInferenceTvm
Definition: status.h:63
@ kStatusCodeNodeInterrupt
Definition: status.h:84
@ kStatusCodeErrorDeviceHexagon
Definition: status.h:38
@ kStatusCodeErrorInferenceSophon
Definition: status.h:67
@ kStatusCodeErrorDeviceX86
Definition: status.h:29
@ kStatusCodeErrorDeviceRkNpu
Definition: status.h:42
@ kStatusCodeErrorInferenceDiff
Definition: status.h:79
@ kStatusCodeErrorInferenceTnn
Definition: status.h:59
@ kStatusCodeErrorInferenceOther
Definition: status.h:80
@ kStatusCodeErrorInferenceOnnxRuntime
Definition: status.h:56
@ kStatusCodeErrorDeviceRocm
Definition: status.h:32
@ kStatusCodeErrorDeviceOpenGL
Definition: status.h:35
@ kStatusCodeErrorNotSupport
Definition: status.h:18
@ kStatusCodeErrorDeviceArm
Definition: status.h:28
@ kStatusCodeErrorInferenceXDit
Definition: status.h:76
@ kStatusCodeErrorInferenceQnn
Definition: status.h:66
@ kStatusCodeErrorDeviceRiscV
Definition: status.h:30
@ kStatusCodeErrorInferenceDefault
Definition: status.h:51
@ kStatusCodeErrorInferenceAITemplate
Definition: status.h:64
@ kStatusCodeErrorDeviceAscendCL
Definition: status.h:40
@ kStatusCodeErrorDeviceSophonNpu
Definition: status.h:45
@ kStatusCodeErrorDag
Definition: status.h:83
@ kStatusCodeErrorDeviceQualcommNpu
Definition: status.h:43
@ kStatusCodeErrorDeviceMtkVpu
Definition: status.h:39
@ kStatusCodeErrorInferenceSGLang
Definition: status.h:72
@ kStatusCodeErrorDeviceOpenCL
Definition: status.h:34
@ kStatusCodeErrorInferenceNeuroPilot
Definition: status.h:70
@ kStatusCodeErrorInvalidValue
Definition: status.h:20
@ kStatusCodeErrorNotImplement
Definition: status.h:19
@ kStatusCodeErrorOutOfMemory
Definition: status.h:17
@ kStatusCodeErrorIO
Definition: status.h:24
@ kStatusCodeErrorDeviceAppleNpu
Definition: status.h:41
@ kStatusCodeErrorInferenceCoreML
Definition: status.h:54
@ kStatusCodeErrorInferenceOpenVino
Definition: status.h:52
@ kStatusCodeErrorThreadPool
Definition: status.h:23
std::string statusCodeToString(StatusCode code)