NVIDIA DeepStream SDK API Reference

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