30 #include <type_traits>
31 #include <unordered_map>
41 namespace onnxruntime {
43 using TimePoint = std::chrono::high_resolution_clock::time_point;
46 #define ORT_UNUSED_PARAMETER(x) (x)
48 #define ORT_UNUSED_PARAMETER(x) (void)(x)
51 #ifndef ORT_HAVE_ATTRIBUTE
52 #ifdef __has_attribute
53 #define ORT_HAVE_ATTRIBUTE(x) __has_attribute(x)
55 #define ORT_HAVE_ATTRIBUTE(x) 0
63 #if ORT_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
64 #undef ORT_ATTRIBUTE_UNUSED
65 #define ORT_ATTRIBUTE_UNUSED __attribute__((__unused__))
67 #define ORT_ATTRIBUTE_UNUSED
70 #ifdef ORT_NO_EXCEPTIONS
75 void PrintFinalMessage(
const char* msg);
79 #define ORT_IGNORE_RETURN_VALUE(fn) \
85 const char*
function, uint32_t line);
89 #if (_MSC_VER && !defined(__PRETTY_FUNCTION__))
90 #define __PRETTY_FUNCTION__ __FUNCTION__
94 #define ORT_WHERE ::onnxruntime::CodeLocation(__FILE__, __LINE__, static_cast<const char*>(__FUNCTION__))
96 #define ORT_WHERE_WITH_STACK \
97 ::onnxruntime::CodeLocation(__FILE__, __LINE__, static_cast<const char*>(__PRETTY_FUNCTION__), ::onnxruntime::GetStackTrace())
99 #ifdef ORT_NO_EXCEPTIONS
101 #define ORT_TRY if (true)
102 #define ORT_CATCH(x) else if (false)
108 #define ORT_HANDLE_EXCEPTION(func)
113 #define ORT_THROW(...) \
115 ::onnxruntime::PrintFinalMessage( \
116 ::onnxruntime::OnnxRuntimeException( \
117 ORT_WHERE_WITH_STACK, ::onnxruntime::MakeString(__VA_ARGS__)) \
123 #define ORT_NOT_IMPLEMENTED(...) \
125 ::onnxruntime::PrintFinalMessage( \
126 ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__)) \
134 #define ORT_ENFORCE(condition, ...) \
136 if (!(condition)) { \
137 ::onnxruntime::PrintFinalMessage( \
138 ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \
139 ::onnxruntime::MakeString(__VA_ARGS__)) \
145 #define ORT_THROW_EX(ex, ...) \
147 ::onnxruntime::PrintFinalMessage( \
148 ::onnxruntime::MakeString(#ex, "(", ::onnxruntime::MakeString(__VA_ARGS__), ")").c_str()); \
155 #define ORT_CATCH(x) catch (x)
156 #define ORT_RETHROW throw;
158 #define ORT_HANDLE_EXCEPTION(func) func()
163 #define ORT_THROW(...) \
164 throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, ::onnxruntime::MakeString(__VA_ARGS__))
167 #define ORT_NOT_IMPLEMENTED(...) \
168 throw ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__))
173 #define ORT_ENFORCE(condition, ...) \
175 if (!(condition)) { \
176 throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \
177 ::onnxruntime::MakeString(__VA_ARGS__)); \
181 #define ORT_THROW_EX(ex, ...) \
182 throw ex(__VA_ARGS__)
186 #define ORT_MAKE_STATUS(category, code, ...) \
187 ::onnxruntime::common::Status(::onnxruntime::common::category, \
188 ::onnxruntime::common::code, \
189 ::onnxruntime::MakeString(__VA_ARGS__))
192 #define ORT_RETURN_IF(condition, ...) \
195 return ::onnxruntime::common::Status(::onnxruntime::common::ONNXRUNTIME, \
196 ::onnxruntime::common::FAIL, \
197 ::onnxruntime::MakeString(ORT_WHERE.ToString(), " ", __VA_ARGS__)); \
202 #define ORT_RETURN_IF_NOT(condition, ...) \
203 ORT_RETURN_IF(!(condition), __VA_ARGS__)
208 #define ORT_DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete
210 #define ORT_DISALLOW_ASSIGNMENT(TypeName) TypeName& operator=(const TypeName&) = delete
212 #define ORT_DISALLOW_COPY_AND_ASSIGNMENT(TypeName) \
213 ORT_DISALLOW_COPY(TypeName); \
214 ORT_DISALLOW_ASSIGNMENT(TypeName)
216 #define ORT_DISALLOW_MOVE(TypeName) \
217 TypeName(TypeName&&) = delete; \
218 TypeName& operator=(TypeName&&) = delete
220 #define ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(TypeName) \
221 ORT_DISALLOW_COPY_AND_ASSIGNMENT(TypeName); \
222 ORT_DISALLOW_MOVE(TypeName)
224 #define ORT_RETURN_IF_ERROR_SESSIONID(expr, session_id) \
226 auto _status = (expr); \
227 if ((!_status.IsOK())) { \
228 ::onnxruntime::LogRuntimeError(session_id, _status, __FILE__, static_cast<const char*>(__FUNCTION__), __LINE__); \
233 #define ORT_RETURN_IF_ERROR_SESSIONID_(expr) ORT_RETURN_IF_ERROR_SESSIONID(expr, session_id_)
234 #define ORT_RETURN_IF_ERROR(expr) ORT_RETURN_IF_ERROR_SESSIONID(expr, 0)
236 #define ORT_THROW_IF_ERROR(expr) \
238 auto _status = (expr); \
239 if ((!_status.IsOK())) { \
240 ::onnxruntime::LogRuntimeError(0, _status, __FILE__, static_cast<const char*>(__FUNCTION__), __LINE__); \
241 ORT_THROW(_status); \
246 #define ORT_CHECK_AND_SET_RETVAL(expr) \
248 if (retval.IsOK()) { \
254 auto end_time = std::chrono::high_resolution_clock::now();
255 return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).
count();
259 return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).
count();
271 inline std::wstring
ToWideString(
const std::wstring&
s) {
return s; }
280 template <
typename Key,
typename... OtherContainerArgs,
281 template <
typename...>
typename AssociativeContainer,
283 inline bool Contains(
const AssociativeContainer<Key, OtherContainerArgs...>& container, LookupKey&& key) {
284 return container.find(std::forward<LookupKey>(key)) != container.end();
bool Contains(const AssociativeContainer< Key, OtherContainerArgs...> &container, LookupKey &&key)
GLsizei const GLchar *const * string
long long TimeDiffMicroSeconds(TimePoint start_time)
std::chrono::high_resolution_clock::time_point TimePoint
std::string ToWideString(const std::string &s)
void LogRuntimeError(uint32_t session_id, const common::Status &status, const char *file, const char *function, uint32_t line)
std::string ToUTF8String(const std::string &s)
constexpr size_t kMaxStrLen
std::vector< std::string > GetStackTrace()