NVIDIA DeepStream SDK API Reference

6.4 Release
Array.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
3  *
4  * NVIDIA CORPORATION and its licensors retain all intellectual property
5  * and proprietary rights in and to this software, related documentation
6  * and any modifications thereto. Any use, reproduction, disclosure or
7  * distribution of this software and related documentation without an express
8  * license agreement from NVIDIA CORPORATION is strictly prohibited.
9  */
10 
11 #ifndef CVCORE_ARRAY_H
12 #define CVCORE_ARRAY_H
13 
14 #include <cassert>
15 #include <new>
16 #include <utility>
17 
18 namespace cvcore {
19 
23 class ArrayBase
24 {
25 public:
33  ArrayBase(std::size_t capacity, std::size_t elemSize, void *dataPtr, bool isCPU);
34 
41  ArrayBase(std::size_t capacity, std::size_t elemSize, bool isCPU);
42 
46  ~ArrayBase();
47 
51  ArrayBase(const ArrayBase &) = delete;
52 
56  ArrayBase &operator=(const ArrayBase &) = delete;
57 
61  ArrayBase(ArrayBase &&);
62 
67 
73  void *getElement(int idx) const;
74 
79  std::size_t getSize() const;
80 
85  std::size_t getCapacity() const;
86 
91  std::size_t getElementSize() const;
92 
97  void setSize(std::size_t size);
98 
103  bool isCPU() const;
104 
109  bool isOwning() const;
110 
115  void *getData() const;
116 
117 private:
118  ArrayBase();
119 
120  void *m_data;
121  std::size_t m_size;
122  std::size_t m_capacity;
123  std::size_t m_elemSize;
124  bool m_isOwning;
125  bool m_isCPU;
126 };
127 
132 template<typename T>
133 class Array : public ArrayBase
134 {
135 public:
140  : ArrayBase{0, sizeof(T), nullptr, true}
141  {
142  }
143 
151  Array(std::size_t size, std::size_t capacity, void *dataPtr, bool isCPU = true)
152  : ArrayBase{capacity, sizeof(T), dataPtr, isCPU}
153  {
154  ArrayBase::setSize(size);
155  }
156 
162  Array(std::size_t capacity, bool isCPU = true)
163  : ArrayBase{capacity, sizeof(T), isCPU}
164  {
165  }
166 
171  {
172  // call resize here such that CPU-based destructor
173  // will call destructors of the objects stored
174  // in the array before deallocating the storage
175  setSize(0);
176  }
177 
181  Array(const Array &) = delete;
182 
186  Array &operator=(const Array &) = delete;
187 
192  : Array()
193  {
194  *this = std::move(t);
195  }
196 
201  {
202  static_cast<ArrayBase &>(*this) = std::move(t);
203  return *this;
204  }
205 
210  void setSize(std::size_t size)
211  {
212  const std::size_t oldSize = getSize();
213  ArrayBase::setSize(size);
214  if (isCPU())
215  {
216  // shrinking case
217  for (std::size_t i = size; i < oldSize; ++i)
218  {
219  reinterpret_cast<T *>(getElement(i))->~T();
220  }
221  // expanding case
222  for (std::size_t i = oldSize; i < size; ++i)
223  {
224  new (getElement(i)) T;
225  }
226  }
227  }
228 
234  const T &operator[](int idx) const
235  {
236  assert(idx >= 0 && idx < getSize());
237  return *reinterpret_cast<T *>(getElement(idx));
238  }
239 
245  T &operator[](int idx)
246  {
247  assert(idx >= 0 && idx < getSize());
248  return *reinterpret_cast<T *>(getElement(idx));
249  }
250 };
251 
257 template<typename T, std::size_t N>
258 class ArrayN : public ArrayBase
259 {
260 public:
265  : ArrayBase{N, sizeof(T), true}
266  {
267  setSize(N);
268  }
269 
276  ArrayN(std::size_t size, void *dataPtr, bool isCPU = true)
277  : ArrayBase{N, sizeof(T), dataPtr, isCPU}
278  {
279  ArrayBase::setSize(size);
280  }
281 
286  ArrayN(bool isCPU)
287  : ArrayBase{N, sizeof(T), isCPU}
288  {
289  setSize(N);
290  }
291 
296  {
297  // call resize here such that CPU-based destructor
298  // will call destructors of the objects stored
299  // in the array before deallocating the storage
300  setSize(0);
301  }
302 
306  ArrayN(const ArrayN &) = delete;
307 
311  ArrayN &operator=(const ArrayN &) = delete;
312 
317  : ArrayN()
318  {
319  *this = std::move(t);
320  }
321 
326  {
327  static_cast<ArrayBase &>(*this) = std::move(t);
328  return *this;
329  }
330 
335  void setSize(std::size_t size)
336  {
337  const std::size_t oldSize = getSize();
338  ArrayBase::setSize(size);
339  if (isCPU())
340  {
341  // shrinking case
342  for (std::size_t i = size; i < oldSize; ++i)
343  {
344  reinterpret_cast<T *>(getElement(i))->~T();
345  }
346  // expanding case
347  for (std::size_t i = oldSize; i < size; ++i)
348  {
349  new (getElement(i)) T;
350  }
351  }
352  }
353 
359  const T &operator[](int idx) const
360  {
361  assert(idx >= 0 && idx < getSize());
362  return *reinterpret_cast<T *>(getElement(idx));
363  }
364 
370  T &operator[](int idx)
371  {
372  assert(idx >= 0 && idx < getSize());
373  return *reinterpret_cast<T *>(getElement(idx));
374  }
375 };
376 
377 } // namespace cvcore
378 
379 #endif // CVCORE_ARRAY_H
cvcore::ArrayBase::getElementSize
std::size_t getElementSize() const
Get the size of each element.
cvcore::Array::operator=
Array & operator=(const Array &)=delete
Array is non-copyable.
cvcore::ArrayBase
Base implementation of Array.
Definition: Array.h:23
cvcore::ArrayBase::isCPU
bool isCPU() const
Get the flag whether the array is CPU or GPU array.
cvcore::ArrayN::ArrayN
ArrayN(bool isCPU)
Constructor of a memory-owning ArrayN.
Definition: Array.h:286
cvcore::Array::~Array
~Array()
Destructor of the Array.
Definition: Array.h:170
cvcore::ArrayN::~ArrayN
~ArrayN()
Destructor of the ArrayN.
Definition: Array.h:295
cvcore::ArrayBase::isOwning
bool isOwning() const
Get the flag whether the array is owning memory space.
cvcore
Definition: PnP.h:20
cvcore::ArrayBase::getElement
void * getElement(int idx) const
Get the pointer to specified index.
cvcore::ArrayN::ArrayN
ArrayN(std::size_t size, void *dataPtr, bool isCPU=true)
Constructor of a non-owning ArrayN.
Definition: Array.h:276
cvcore::ArrayN::operator[]
T & operator[](int idx)
ArrayN index operator.
Definition: Array.h:370
cvcore::ArrayN::ArrayN
ArrayN()
Default constructor of ArrayN (create an owning Tensor with capacity N).
Definition: Array.h:264
cvcore::Array::operator[]
T & operator[](int idx)
Array index operator.
Definition: Array.h:245
cvcore::ArrayBase::operator=
ArrayBase & operator=(const ArrayBase &)=delete
ArrayBase is non-copyable.
cvcore::ArrayN::operator[]
const T & operator[](int idx) const
Const ArrayN index operator.
Definition: Array.h:359
cvcore::Array::Array
Array(std::size_t size, std::size_t capacity, void *dataPtr, bool isCPU=true)
Constructor of a non-owning array.
Definition: Array.h:151
cvcore::ArrayBase::setSize
void setSize(std::size_t size)
Set the size of the array.
cvcore::Array
Implementation of Array class.
Definition: Array.h:133
cvcore::Array::operator=
Array & operator=(Array &&t)
Move assignment operator of Array.
Definition: Array.h:200
cvcore::ArrayBase::getSize
std::size_t getSize() const
Get the size of the array.
cvcore::ArrayBase::getData
void * getData() const
Get the raw pointer to the array data.
cvcore::Array::Array
Array()
Default constructor of an array.
Definition: Array.h:139
cvcore::ArrayN::ArrayN
ArrayN(ArrayN &&t)
Move constructor of ArrayN.
Definition: Array.h:316
cvcore::ArrayBase::~ArrayBase
~ArrayBase()
Destructor of ArrayBase.
cvcore::ArrayN::operator=
ArrayN & operator=(ArrayN &&t)
Move assignment operator of ArrayN.
Definition: Array.h:325
cvcore::Array::Array
Array(Array &&t)
Move constructor of Array.
Definition: Array.h:191
cvcore::ArrayN::setSize
void setSize(std::size_t size)
Set size of the ArrayN.
Definition: Array.h:335
cvcore::ArrayN
Implementation of ArrayN class.
Definition: Array.h:258
cvcore::Array::setSize
void setSize(std::size_t size)
Set size of the Array.
Definition: Array.h:210
cvcore::ArrayN::operator=
ArrayN & operator=(const ArrayN &)=delete
ArrayN is non-copyable.
cvcore::Array::Array
Array(std::size_t capacity, bool isCPU=true)
Constructor of a memory-owning array.
Definition: Array.h:162
cvcore::Array::operator[]
const T & operator[](int idx) const
Const array index operator.
Definition: Array.h:234
cvcore::ArrayBase::getCapacity
std::size_t getCapacity() const
Get the capacity of the array.