NVIDIA DeepStream SDK API Reference

7.0 Release
obj.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 
14 #ifndef _DS3D_COMMON_HPP_OBJ_HPP
15 #define _DS3D_COMMON_HPP_OBJ_HPP
16 
17 #include <ds3d/common/abi_obj.h>
18 #include <ds3d/common/common.h>
19 #include <ds3d/common/func_utils.h>
20 
21 #define DS3D_REF_COPY_DESTROY_IMPL(clss) \
22  void destroy() final { delete this; } \
23  ~clss() override = default; \
24  abiRefObj* refCopy_i() const final { return new clss(*this); }
25 
26 namespace ds3d {
27 
28 template <class T>
29 using ShrdPtr = std::shared_ptr<T>;
30 template <class T>
31 using UniqPtr = std::unique_ptr<T, std::function<void(T*)>>;
32 template <class T>
33 using Ptr = ShrdPtr<T>;
34 
35 template <class T>
36 void
38 {
39  if (t) {
40  delete t;
41  }
42 }
43 
44 template <typename T>
46  typename std::enable_if_t<TpId<std::remove_cv_t<T>>::__typeid() >= 0, bool>;
47 
48 template <typename T, typename... Args>
49 using _IsConstructible = typename std::is_constructible<T, Args...>::value;
50 
51 template <typename T, typename... Args>
53  typename std::enable_if<std::is_constructible<T, Args...>::value, bool>::type;
54 
55 template <class From, class To>
56 using _Convertible = std::enable_if_t<
57  std::is_convertible<From, To>::value ||
58  std::is_constructible<std::remove_reference<To>, std::remove_reference<From>>::value,
59  bool>;
60 template <class Base, class Derived>
61 using _EnableIfBaseOf = std::enable_if_t<std::is_base_of<Base, Derived>::value, bool>;
62 
63 template <class From, class To>
64 using _PtrConvertible = std::enable_if_t<
65  std::is_convertible<From*, To*>::value ||
66  std::is_constructible<std::remove_reference<To*>, std::remove_reference<From*>>::value ||
67  std::is_base_of<From, To>::value,
68  bool>;
69 
70 template <class From, class To, class convertType>
71 To* pointerCast(From* a);
72 
73 template <class From, class To, _Convertible<From*, To*> = true>
74 To*
75 pointerCast(From* a)
76 {
77  return static_cast<To*>(a);
78 }
79 
80 template <class From, class To, _EnableIfBaseOf<From, To> = true>
81 To*
82 pointerCast(From* a)
83 {
84  return dynamic_cast<To*>(a);
85 }
86 
87 template <class From, class To, std::enable_if_t<std::is_void<From>::value, bool> = true>
88 To*
89 pointerCast(From* a)
90 {
91  return static_cast<To*>(a);
92 }
93 
94 template <class From, class To>
95 class abiRefCast : public abiRefT<To> {
96  abiRefT<From>* _from = nullptr;
97  abiRefCast() = default;
98 
99 public:
100  abiRefCast(const abiRefT<From>& from) { _from = from.refCopy(); }
101  abiRefCast(abiRefT<From>* from, bool takeOwner)
102  {
103  if (takeOwner) {
104  _from = from;
105  } else {
106  _from = from->refCopy();
107  }
108  }
109  void reset()
110  {
111  if (_from) {
112  _from->destroy();
113  _from = nullptr;
114  }
115  }
117  void destroy() final
118  {
119  reset();
120  delete this;
121  }
122  To* data() const final { return pointerCast<From, To>(_from->data()); }
123  abiRefObj* refCopy_i() const final
124  {
125  auto newptr = new abiRefCast();
126  newptr->_from = _from->refCopy();
127  return newptr;
128  }
130 };
131 
132 template <class Tp>
133 class SharedRefObj : public abiRefT<Tp> {
134  ShrdPtr<Tp> _ptr;
135 
136 public:
137  SharedRefObj(ShrdPtr<Tp>&& v) : _ptr(std::move(v)) { DS_ASSERT(_ptr); }
138  template <class Ty, _PtrConvertible<Ty, Tp> = true>
140  {
141  DS_ASSERT(v);
142  Tp* d = pointerCast<Ty, Tp>(v.get());
143  _ptr = ShrdPtr<Tp>(std::move(v), d);
144  }
145  // template <class DelF>
146  // SharedRefObj(Tp* v, DelF f) : _ptr(v, std::move(f))
147  SharedRefObj(Tp* v, std::function<void(Tp*)> f) : _ptr(v, (f ? std::move(f) : [](Tp*) {})) {}
148 
149  Tp* data() const final { return _ptr.get(); }
151 };
152 
153 template <class Tp>
154 abiRefT<Tp>*
155 NewAbiRef(Tp* rawAbiObj)
156 {
157  return new SharedRefObj<Tp>(rawAbiObj, &DeleteTFunc<Tp>);
158 }
159 
161 
162 template <class From, class To = From>
163 inline SharedRefObj<To>*
165 {
166  return new SharedRefObj<To>(std::move(p));
167 }
168 
169 template <class From, class To = From>
170 inline ShrdPtr<To>
172 {
173  if (!p.data())
174  return nullptr;
175  abiRefT<From>* copy = p.refCopy();
176  DS_ASSERT(copy);
177  return ShrdPtr<To>(pointerCast<From, To>(p.data()), [copy](To*) {
178  if (copy) {
179  copy->destroy();
180  }
181  });
182 }
183 
184 /* Examples:
185  * auto f = [](ErrCode c, const char* msg){
186  * printf("error: %s detected, msg: %s", ErrCodeStr(c), msg);
187  * }
188  * // decltype(f) should be same as CBObjT<ErrCode, const char*>::cbType
189  * CBObjT<ErrCode, const char*> cbObj(std::move(f));
190  */
191 template <typename... Args>
192 class CBObjT : public abiCallBackT<Args...> {
193 public:
194  using cbType = std::function<void(Args...)>;
195  bool isValid() const { return bool(_f); }
196  CBObjT(cbType&& f) { _f = std::move(f); }
197  void notify(Args... args) final
198  {
199  if (_f)
200  _f(args...);
201  }
203 private:
204  cbType _f;
205 };
206 
212 template <typename ref, _EnableIfBaseOf<abiRefObj, ref> = true>
213 class GuardRef {
214  ref* _abiRef = nullptr;
215 
216 public:
217  GuardRef() = default;
218  // make a safe copy on data reference abiref
219  GuardRef(const ref& abiref)
220  {
221  DS_ASSERT((std::is_base_of<abiRefObj, ref>::value));
222  _abiRef = static_cast<ref*>(abiref.refCopy_i());
223  }
224  // take owership of the data reference abiref pointer
225  GuardRef(ref* abiref, bool takeowner)
226  {
227  DS_ASSERT(std::is_base_of<abiRefObj, ref>::value);
228  if (takeowner) {
229  _abiRef = abiref;
230  } else {
231  _abiRef = abiref->refCopy();
232  }
233  }
234  // move constructor
236  {
237  _abiRef = o._abiRef;
238  o._abiRef = nullptr;
239  }
240  // copy from another GuardRef
241  GuardRef(const GuardRef& o)
242  {
243  _abiRef = o._abiRef ? static_cast<ref*>(o._abiRef->refCopy_i()) : nullptr;
244  //DS_ASSERT(_abiRef);
245  }
246  // copy from another GuardRef
248  {
249  _abiRef = o._abiRef ? static_cast<ref*>(o._abiRef->refCopy_i()) : nullptr;
250  return *this;
251  }
252  // release this abiRef. user need to take owership of the released abiRef and destroy after use.
253  ref* release()
254  {
255  ref* res = _abiRef;
256  _abiRef = nullptr;
257  return res;
258  }
259  // reset and destroy the abiRef
260  void reset(ref* abiref = nullptr)
261  {
262  if (_abiRef) {
263  _abiRef->destroy();
264  }
265  _abiRef = abiref;
266  }
267  // destructor
268  virtual ~GuardRef() { reset(nullptr); }
269  ref* abiRef() const { return _abiRef; }
270 };
271 
272 /*
273  * examples to get GuardCB from abiCallBackT:
274  * using abiErrorCB = abiCallBackT<ErrCode, const char*>;
275  * const abiErrorCB& cb = ...;
276  * GuardCB<abiErrorCB> guardCB(cb);
277  * guardCB(ErrCode::kGood, "sample");
278  * examples to get GuardCB from std::function
279  * auto errFn = [](ErrCode, const char*) {...};
280  * GuardCB<abiErrorCB> guardCB(errFn);
281  * guardCB(ErrCode::kGood, "sample");
282  */
283 
284 template <typename abiCB>
285 class GuardCB : public GuardRef<abiCB> {
287 
288 public:
289  GuardCB() = default;
290  GuardCB(const abiCB& cb) : _GuardCBBase(cb) {}
291  GuardCB(abiCB* cb, bool takeowner = true) : _GuardCBBase(cb, takeowner) {}
292  GuardCB(nullptr_t) {}
293  GuardCB(const GuardCB& o) : _GuardCBBase(o) {}
294  ~GuardCB() override = default;
295 
296  template <typename... Args, typename F>
297  void setFn(F f)
298  {
299  auto obj = std::make_unique<CBObjT<Args...>>(std::move(f));
300  if (obj && obj->isValid()) {
301  this->reset(obj.release());
302  } else {
303  this->reset();
304  }
305  }
306 
307  // disable operator ->, redirect it to callback abi object.
308  // abiCB* operator->() const { return this->abiRef(); }
309  // check whether it is intialized
310  operator bool() { return this->abiRef(); }
311 
312  template <typename... Args>
313  void operator()(Args&&... args) const
314  {
315  DS_ASSERT(this->abiRef());
316  this->abiRef()->notify(std::forward<Args>(args)...);
317  }
318 };
319 
320 /*
321  * examples:
322  * const abiRefT<abiDataMap>& a_data_map_ref = ...;
323  * GuardDataT<abiDataMap> guard(a_data_map_ref);
324  * guard->bytes(); // -> point to abiDataMap interface.
325  * guard.abiRef() // get abiDataMap ref. abiRefT<abiDataMap>
326  * GuardDataT<abiDataMap> another_guard = guard; // copy into another guard.
327  * Ptr<abiDataMap> data_ptr = guard; // get a abiDataMap safe pointer, which could be copied into
328  * everywhere
329  */
330 template <class Tp>
331 class GuardDataT : public GuardRef<abiRefT<Tp>> {
332 public:
334  GuardDataT() = default;
335  GuardDataT(nullptr_t) {}
336  GuardDataT(const abiRefT<Tp>& rf) : GuardRef<abiRefT<Tp>>(rf) {}
337  GuardDataT(abiRefT<Tp>* refPtr, bool takeOwner) : GuardRef<abiRefT<Tp>>(refPtr, takeOwner) {}
338  GuardDataT(const GuardDataT& o): GuardRef<abiRefT<Tp>>(o) {}
339  ~GuardDataT() override = default;
340 
341  // convert from a derived reference
342  template <class Ty, _PtrConvertible<Ty, Tp> = true>
343  GuardDataT(const abiRefT<Ty>& derived)
344  {
345  this->reset(new abiRefCast<Ty, Tp>(derived));
346  }
347 
348  // convert from a derived ref pointer
349  template <class Ty, _PtrConvertible<Ty, Tp> = true>
350  GuardDataT(abiRefT<Ty>* abiref, bool takeOwner)
351  {
352  this->reset(new abiRefCast<Ty, Tp>(abiref, takeOwner));
353  }
354 
355  template<class GuardTy>
356  GuardTy cast() {
357  return GuardTy(this->abiRef(), false);
358  }
359 
360  // return pointer of the actual data.
361  Tp* ptr() const
362  {
363  if (this->abiRef())
364  return this->abiRef()->data();
365  return nullptr;
366  }
367  // override operator ->, redirect it to abiData object.
368  Tp* operator->() const { return ptr(); }
369  // check whether it is intialized
370  operator bool() const { return ptr(); }
371  // convert it into shared_ptr<Tp> for externer safe use
372  operator ShrdPtr<Tp>()
373  {
374  if (this->abiRef()) {
375  return AbiRefToPtr(*(this->abiRef()));
376  }
377  return nullptr;
378  }
379 };
380 
381 } // namespace ds3d
382 
383 #endif // _DS3D_COMMON_HPP_OBJ_HPP
ds3d::SharedRefObj::SharedRefObj
SharedRefObj(Tp *v, std::function< void(Tp *)> f)
Definition: obj.hpp:147
ds3d::UniqPtr
std::unique_ptr< T, std::function< void(T *)> > UniqPtr
Definition: obj.hpp:31
DS_ASSERT
#define DS_ASSERT(...)
Definition: defines.h:31
ds3d::abiRefCast::destroy
void destroy() final
Definition: obj.hpp:117
ds3d::CBObjT::isValid
bool isValid() const
Definition: obj.hpp:195
ds3d::GuardCB::GuardCB
GuardCB(abiCB *cb, bool takeowner=true)
Definition: obj.hpp:291
ds3d::abiRefT::refCopy
abiRefT * refCopy() const
Definition: abi_obj.h:41
ds3d::GuardRef::~GuardRef
virtual ~GuardRef()
Definition: obj.hpp:268
ds3d::abiRefCast
Definition: obj.hpp:95
ds3d::_Convertible
std::enable_if_t< std::is_convertible< From, To >::value||std::is_constructible< std::remove_reference< To >, std::remove_reference< From > >::value, bool > _Convertible
Definition: obj.hpp:59
ds3d::GuardRef::abiRef
ref * abiRef() const
Definition: obj.hpp:269
ds3d::CBObjT::CBObjT
CBObjT(cbType &&f)
Definition: obj.hpp:196
ds3d::GuardDataT::GuardDataT
GuardDataT(const abiRefT< Tp > &rf)
Definition: obj.hpp:336
ds3d::_PtrConvertible
std::enable_if_t< std::is_convertible< From *, To * >::value||std::is_constructible< std::remove_reference< To * >, std::remove_reference< From * > >::value||std::is_base_of< From, To >::value, bool > _PtrConvertible
Definition: obj.hpp:68
ds3d::SharedRefObj::data
Tp * data() const final
Definition: obj.hpp:149
ds3d::GuardCB::operator()
void operator()(Args &&... args) const
Definition: obj.hpp:313
ds3d::abiRefCast::abiRefCast
abiRefCast(abiRefT< From > *from, bool takeOwner)
Definition: obj.hpp:101
ds3d::abiRefCast::~abiRefCast
~abiRefCast()
Definition: obj.hpp:116
ds3d::abiRefT
Definition: abi_obj.h:39
ds3d::NewAbiRef
abiRefT< Tp > * NewAbiRef(Tp *rawAbiObj)
Definition: obj.hpp:155
ds3d::GuardRef
Guard to wrapper all abiRefObj& data.
Definition: obj.hpp:213
ds3d::GuardDataT
Definition: obj.hpp:331
ds3d::abiRefCast::reset
void reset()
Definition: obj.hpp:109
ds3d::abiRefObj::destroy
virtual void destroy()=0
ds3d::GuardCB::setFn
void setFn(F f)
Definition: obj.hpp:297
ds3d::pointerCast
To * pointerCast(From *a)
Definition: obj.hpp:75
ds3d::GuardCB::GuardCB
GuardCB(const abiCB &cb)
Definition: obj.hpp:290
ds3d::DeleteTFunc
void DeleteTFunc(T *t)
Definition: obj.hpp:37
ds3d::AbiRefToPtr
ShrdPtr< To > AbiRefToPtr(const abiRefT< From > &p)
Definition: obj.hpp:171
ds3d::SharedRefObj::SharedRefObj
SharedRefObj(ShrdPtr< Tp > &&v)
Definition: obj.hpp:137
ds3d::CBObjT
Definition: obj.hpp:192
ds3d::CBObjT::cbType
std::function< void(Args...)> cbType
Definition: obj.hpp:194
ds3d::GuardRef::GuardRef
GuardRef(GuardRef &&o)
Definition: obj.hpp:235
ds3d::GuardRef::operator=
GuardRef & operator=(const GuardRef &o)
Definition: obj.hpp:247
ds3d::GuardRef::reset
void reset(ref *abiref=nullptr)
Definition: obj.hpp:260
ds3d::abiRefCast::DS3D_DISABLE_CLASS_COPY
DS3D_DISABLE_CLASS_COPY(abiRefCast)
ds3d::GuardRef::GuardRef
GuardRef(const GuardRef &o)
Definition: obj.hpp:241
ds3d::GuardDataT::cast
GuardTy cast()
Definition: obj.hpp:356
ds3d::GuardDataT::GuardDataT
GuardDataT(abiRefT< Tp > *refPtr, bool takeOwner)
Definition: obj.hpp:337
ds3d::GuardDataT::operator->
Tp * operator->() const
Definition: obj.hpp:368
ds3d::abiRefCast::refCopy_i
abiRefObj * refCopy_i() const final
Definition: obj.hpp:123
common.h
DS3D_REF_COPY_DESTROY_IMPL
#define DS3D_REF_COPY_DESTROY_IMPL(clss)
Definition: obj.hpp:21
ds3d::GuardDataT::ptr
Tp * ptr() const
Definition: obj.hpp:361
ds3d::_EnableIfValidIdType
typename std::enable_if_t< TpId< std::remove_cv_t< T > >::__typeid() >=0, bool > _EnableIfValidIdType
Definition: obj.hpp:46
ds3d::GuardDataT::GuardDataT
GuardDataT(const GuardDataT &o)
Definition: obj.hpp:338
ds3d::ShrdPtr
std::shared_ptr< T > ShrdPtr
Definition: obj.hpp:29
ds3d::SharedRefObj::SharedRefObj
SharedRefObj(ShrdPtr< Ty > &&v)
Definition: obj.hpp:139
ds3d::abiRefObj
Definition: abi_obj.h:23
ds3d::Ptr
ShrdPtr< T > Ptr
Definition: obj.hpp:33
ds3d::GuardRef::release
ref * release()
Definition: obj.hpp:253
ds3d::GuardCB::GuardCB
GuardCB(const GuardCB &o)
Definition: obj.hpp:293
ds3d::GuardDataT::GuardDataT
GuardDataT(abiRefT< Ty > *abiref, bool takeOwner)
Definition: obj.hpp:350
ds3d::PtrToAbiRef
SharedRefObj< To > * PtrToAbiRef(ShrdPtr< From > &&p)
Definition: obj.hpp:164
ds3d::CBObjT::notify
void notify(Args... args) final
Definition: obj.hpp:197
func_utils.h
abi_obj.h
ds3d::GuardRef::GuardRef
GuardRef(const ref &abiref)
Definition: obj.hpp:219
ds3d::GuardCB::GuardCB
GuardCB(nullptr_t)
Definition: obj.hpp:292
ds3d::GuardDataT::GuardDataT
GuardDataT(const abiRefT< Ty > &derived)
Definition: obj.hpp:343
ds3d::GuardDataT::GuardDataT
GuardDataT(nullptr_t)
Definition: obj.hpp:335
ds3d::abiRefCast::data
To * data() const final
Definition: obj.hpp:122
ds3d::abiRefT::data
virtual T * data() const =0
ds3d::_IsConstructible
typename std::is_constructible< T, Args... >::value _IsConstructible
Definition: obj.hpp:49
ds3d::SharedRefObj
Definition: obj.hpp:133
ds3d::_EnableIfConstructible
typename std::enable_if< std::is_constructible< T, Args... >::value, bool >::type _EnableIfConstructible
Definition: obj.hpp:53
ds3d
Definition: lidar_3d_datatype.h:35
ds3d::abiCallBackT
Definition: abi_obj.h:71
ds3d::abiRefCast::abiRefCast
abiRefCast(const abiRefT< From > &from)
Definition: obj.hpp:100
ds3d::GuardRef::GuardRef
GuardRef(ref *abiref, bool takeowner)
Definition: obj.hpp:225
ds3d::_EnableIfBaseOf
std::enable_if_t< std::is_base_of< Base, Derived >::value, bool > _EnableIfBaseOf
Definition: obj.hpp:61
ds3d::GuardCB
Definition: obj.hpp:285