NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/nostd/unique_ptr.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #if defined(OPENTELEMETRY_STL_VERSION)
7 # if OPENTELEMETRY_STL_VERSION >= 2011
8 # include "opentelemetry/std/unique_ptr.h"
9 # define OPENTELEMETRY_HAVE_STD_UNIQUE_PTR
10 # endif
11 #endif
12 
13 #if !defined(OPENTELEMETRY_HAVE_STD_UNIQUE_PTR)
14 # include <cstddef>
15 # include <memory>
16 # include <type_traits>
17 # include <utility>
18 
19 # include "opentelemetry/version.h"
20 
22 namespace nostd
23 {
24 namespace detail
25 {
26 template <class T>
27 struct unique_ptr_element_type
28 {
29  using type = T;
30 };
31 
32 template <class T>
33 struct unique_ptr_element_type<T[]>
34 {
35  using type = T;
36 };
37 } // namespace detail
38 
44 template <class T>
45 class unique_ptr
46 {
47 public:
49  using pointer = element_type *;
50 
51  unique_ptr() noexcept : ptr_{nullptr} {}
52 
53  unique_ptr(std::nullptr_t) noexcept : ptr_{nullptr} {}
54 
55  explicit unique_ptr(pointer ptr) noexcept : ptr_{ptr} {}
56 
57  unique_ptr(unique_ptr &&other) noexcept : ptr_{other.release()} {}
58 
59  template <class U,
60  typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
61  unique_ptr(unique_ptr<U> &&other) noexcept : ptr_{other.release()}
62  {}
63 
64  template <class U,
65  typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
66  unique_ptr(std::unique_ptr<U> &&other) noexcept : ptr_{other.release()}
67  {}
68 
69  ~unique_ptr() { reset(); }
70 
71  unique_ptr &operator=(unique_ptr &&other) noexcept
72  {
73  reset(other.release());
74  return *this;
75  }
76 
77  unique_ptr &operator=(std::nullptr_t) noexcept
78  {
79  reset();
80  return *this;
81  }
82 
83  template <class U,
84  typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
85  unique_ptr &operator=(unique_ptr<U> &&other) noexcept
86  {
87  reset(other.release());
88  return *this;
89  }
90 
91  template <class U,
92  typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
93  unique_ptr &operator=(std::unique_ptr<U> &&other) noexcept
94  {
95  reset(other.release());
96  return *this;
97  }
98 
99  operator std::unique_ptr<T>() && noexcept { return std::unique_ptr<T>{release()}; }
100 
101  operator bool() const noexcept { return ptr_ != nullptr; }
102 
103  element_type &operator*() const noexcept { return *ptr_; }
104 
105  pointer operator->() const noexcept { return get(); }
106 
107  pointer get() const noexcept { return ptr_; }
108 
109  void reset(pointer ptr = nullptr) noexcept
110  {
111  if (ptr_ != nullptr)
112  {
113  this->delete_ptr();
114  }
115  ptr_ = ptr;
116  }
117 
118  pointer release() noexcept
119  {
120  auto result = ptr_;
121  ptr_ = nullptr;
122  return result;
123  }
124 
125  void swap(unique_ptr &other) noexcept { std::swap(ptr_, other.ptr_); }
126 
127 private:
128  pointer ptr_;
129 
130  void delete_ptr() noexcept
131  {
132  if (std::is_array<T>::value)
133  {
134  delete[] ptr_;
135  }
136  else
137  {
138  delete ptr_;
139  }
140  }
141 };
142 
143 template <class T1, class T2>
144 bool operator==(const unique_ptr<T1> &lhs, const unique_ptr<T2> &rhs) noexcept
145 {
146  return lhs.get() == rhs.get();
147 }
148 
149 template <class T>
150 bool operator==(const unique_ptr<T> &lhs, std::nullptr_t) noexcept
151 {
152  return lhs.get() == nullptr;
153 }
154 
155 template <class T>
156 bool operator==(std::nullptr_t, const unique_ptr<T> &rhs) noexcept
157 {
158  return nullptr == rhs.get();
159 }
160 
161 template <class T1, class T2>
162 bool operator!=(const unique_ptr<T1> &lhs, const unique_ptr<T2> &rhs) noexcept
163 {
164  return lhs.get() != rhs.get();
165 }
166 
167 template <class T>
168 bool operator!=(const unique_ptr<T> &lhs, std::nullptr_t) noexcept
169 {
170  return lhs.get() != nullptr;
171 }
172 
173 template <class T>
174 bool operator!=(std::nullptr_t, const unique_ptr<T> &rhs) noexcept
175 {
176  return nullptr != rhs.get();
177 }
178 } // namespace nostd
180 #endif /* OPENTELEMETRY_HAVE_STD_UNIQUE_PTR */
nostd::unique_ptr::unique_ptr
unique_ptr() noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:51
nostd
Definition: includes/opentelemetry/nostd/function_ref.h:14
nostd::unique_ptr::reset
void reset(pointer ptr=nullptr) noexcept
Definition: includes/opentelemetry/nostd/unique_ptr.h:109
nostd::unique_ptr< const char[]>::pointer
element_type * pointer
Definition: includes/opentelemetry/nostd/unique_ptr.h:49
nostd::detail::unique_ptr_element_type
Definition: includes/opentelemetry/nostd/unique_ptr.h:27
nostd::unique_ptr
Provide a simplified port of std::unique_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/unique_ptr.h:45
nostd::operator==
bool operator==(const shared_ptr< T1 > &lhs, const shared_ptr< T2 > &rhs) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:180
nostd::unique_ptr::unique_ptr
unique_ptr(unique_ptr< U > &&other) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:61
absl::swap_internal::swap
void swap()
nostd::unique_ptr::unique_ptr
unique_ptr(std::nullptr_t) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:53
nostd::unique_ptr::release
pointer release() noexcept
Definition: includes/opentelemetry/nostd/unique_ptr.h:118
nostd::operator!=
bool operator!=(const shared_ptr< T1 > &lhs, const shared_ptr< T2 > &rhs) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:174
nostd::unique_ptr::get
pointer get() const noexcept
Definition: includes/opentelemetry/nostd/unique_ptr.h:107
nostd::detail::unique_ptr_element_type< T[]>::type
T type
Definition: includes/opentelemetry/nostd/unique_ptr.h:35
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
nostd::unique_ptr::swap
void swap(unique_ptr &other) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:125
nostd::unique_ptr::unique_ptr
unique_ptr(unique_ptr &&other) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:57
nostd::unique_ptr::~unique_ptr
~unique_ptr()
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:69
nostd::unique_ptr::operator=
unique_ptr & operator=(unique_ptr< U > &&other) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:85
nostd::unique_ptr::operator*
element_type & operator*() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:103
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
nostd::unique_ptr::operator=
unique_ptr & operator=(std::nullptr_t) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:77
nostd::unique_ptr::element_type
typename detail::unique_ptr_element_type< T >::type element_type
Definition: includes/opentelemetry/nostd/unique_ptr.h:48
nostd::unique_ptr::unique_ptr
unique_ptr(std::unique_ptr< U > &&other) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:66
nostd::unique_ptr::unique_ptr
unique_ptr(pointer ptr) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:55
nostd::unique_ptr::operator=
unique_ptr & operator=(unique_ptr &&other) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:71
nostd::unique_ptr::operator->
pointer operator->() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:105
nostd::detail::unique_ptr_element_type::type
T type
Definition: includes/opentelemetry/nostd/unique_ptr.h:29
nostd::unique_ptr::operator=
unique_ptr & operator=(std::unique_ptr< U > &&other) noexcept
Definition: 9.2/includes/opentelemetry/nostd/unique_ptr.h:93