forked from onnx/onnx-tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Status.hpp
203 lines (183 loc) · 7.75 KB
/
Status.hpp
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
/*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "NvOnnxParser.h"
#include <cassert>
#include <string>
#ifndef ENABLE_STD_PLUGIN
#define ENABLE_STD_PLUGIN 1
#endif // ENABLE_STD_PLUGIN
#ifndef ENABLE_SAFE_PLUGIN
#define ENABLE_SAFE_PLUGIN 0
#endif // ENABLE_SAFE_PLUGIN
// Used to strip out build path information from debug prints
#if defined(SOURCE_LENGTH)
#define __FILENAME__ (__FILE__ + SOURCE_LENGTH)
#else
#define __FILENAME__ (__FILE__)
#endif
#define MAKE_ERROR(desc, code) onnx2trt::Status((code), (desc), __FILENAME__, __LINE__, __func__)
#define ASSERT(condition, error_code) \
do \
{ \
if (!(condition)) \
{ \
return MAKE_ERROR("Assertion failed: " #condition, (error_code)); \
} \
} while (0)
#define MAKE_INPUT_ERROR(desc, code, name) Status((code), (desc), name, __LINE__, __func__)
#define ASSERT_INPUT(condition, error_code, name) \
do \
{ \
if (!(condition)) \
{ \
return MAKE_INPUT_ERROR("Assertion failed: " #condition, (error_code), (name)); \
} \
} while (0)
#define ASSERT_C(condition, error_code) \
do \
{ \
if (!(condition)) \
{ \
return error_code; \
} \
} while (0)
#define GET_VALUE(value_or_error_, result_ptr) \
do \
{ \
auto const& value_or_error = value_or_error_; \
if (value_or_error.is_error()) \
{ \
return value_or_error.error(); \
} \
else \
{ \
*result_ptr = value_or_error.value(); \
} \
} while (0)
#define CHECK(call) \
do \
{ \
Status status = call; \
if (!status.is_success()) \
{ \
return status; \
} \
} while (0)
namespace onnx2trt
{
using nvonnxparser::ErrorCode;
class Status : public nvonnxparser::IParserError
{
ErrorCode _code;
std::string _desc;
std::string _file;
int _line;
std::string _func;
int _node;
public:
static Status success()
{
return Status(ErrorCode::kSUCCESS);
}
Status()
{
}
explicit Status(ErrorCode code, std::string desc = "", std::string file = "", int line = 0, std::string func = "",
int node = -1)
: _code(code)
, _desc(desc)
, _file(file)
, _line(line)
, _func(func)
, _node(node)
{
}
ErrorCode code() const override
{
return _code;
}
const char* desc() const override
{
return _desc.c_str();
}
const char* file() const override
{
return _file.c_str();
}
int line() const override
{
return _line;
}
const char* func() const override
{
return _func.c_str();
}
int node() const override
{
return _node;
}
bool is_error() const
{
return _code != ErrorCode::kSUCCESS;
}
bool is_success() const
{
return _code == ErrorCode::kSUCCESS;
}
void setNode(int node)
{
_node = node;
}
};
template <typename T>
class ValueOrStatus
{
bool _is_error;
T _value;
Status _error;
public:
ValueOrStatus(T const& value)
: _is_error(false)
, _value(value)
, _error(Status::success())
{
}
ValueOrStatus(T&& value)
: _is_error(false)
, _value(value)
, _error(Status::success())
{
}
ValueOrStatus(Status const& error)
: _is_error(true)
, _error(error)
{
}
ValueOrStatus(Status&& error)
: _is_error(true)
, _error(error)
{
}
bool is_error() const
{
return _is_error;
}
T const& value() const
{
assert(!_is_error);
return _value;
}
T& value()
{
assert(!_is_error);
return _value;
}
Status const& error() const
{
assert(_is_error);
return _error;
}
};
} // namespace onnx2trt