HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
common.h
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2016-present, Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // Portions Copyright (c) Microsoft Corporation
17 
18 #pragma once
19 
20 #include <climits>
21 #include <cstring>
22 #include <algorithm>
23 #include <chrono>
24 #include <functional>
25 #include <memory>
26 #include <numeric>
27 #include <set>
28 #include <sstream>
29 #include <string>
30 #include <type_traits>
31 #include <unordered_map>
32 #include <utility>
33 #include <vector>
34 
36 #include "core/common/exceptions.h"
38 #include "core/common/status.h"
39 
40 
41 namespace onnxruntime {
42 
43 using TimePoint = std::chrono::high_resolution_clock::time_point;
44 
45 #ifdef _WIN32
46 #define ORT_UNUSED_PARAMETER(x) (x)
47 #else
48 #define ORT_UNUSED_PARAMETER(x) (void)(x)
49 #endif
50 
51 #ifndef ORT_HAVE_ATTRIBUTE
52 #ifdef __has_attribute
53 #define ORT_HAVE_ATTRIBUTE(x) __has_attribute(x)
54 #else
55 #define ORT_HAVE_ATTRIBUTE(x) 0
56 #endif
57 #endif
58 
59 // ORT_ATTRIBUTE_UNUSED
60 //
61 // Prevents the compiler from complaining about or optimizing away variables
62 // that appear unused on Linux
63 #if ORT_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
64 #undef ORT_ATTRIBUTE_UNUSED
65 #define ORT_ATTRIBUTE_UNUSED __attribute__((__unused__))
66 #else
67 #define ORT_ATTRIBUTE_UNUSED
68 #endif
69 
70 #ifdef ORT_NO_EXCEPTIONS
71 // Print the given final message, the message must be a null terminated char*
72 // ORT will abort after printing the message.
73 // For Android, will print to Android system log
74 // For other platforms, will print to stderr
75 void PrintFinalMessage(const char* msg);
76 #endif
77 
78 // macro to explicitly ignore the return value from a function call so Code Analysis doesn't complain
79 #define ORT_IGNORE_RETURN_VALUE(fn) \
80  static_cast<void>(fn)
81 
82 std::vector<std::string> GetStackTrace();
83 // these is a helper function that gets defined by platform/Telemetry
84 void LogRuntimeError(uint32_t session_id, const common::Status& status, const char* file,
85  const char* function, uint32_t line);
86 
87 // __PRETTY_FUNCTION__ isn't a macro on gcc, so use a check for _MSC_VER
88 // so we only define it as one for MSVC
89 #if (_MSC_VER && !defined(__PRETTY_FUNCTION__))
90 #define __PRETTY_FUNCTION__ __FUNCTION__
91 #endif
92 
93 // Capture where a message is coming from. Use __FUNCTION__ rather than the much longer __PRETTY_FUNCTION__
94 #define ORT_WHERE ::onnxruntime::CodeLocation(__FILE__, __LINE__, static_cast<const char*>(__FUNCTION__))
95 
96 #define ORT_WHERE_WITH_STACK \
97  ::onnxruntime::CodeLocation(__FILE__, __LINE__, static_cast<const char*>(__PRETTY_FUNCTION__), ::onnxruntime::GetStackTrace())
98 
99 #ifdef ORT_NO_EXCEPTIONS
100 
101 #define ORT_TRY if (true)
102 #define ORT_CATCH(x) else if (false)
103 #define ORT_RETHROW
104 
105 // In order to ignore the catch statement when a specific exception (not ... ) is caught and referred
106 // in the body of the catch statements, it is necessary to wrap the body of the catch statement into
107 // a lambda function. otherwise the exception referred will be undefined and cause build break
108 #define ORT_HANDLE_EXCEPTION(func)
109 
110 // Throw an exception with optional message.
111 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
112 // DO NOT use a printf format string, as that will not work as you expect.
113 #define ORT_THROW(...) \
114  do { \
115  ::onnxruntime::PrintFinalMessage( \
116  ::onnxruntime::OnnxRuntimeException( \
117  ORT_WHERE_WITH_STACK, ::onnxruntime::MakeString(__VA_ARGS__)) \
118  .what()); \
119  abort(); \
120  } while (false)
121 
122 // Just in order to mark things as not implemented. Do not use in final code.
123 #define ORT_NOT_IMPLEMENTED(...) \
124  do { \
125  ::onnxruntime::PrintFinalMessage( \
126  ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__)) \
127  .what()); \
128  abort(); \
129  } while (false)
130 
131 // Check condition.
132 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
133 // DO NOT use a printf format string, as that will not work as you expect.
134 #define ORT_ENFORCE(condition, ...) \
135  do { \
136  if (!(condition)) { \
137  ::onnxruntime::PrintFinalMessage( \
138  ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \
139  ::onnxruntime::MakeString(__VA_ARGS__)) \
140  .what()); \
141  abort(); \
142  } \
143  } while (false)
144 
145 #define ORT_THROW_EX(ex, ...) \
146  do { \
147  ::onnxruntime::PrintFinalMessage( \
148  ::onnxruntime::MakeString(#ex, "(", ::onnxruntime::MakeString(__VA_ARGS__), ")").c_str()); \
149  abort(); \
150  } while (false)
151 
152 #else
153 
154 #define ORT_TRY try
155 #define ORT_CATCH(x) catch (x)
156 #define ORT_RETHROW throw;
157 
158 #define ORT_HANDLE_EXCEPTION(func) func()
159 
160 // Throw an exception with optional message.
161 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
162 // DO NOT use a printf format string, as that will not work as you expect.
163 #define ORT_THROW(...) \
164  throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, ::onnxruntime::MakeString(__VA_ARGS__))
165 
166 // Just in order to mark things as not implemented. Do not use in final code.
167 #define ORT_NOT_IMPLEMENTED(...) \
168  throw ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__))
169 
170 // Check condition.
171 // NOTE: The arguments get streamed into a string via ostringstream::operator<<
172 // DO NOT use a printf format string, as that will not work as you expect.
173 #define ORT_ENFORCE(condition, ...) \
174  do { \
175  if (!(condition)) { \
176  throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \
177  ::onnxruntime::MakeString(__VA_ARGS__)); \
178  } \
179  } while (false)
180 
181 #define ORT_THROW_EX(ex, ...) \
182  throw ex(__VA_ARGS__)
183 
184 #endif
185 
186 #define ORT_MAKE_STATUS(category, code, ...) \
187  ::onnxruntime::common::Status(::onnxruntime::common::category, \
188  ::onnxruntime::common::code, \
189  ::onnxruntime::MakeString(__VA_ARGS__))
190 
191 // Check condition. if met, return status.
192 #define ORT_RETURN_IF(condition, ...) \
193  do { \
194  if (condition) { \
195  return ::onnxruntime::common::Status(::onnxruntime::common::ONNXRUNTIME, \
196  ::onnxruntime::common::FAIL, \
197  ::onnxruntime::MakeString(ORT_WHERE.ToString(), " ", __VA_ARGS__)); \
198  } \
199  } while (false)
200 
201 // Check condition. if not met, return status.
202 #define ORT_RETURN_IF_NOT(condition, ...) \
203  ORT_RETURN_IF(!(condition), __VA_ARGS__)
204 
205 // Macros to disable the copy and/or move ctor and assignment methods
206 // These are usually placed in the private: declarations for a class.
207 
208 #define ORT_DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete
209 
210 #define ORT_DISALLOW_ASSIGNMENT(TypeName) TypeName& operator=(const TypeName&) = delete
211 
212 #define ORT_DISALLOW_COPY_AND_ASSIGNMENT(TypeName) \
213  ORT_DISALLOW_COPY(TypeName); \
214  ORT_DISALLOW_ASSIGNMENT(TypeName)
215 
216 #define ORT_DISALLOW_MOVE(TypeName) \
217  TypeName(TypeName&&) = delete; \
218  TypeName& operator=(TypeName&&) = delete
219 
220 #define ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(TypeName) \
221  ORT_DISALLOW_COPY_AND_ASSIGNMENT(TypeName); \
222  ORT_DISALLOW_MOVE(TypeName)
223 
224 #define ORT_RETURN_IF_ERROR_SESSIONID(expr, session_id) \
225  do { \
226  auto _status = (expr); \
227  if ((!_status.IsOK())) { \
228  ::onnxruntime::LogRuntimeError(session_id, _status, __FILE__, static_cast<const char*>(__FUNCTION__), __LINE__); \
229  return _status; \
230  } \
231  } while (0)
232 
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)
235 
236 #define ORT_THROW_IF_ERROR(expr) \
237  do { \
238  auto _status = (expr); \
239  if ((!_status.IsOK())) { \
240  ::onnxruntime::LogRuntimeError(0, _status, __FILE__, static_cast<const char*>(__FUNCTION__), __LINE__); \
241  ORT_THROW(_status); \
242  } \
243  } while (0)
244 
245 // use this macro when cannot early return
246 #define ORT_CHECK_AND_SET_RETVAL(expr) \
247  do { \
248  if (retval.IsOK()) { \
249  retval = (expr); \
250  } \
251  } while (0)
252 
253 inline long long TimeDiffMicroSeconds(TimePoint start_time) {
254  auto end_time = std::chrono::high_resolution_clock::now();
255  return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
256 }
257 
258 inline long long TimeDiffMicroSeconds(TimePoint start_time, TimePoint end_time) {
259  return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
260 }
261 
262 struct null_type {};
263 inline std::string ToUTF8String(const std::string& s) { return s; }
264 #ifdef _WIN32
265 /**
266  * Convert a wide character string to a UTF-8 string
267  */
268 std::string ToUTF8String(const std::wstring& s);
269 
270 std::wstring ToWideString(const std::string& s);
271 inline std::wstring ToWideString(const std::wstring& s) { return s; }
272 #else
273 inline std::string ToWideString(const std::string& s) { return s; }
274 #endif
275 
276 constexpr size_t kMaxStrLen = 2048;
277 
278 // Returns whether `key` is in `container`.
279 // Like C++20's map/set contains() member function.
280 template <typename Key, typename... OtherContainerArgs,
281  template <typename...> typename AssociativeContainer,
282  typename LookupKey>
283 inline bool Contains(const AssociativeContainer<Key, OtherContainerArgs...>& container, LookupKey&& key) {
284  return container.find(std::forward<LookupKey>(key)) != container.end();
285 }
286 
287 } // namespace onnxruntime
bool Contains(const AssociativeContainer< Key, OtherContainerArgs...> &container, LookupKey &&key)
Definition: common.h:283
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
long long TimeDiffMicroSeconds(TimePoint start_time)
Definition: common.h:253
GLdouble s
Definition: glad.h:3009
std::chrono::high_resolution_clock::time_point TimePoint
Definition: common.h:43
std::string ToWideString(const std::string &s)
Definition: common.h:273
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)
Definition: common.h:263
constexpr size_t kMaxStrLen
Definition: common.h:276
std::vector< std::string > GetStackTrace()
GLint GLsizei count
Definition: glcorearb.h:405