NVIDIA DeepStream SDK API Reference

6.4 Release
infer_options.h
Go to the documentation of this file.
1 
17 #ifndef __NVDSINFERSERVER_OPTIONS_H__
18 #define __NVDSINFERSERVER_OPTIONS_H__
19 
20 #include <infer_datatypes.h>
21 
22 #include <optional>
23 #include <string>
24 #include <unordered_map>
25 
26 #ifdef FOR_PRIVATE
27 #include "infer_common.h"
28 #include "infer_utils.h"
29 #else
30 inline void
31 dsInferLogPrint__(NvDsInferLogLevel level, const char* fmt, ...)
32 {
33  va_list args;
34  va_start(args, fmt);
35  vprintf(fmt, args);
36  va_end(args);
37 }
38 #define safeStr(str) str.c_str()
39 
40 #endif
41 
42 namespace nvdsinferserver {
43 
44 class BufOptions;
45 using SharedBufOptions = std::shared_ptr<BufOptions>;
46 
47 class BufOptions : public IOptions {
48 private:
49  struct D {
50  struct BasicValue {
51  union {
52  int64_t vInt64;
53  uint64_t vUint64;
54  double vDouble;
55  bool vBool;
56  void* vPtr;
57  } value;
59  std::string vStr;
60  template <typename V>
61  inline void setV(const V& v, OptionType t)
62  {
63  *((V*)(void*)&value) = v;
64  this->type = t;
65  }
66  } vHead;
67  std::vector<BasicValue> vArray;
68  };
69 
70 public:
71  OptionType getType(const std::string& key) const override
72  {
73  const auto i = m_Fields.find(key);
74  return (i == m_Fields.end() ? OptionType::oNone : i->second.vHead.type);
75  }
76  bool hasValue(const std::string& key) const override
77  {
78  const auto i = m_Fields.find(key);
79  return (i == m_Fields.end() ? false : true);
80  }
81  uint32_t getCount() const final { return (uint32_t)m_Fields.size(); }
82  std::string getKey(uint32_t idx) const final
83  {
84  assert(idx < m_Fields.size());
85  auto i = m_Fields.cbegin();
86  std::advance(i, idx);
87  return i->first;
88  }
89 
90 private:
91  NvDsInferStatus getValuePtr(const std::string& key, OptionType t, void*& ptr) const override
92  {
93  assert(t != OptionType::oNone && t != OptionType::oArray);
94  auto d = getValueD(key, t);
96  d, NVDSINFER_INVALID_PARAMS, "failed to get pointer value:%s", safeStr(key));
97  if (t == OptionType::oString) {
98  ptr = (void*)&(d->vHead.vStr);
99  } else {
100  ptr = (void*)&(d->vHead.value);
101  }
102  return NVDSINFER_SUCCESS;
103  }
104 
105  NvDsInferStatus getArraySize(const std::string& key, uint32_t& size) const override
106  {
107  auto d = getValueD(key, OptionType::oArray);
108  RETURN_IF_FAILED(d, NVDSINFER_INVALID_PARAMS, "failed to get array value:%s", safeStr(key));
109  size = d->vArray.size();
110  return NVDSINFER_SUCCESS;
111  }
112 
113  NvDsInferStatus getRawPtrArray(
114  const std::string& key, OptionType ot, void** ptrBase, uint32_t size) const override
115  {
116  auto d = getValueD(key, OptionType::oArray);
118  d, NVDSINFER_INVALID_PARAMS, "failed to get pointer array value:%s", safeStr(key));
119  assert(size <= d->vArray.size());
120  for (uint32_t i = 0; i < size; ++i) {
121  auto& each = d->vArray[i];
122  assert(each.type != OptionType::oArray && each.type != OptionType::oNone);
124  each.type == ot, NVDSINFER_INVALID_PARAMS,
125  "query value type:%d doesn't match exact type:%d in array.", (int)ot,
126  (int)each.type);
127  if (ot == OptionType::oString) {
128  ptrBase[i] = (void*)&(each.vStr);
129  } else {
130  ptrBase[i] = (void*)&(each.value);
131  }
132  }
133  return NVDSINFER_SUCCESS;
134  }
135 
136  template <typename In>
137  struct convertType {
138  };
139 
140 public:
141  template <typename T>
142  inline void setValue(const std::string& key, const T& v)
143  {
144  using t = typename convertType<std::remove_const_t<std::remove_reference_t<T>>>::t;
145  auto& field = m_Fields[key];
146  field.vHead.setV<t>(v, oType<t>::v);
147  }
148 
149  template <typename T>
150  inline void setValueArray(const std::string& key, const std::vector<T>& values)
151  {
152  if (values.empty()) {
153  return;
154  }
155  using t = typename convertType<std::remove_const_t<std::remove_reference_t<T>>>::t;
156  auto& field = m_Fields[key];
157  field.vHead.type = OptionType::oArray;
158  field.vArray = std::vector<D::BasicValue>(values.size());
159  for (size_t i = 0; i < values.size(); ++i) {
160  auto& data = field.vArray[i];
161  data.setV<t>(t(values[i]), oType<t>::v);
162  }
163  }
164 
165 private:
166  const D* getValueD(const std::string& key, OptionType t) const
167  {
168  const auto i = m_Fields.find(key);
169  if (i == m_Fields.end()) {
170  InferError("BufOptions: No option:%s found.", safeStr(key));
171  return nullptr;
172  }
173  if (i->second.vHead.type != t) {
174  InferError(
175  "BufOptions: get option:%s but type is not matched.",
176  safeStr(key));
177  return nullptr;
178  }
179  return &(i->second);
180  }
181 
182  std::unordered_map<std::string, D> m_Fields;
183 };
184 
185 template <>
186 inline void
187 BufOptions::D::BasicValue::setV<std::string>(const std::string& v, OptionType t)
188 {
189  this->vStr = v;
190  assert(t == OptionType::oString);
191  this->type = t;
192 }
193 
194 template <typename T>
195 struct BufOptions::convertType<T*> {
196  typedef std::remove_const_t<T>* t;
197 };
198 template <>
199 struct BufOptions::convertType<int64_t> {
200  typedef int64_t t;
201 };
202 template <>
203 struct BufOptions::convertType<int32_t> {
204  typedef int64_t t;
205 };
206 template <>
207 struct BufOptions::convertType<int16_t> {
208  typedef int64_t t;
209 };
210 template <>
211 struct BufOptions::convertType<int8_t> {
212  typedef int64_t t;
213 };
214 template <>
215 struct BufOptions::convertType<uint64_t> {
216  typedef uint64_t t;
217 };
218 template <>
219 struct BufOptions::convertType<uint32_t> {
220  typedef uint64_t t;
221 };
222 template <>
223 struct BufOptions::convertType<uint16_t> {
224  typedef uint64_t t;
225 };
226 template <>
227 struct BufOptions::convertType<uint8_t> {
228  typedef uint64_t t;
229 };
230 template <>
231 struct BufOptions::convertType<double> {
232  typedef double t;
233 };
234 template <>
235 struct BufOptions::convertType<float> {
236  typedef double t;
237 };
238 template <>
239 struct BufOptions::convertType<bool> {
240  typedef bool t;
241 };
242 template <>
243 struct BufOptions::convertType<std::string> {
244  typedef std::string t;
245 };
246 
247 template <typename T> // not supported
248 struct BufOptions::convertType<std::vector<T>> {
249 };
250 
251 template <typename T> // not supported
252 struct BufOptions::convertType<std::vector<T*>> {
253 };
254 
255 } // namespace nvdsinferserver
256 
257 #endif //__NVDSINFERSERVER_OPTIONS_H__
258 
nvdsinferserver
Copyright (c) 2021, NVIDIA CORPORATION.
Definition: infer_custom_process.h:28
nvdsinferserver::BufOptions::D::BasicValue::vInt64
int64_t vInt64
Definition: infer_options.h:52
nvdsinferserver::OptionType::oString
@ oString
nvdsinferserver::OptionType::oNone
@ oNone
nvdsinferserver::BufOptions::D::BasicValue::vPtr
void * vPtr
Definition: infer_options.h:56
nvdsinferserver::BufOptions::convertType< uint64_t >::t
uint64_t t
Definition: infer_options.h:216
nvdsinferserver::IOptions::oType
Definition: infer_ioptions.h:78
nvdsinferserver::BufOptions::D::BasicValue::vDouble
double vDouble
Definition: infer_options.h:54
infer_datatypes.h
Header file for the data types used in the inference processing.
nvdsinferserver::BufOptions::D::BasicValue::vUint64
uint64_t vUint64
Definition: infer_options.h:53
nvdsinferserver::BufOptions::getType
OptionType getType(const std::string &key) const override
Definition: infer_options.h:71
nvdsinferserver::OptionType::oArray
@ oArray
RETURN_IF_FAILED
#define RETURN_IF_FAILED(condition, ret, fmt,...)
Definition: infer_defines.h:75
nvdsinferserver::BufOptions::D::BasicValue::type
OptionType type
Definition: infer_options.h:58
nvdsinferserver::BufOptions::convertType< uint8_t >::t
uint64_t t
Definition: infer_options.h:228
NVDSINFER_SUCCESS
@ NVDSINFER_SUCCESS
NvDsInferContext operation succeeded.
Definition: nvdsinfer.h:220
nvdsinferserver::BufOptions::setValue
void setValue(const std::string &key, const T &v)
Definition: infer_options.h:142
nvdsinferserver::BufOptions::convertType< int32_t >::t
int64_t t
Definition: infer_options.h:204
nvdsinferserver::BufOptions::convertType< float >::t
double t
Definition: infer_options.h:236
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: nvdsinfer.h:249
nvdsinferserver::BufOptions::getKey
std::string getKey(uint32_t idx) const final
Definition: infer_options.h:82
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
nvdsinferserver::BufOptions::D::BasicValue
Definition: infer_options.h:50
infer_common.h
Header file of the common declarations for the nvinferserver library.
nvdsinferserver::BufOptions::convertType< uint16_t >::t
uint64_t t
Definition: infer_options.h:224
nvdsinferserver::SharedBufOptions
std::shared_ptr< BufOptions > SharedBufOptions
Definition: infer_options.h:45
nvdsinferserver::BufOptions::D::BasicValue::setV
void setV(const V &v, OptionType t)
Definition: infer_options.h:61
nvdsinferserver::BufOptions::convertType< T * >::t
std::remove_const_t< T > * t
Definition: infer_options.h:196
nvdsinferserver::BufOptions::setValueArray
void setValueArray(const std::string &key, const std::vector< T > &values)
Definition: infer_options.h:150
nvdsinferserver::BufOptions::convertType< bool >::t
bool t
Definition: infer_options.h:240
nvdsinferserver::BufOptions::convertType< std::string >::t
std::string t
Definition: infer_options.h:244
nvdsinferserver::BufOptions::D::BasicValue::value
union nvdsinferserver::BufOptions::D::BasicValue::@0 value
nvdsinferserver::BufOptions::convertType< int16_t >::t
int64_t t
Definition: infer_options.h:208
nvdsinferserver::BufOptions::D::BasicValue::vStr
std::string vStr
Definition: infer_options.h:59
nvdsinferserver::BufOptions::D::BasicValue::vBool
bool vBool
Definition: infer_options.h:55
nvdsinferserver::BufOptions::convertType< int8_t >::t
int64_t t
Definition: infer_options.h:212
NVDSINFER_INVALID_PARAMS
@ NVDSINFER_INVALID_PARAMS
Invalid parameters were supplied.
Definition: nvdsinfer.h:227
nvdsinferserver::IOptions
Definition: infer_ioptions.h:57
nvdsinferserver::BufOptions::hasValue
bool hasValue(const std::string &key) const override
Definition: infer_options.h:76
nvdsinferserver::BufOptions::getCount
uint32_t getCount() const final
Definition: infer_options.h:81
InferError
#define InferError(fmt,...)
Definition: infer_defines.h:51
safeStr
#define safeStr(str)
Definition: infer_options.h:38
nvdsinferserver::BufOptions
Definition: infer_options.h:47
nvdsinferserver::OptionType
OptionType
Definition: infer_ioptions.h:31
nvdsinferserver::BufOptions::convertType< double >::t
double t
Definition: infer_options.h:232
dsInferLogPrint__
void dsInferLogPrint__(NvDsInferLogLevel level, const char *fmt,...)
Copyright (c) 2019-2021, NVIDIA CORPORATION.
Definition: infer_options.h:31
nvdsinferserver::BufOptions::convertType< int64_t >::t
int64_t t
Definition: infer_options.h:200
nvdsinferserver::BufOptions::convertType< uint32_t >::t
uint64_t t
Definition: infer_options.h:220
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: nvdsinfer.h:218