NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/sources/libs/ds3d/common/defines.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DS3D_COMMON_DEFINES__H
19 #define _DS3D_COMMON_DEFINES__H
20 
21 #include <cassert>
22 #include <fstream>
23 #include <iostream>
24 #include <memory>
25 #include <numeric>
26 
27 #define ENABLE_DEBUG NvDs3dEnableDebug()
28 
29 #ifndef DS3D_DISABLE_CLASS_COPY
30 #define DS3D_DISABLE_CLASS_COPY(NoCopyClass) \
31  NoCopyClass(const NoCopyClass&) = delete; \
32  void operator=(const NoCopyClass&) = delete
33 #endif
34 
35 #undef DS_ASSERT
36 #define DS_ASSERT(...) assert((__VA_ARGS__))
37 
38 #if defined(NDEBUG)
39 #define DS3D_FORMAT_(fmt, ...) fmt "\n", ##__VA_ARGS__
40 #else
41 #define DS3D_FORMAT_(fmt, ...) "%s:%d, " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__
42 #endif
43 
44 
45 #ifdef _WIN32
46 #define DS3D_LOG_PRINT_(out, level, ...) \
47  do { \
48  fprintf(out, #level ": "); \
49  fprintf(out, __VA_ARGS__); \
50  fprintf(out, "\n"); \
51  fflush(out); \
52  } while (0)
53 #else
54 #define DS3D_LOG_PRINT_(out, level, fmt, ...) \
55  fprintf(out, DS3D_FORMAT_(#level ": " fmt, ##__VA_ARGS__)); \
56  fflush(out)
57 #endif
58 
59 #undef LOG_PRINT
60 #undef LOG_DEBUG
61 #undef LOG_ERROR
62 #undef LOG_WARNING
63 #undef LOG_INFO
64 
65 #define LOG_PRINT(fmt, ...) \
66  fprintf(stdout, fmt, ##__VA_ARGS__); \
67  fflush(stdout)
68 
69 #define LOG_DEBUG(fmt, ...) \
70  if (ENABLE_DEBUG) { \
71  DS3D_LOG_PRINT_(stdout, DEBUG, fmt, ##__VA_ARGS__); \
72  }
73 
74 #define LOG_ERROR(fmt, ...) DS3D_LOG_PRINT_(stderr, ERROR, fmt, ##__VA_ARGS__)
75 #define LOG_WARNING(fmt, ...) DS3D_LOG_PRINT_(stderr, WARNING, fmt, ##__VA_ARGS__)
76 #define LOG_INFO(fmt, ...) DS3D_LOG_PRINT_(stdout, INFO, fmt, ##__VA_ARGS__)
77 
78 // check ERROR
79 #define DS3D_FAILED_RETURN(condition, ret, fmt, ...) \
80  do { \
81  if (!(condition)) { \
82  LOG_ERROR(fmt ", check failure", ##__VA_ARGS__); \
83  return (ret); \
84  } \
85  } while (0)
86 
87 #define DS3D_ERROR_RETURN(code, fmt, ...) \
88  do { \
89  ErrCode __cd = (code); \
90  DS3D_FAILED_RETURN(isGood(__cd), __cd, fmt, ##__VA_ARGS__); \
91  } while (0)
92 
93 #define DS3D_THROW_ERROR(statement, code, msg) \
94  if (!(statement)) { \
95  throwError(code, msg); \
96  }
97 
98 #define DS3D_THROW_ERROR_FMT(statement, code, fmt, ...) \
99  if (!(statement)) { \
100  char __smsg[2048] = {0}; \
101  snprintf(__smsg, sizeof(__smsg) - 1, fmt, ##__VA_ARGS__); \
102  throwError(code, __smsg); \
103  }
104 
105 #define DS3D_CHECK_CUDA_ERROR(err, action, fmt, ...) \
106  do { \
107  cudaError_t errnum = (err); \
108  if (errnum != cudaSuccess) { \
109  LOG_ERROR( \
110  fmt ", cuda err_no:%d, err_str:%s", ##__VA_ARGS__, (int)errnum, \
111  cudaGetErrorName(errnum)); \
112  action; \
113  } \
114  } while (0)
115 
116 #define DS3D_UNUSED(a) (void)(a)
117 
118 #undef DS3D_TRY
119 #undef DS3D_CATCH_ERROR
120 #undef DS3D_CATCH_ANY
121 #undef DS3D_CATCH_FULL
122 
123 #define DS3D_TRY try
124 
125 #ifdef _WIN32
126 #define DS3D_CATCH_ERROR(type, errcode, fmt, ...) \
127  catch (const type& e) \
128  { \
129  LOG_ERROR("%s, catched " #type ": %s", fmt, e.what()); \
130  return errcode; \
131  }
132 
133 #define DS3D_CATCH_ANY(errcode, fmt, ...) \
134  catch (...) \
135  { \
136  LOG_ERROR("%s, catched unknown error", fmt); \
137  return errcode; \
138  }
139 #else
140 #define DS3D_CATCH_ERROR(type, errcode, fmt, ...) \
141  catch (const type& e) \
142  { \
143  LOG_ERROR(fmt ", catched " #type ": %s", ##__VA_ARGS__, e.what()); \
144  return errcode; \
145  }
146 
147 #define DS3D_CATCH_ANY(errcode, fmt, ...) \
148  catch (...) \
149  { \
150  LOG_ERROR(fmt ", catched unknown error", ##__VA_ARGS__); \
151  return errcode; \
152  }
153 #endif
154 
155 #ifdef _WIN32
156 #define DS3D_EXPORT_API __declspec(dllexport)
157 #else
158 #define DS3D_EXPORT_API __attribute__((__visibility__("default")))
159 #endif
160 #define DS3D_EXTERN_C_BEGIN extern "C" {
161 #define DS3D_EXTERN_C_END }
162 
163 #define DS3D_STR_PREFIX "DS3D::"
164 #define DS3D_KEY_NAME(name) DS3D_STR_PREFIX name
165 
166 #define REGISTER_TYPE_ID(uint64Id) \
167  static constexpr TIdType __typeid() { return uint64Id; }
168 
169 
170 #define DS3D_ROUND_UP(value, align) (((value) + (align)-1) & (~((align)-1)))
171 
172 #endif // _DS3D_COMMON_DEFINES__H