NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/nostd/shared_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/shared_ptr.h"
9 # define OPENTELEMETRY_HAVE_STD_SHARED_PTR
10 # endif
11 #endif
12 
13 #if !defined(OPENTELEMETRY_HAVE_STD_SHARED_PTR)
14 # include <cstdlib>
15 # include <memory>
16 # include <utility>
17 
18 # include "opentelemetry/nostd/unique_ptr.h"
19 # include "opentelemetry/version.h"
20 
22 namespace nostd
23 {
27 template <class T>
29 {
30 public:
31  using element_type = T;
32  using pointer = element_type *;
33 
34 private:
35  static constexpr size_t kMaxSize = 32;
36  static constexpr size_t kAlignment = 8;
37 
38  struct alignas(kAlignment) PlacementBuffer
39  {
40  char data[kMaxSize]{};
41  };
42 
43  class shared_ptr_wrapper
44  {
45  public:
46  shared_ptr_wrapper() noexcept = default;
47 
48  shared_ptr_wrapper(std::shared_ptr<T> &&ptr) noexcept : ptr_{std::move(ptr)} {}
49 
50  virtual ~shared_ptr_wrapper() {}
51 
52  virtual void CopyTo(PlacementBuffer &buffer) const noexcept
53  {
54  new (buffer.data) shared_ptr_wrapper{*this};
55  }
56 
57  virtual void MoveTo(PlacementBuffer &buffer) noexcept
58  {
59  new (buffer.data) shared_ptr_wrapper{std::move(this->ptr_)};
60  }
61 
62  template <class U,
63  typename std::enable_if<std::is_convertible<pointer, U *>::value>::type * = nullptr>
64  void MoveTo(typename shared_ptr<U>::PlacementBuffer &buffer) noexcept
65  {
66  using other_shared_ptr_wrapper = typename shared_ptr<U>::shared_ptr_wrapper;
67  new (buffer.data) other_shared_ptr_wrapper{std::move(this->ptr_)};
68  }
69 
70  virtual pointer Get() const noexcept { return ptr_.get(); }
71 
72  virtual void Reset() noexcept { ptr_.reset(); }
73 
74  private:
75  std::shared_ptr<T> ptr_;
76  };
77 
78  static_assert(sizeof(shared_ptr_wrapper) <= kMaxSize, "Placement buffer is too small");
79  static_assert(alignof(shared_ptr_wrapper) <= kAlignment, "Placement buffer not properly aligned");
80 
81 public:
82  shared_ptr() noexcept { new (buffer_.data) shared_ptr_wrapper{}; }
83 
84  explicit shared_ptr(pointer ptr)
85  {
86  std::shared_ptr<T> ptr_(ptr);
87  new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
88  }
89 
90  shared_ptr(std::shared_ptr<T> ptr) noexcept
91  {
92  new (buffer_.data) shared_ptr_wrapper{std::move(ptr)};
93  }
94 
95  shared_ptr(shared_ptr &&other) noexcept { other.wrapper().MoveTo(buffer_); }
96 
97  template <class U,
98  typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
99  shared_ptr(shared_ptr<U> &&other) noexcept
100  {
101  other.wrapper().template MoveTo<T>(buffer_);
102  }
103 
104  shared_ptr(const shared_ptr &other) noexcept { other.wrapper().CopyTo(buffer_); }
105 
106  shared_ptr(unique_ptr<T> &&other) noexcept
107  {
108  std::shared_ptr<T> ptr_(other.release());
109  new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
110  }
111 
112  shared_ptr(std::unique_ptr<T> &&other) noexcept
113  {
114  std::shared_ptr<T> ptr_(other.release());
115  new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
116  }
117 
118  ~shared_ptr() { wrapper().~shared_ptr_wrapper(); }
119 
120  shared_ptr &operator=(shared_ptr &&other) noexcept
121  {
122  wrapper().~shared_ptr_wrapper();
123  other.wrapper().MoveTo(buffer_);
124  return *this;
125  }
126 
127  shared_ptr &operator=(std::nullptr_t) noexcept
128  {
129  wrapper().Reset();
130  return *this;
131  }
132 
133  shared_ptr &operator=(const shared_ptr &other) noexcept
134  {
135  wrapper().~shared_ptr_wrapper();
136  other.wrapper().CopyTo(buffer_);
137  return *this;
138  }
139 
140  element_type &operator*() const noexcept { return *wrapper().Get(); }
141 
142  pointer operator->() const noexcept { return wrapper().Get(); }
143 
144  operator bool() const noexcept { return wrapper().Get() != nullptr; }
145 
146  pointer get() const noexcept { return wrapper().Get(); }
147 
148  void swap(shared_ptr<T> &other) noexcept
149  {
150  shared_ptr<T> tmp{std::move(other)};
151 
152  wrapper().MoveTo(other.buffer_);
153  tmp.wrapper().MoveTo(buffer_);
154  }
155 
156  template <typename U>
157  friend class shared_ptr;
158 
159 private:
160  PlacementBuffer buffer_;
161 
162  shared_ptr_wrapper &wrapper() noexcept
163  {
164  return *reinterpret_cast<shared_ptr_wrapper *>(buffer_.data);
165  }
166 
167  const shared_ptr_wrapper &wrapper() const noexcept
168  {
169  return *reinterpret_cast<const shared_ptr_wrapper *>(buffer_.data);
170  }
171 };
172 
173 template <class T1, class T2>
174 bool operator!=(const shared_ptr<T1> &lhs, const shared_ptr<T2> &rhs) noexcept
175 {
176  return lhs.get() != rhs.get();
177 }
178 
179 template <class T1, class T2>
180 bool operator==(const shared_ptr<T1> &lhs, const shared_ptr<T2> &rhs) noexcept
181 {
182  return lhs.get() == rhs.get();
183 }
184 
185 template <class T>
186 inline bool operator==(const shared_ptr<T> &lhs, std::nullptr_t) noexcept
187 {
188  return lhs.get() == nullptr;
189 }
190 
191 template <class T>
192 inline bool operator==(std::nullptr_t, const shared_ptr<T> &rhs) noexcept
193 {
194  return nullptr == rhs.get();
195 }
196 
197 template <class T>
198 inline bool operator!=(const shared_ptr<T> &lhs, std::nullptr_t) noexcept
199 {
200  return lhs.get() != nullptr;
201 }
202 
203 template <class T>
204 inline bool operator!=(std::nullptr_t, const shared_ptr<T> &rhs) noexcept
205 {
206  return nullptr != rhs.get();
207 }
208 } // namespace nostd
210 #endif /* OPENTELEMETRY_HAVE_STD_SHARED_PTR */
nostd
Definition: includes/opentelemetry/nostd/function_ref.h:14
nostd::shared_ptr::operator*
element_type & operator*() const noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:140
nostd::shared_ptr::pointer
element_type * pointer
Definition: includes/opentelemetry/nostd/shared_ptr.h:32
nostd::shared_ptr::swap
void swap(shared_ptr< T > &other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:148
nostd::unique_ptr
Provide a simplified port of std::unique_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/unique_ptr.h:45
nostd::shared_ptr::shared_ptr
shared_ptr(std::shared_ptr< T > ptr) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:90
nostd::operator==
bool operator==(const shared_ptr< T1 > &lhs, const shared_ptr< T2 > &rhs) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:180
nostd::shared_ptr
Provide a type-erased version of std::shared_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/shared_ptr.h:28
nostd::operator!=
bool operator!=(const shared_ptr< T1 > &lhs, const shared_ptr< T2 > &rhs) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:174
ext::http::client::curl::HttpCurlGlobalInitializer
Definition: includes/opentelemetry/ext/http/client/curl/http_client_curl.h:42
nostd::shared_ptr::get
pointer get() const noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:146
nostd::shared_ptr::operator->
pointer operator->() const noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:142
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
nostd::shared_ptr::operator=
shared_ptr & operator=(const shared_ptr &other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:133
nostd::shared_ptr::shared_ptr
shared_ptr(pointer ptr)
Definition: includes/opentelemetry/nostd/shared_ptr.h:84
nostd::shared_ptr::shared_ptr
shared_ptr(const shared_ptr &other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:104
nostd::shared_ptr::shared_ptr
shared_ptr(shared_ptr &&other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:95
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
nostd::shared_ptr::shared_ptr
shared_ptr(std::unique_ptr< T > &&other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:112
nostd::shared_ptr::operator=
shared_ptr & operator=(shared_ptr &&other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:120
nostd::shared_ptr::shared_ptr
shared_ptr(unique_ptr< T > &&other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:106
nostd::shared_ptr::shared_ptr
shared_ptr() noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:82
std
Extensions to the C++ standard library.
Definition: includes/opentelemetry/nostd/string_view.h:212
ext::http::client::Method::Get
@ Get
nostd::shared_ptr::shared_ptr
shared_ptr(shared_ptr< U > &&other) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:99
nostd::shared_ptr::~shared_ptr
~shared_ptr()
Definition: includes/opentelemetry/nostd/shared_ptr.h:118
nostd::data
auto data(C &c) noexcept(noexcept(c.data())) -> decltype(c.data())
Back port of std::data.
Definition: includes/opentelemetry/nostd/utility.h:31
nostd::shared_ptr::operator=
shared_ptr & operator=(std::nullptr_t) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:127