File: shared_ptr.h

package info (click to toggle)
opentelemetry-cpp 1.23.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,368 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 38; python: 31
file content (210 lines) | stat: -rw-r--r-- 5,288 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#if defined(OPENTELEMETRY_STL_VERSION)
#  if OPENTELEMETRY_STL_VERSION >= 2011
#    include "opentelemetry/std/shared_ptr.h"
#    define OPENTELEMETRY_HAVE_STD_SHARED_PTR
#  endif
#endif

#if !defined(OPENTELEMETRY_HAVE_STD_SHARED_PTR)
#  include <cstdlib>
#  include <memory>
#  include <utility>

#  include "opentelemetry/nostd/unique_ptr.h"
#  include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace nostd
{
/**
 * Provide a type-erased version of std::shared_ptr that has ABI stability.
 */
template <class T>
class shared_ptr
{
public:
  using element_type = T;
  using pointer      = element_type *;

private:
  static constexpr size_t kMaxSize   = 32;
  static constexpr size_t kAlignment = 8;

  struct alignas(kAlignment) PlacementBuffer
  {
    char data[kMaxSize]{};
  };

  class shared_ptr_wrapper
  {
  public:
    shared_ptr_wrapper() noexcept = default;

    shared_ptr_wrapper(std::shared_ptr<T> &&ptr) noexcept : ptr_{std::move(ptr)} {}

    virtual ~shared_ptr_wrapper() {}

    virtual void CopyTo(PlacementBuffer &buffer) const noexcept
    {
      new (buffer.data) shared_ptr_wrapper{*this};
    }

    virtual void MoveTo(PlacementBuffer &buffer) noexcept
    {
      new (buffer.data) shared_ptr_wrapper{std::move(this->ptr_)};
    }

    template <class U,
              typename std::enable_if<std::is_convertible<pointer, U *>::value>::type * = nullptr>
    void MoveTo(typename shared_ptr<U>::PlacementBuffer &buffer) noexcept
    {
      using other_shared_ptr_wrapper = typename shared_ptr<U>::shared_ptr_wrapper;
      new (buffer.data) other_shared_ptr_wrapper{std::move(this->ptr_)};
    }

    virtual pointer Get() const noexcept { return ptr_.get(); }

    virtual void Reset() noexcept { ptr_.reset(); }

  private:
    std::shared_ptr<T> ptr_;
  };

  static_assert(sizeof(shared_ptr_wrapper) <= kMaxSize, "Placement buffer is too small");
  static_assert(alignof(shared_ptr_wrapper) <= kAlignment, "Placement buffer not properly aligned");

public:
  shared_ptr() noexcept { new (buffer_.data) shared_ptr_wrapper{}; }

  explicit shared_ptr(pointer ptr)
  {
    std::shared_ptr<T> ptr_(ptr);
    new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
  }

  shared_ptr(std::shared_ptr<T> ptr) noexcept
  {
    new (buffer_.data) shared_ptr_wrapper{std::move(ptr)};
  }

  shared_ptr(shared_ptr &&other) noexcept { other.wrapper().MoveTo(buffer_); }

  template <class U,
            typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
  shared_ptr(shared_ptr<U> &&other) noexcept
  {
    other.wrapper().template MoveTo<T>(buffer_);
  }

  shared_ptr(const shared_ptr &other) noexcept { other.wrapper().CopyTo(buffer_); }

  shared_ptr(unique_ptr<T> &&other) noexcept
  {
    std::shared_ptr<T> ptr_(other.release());
    new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
  }

  shared_ptr(std::unique_ptr<T> &&other) noexcept
  {
    std::shared_ptr<T> ptr_(other.release());
    new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
  }

  ~shared_ptr() { wrapper().~shared_ptr_wrapper(); }

  shared_ptr &operator=(shared_ptr &&other) noexcept
  {
    wrapper().~shared_ptr_wrapper();
    other.wrapper().MoveTo(buffer_);
    return *this;
  }

  shared_ptr &operator=(std::nullptr_t) noexcept
  {
    wrapper().Reset();
    return *this;
  }

  shared_ptr &operator=(const shared_ptr &other) noexcept
  {
    wrapper().~shared_ptr_wrapper();
    other.wrapper().CopyTo(buffer_);
    return *this;
  }

  element_type &operator*() const noexcept { return *wrapper().Get(); }

  pointer operator->() const noexcept { return wrapper().Get(); }

  operator bool() const noexcept { return wrapper().Get() != nullptr; }

  pointer get() const noexcept { return wrapper().Get(); }

  void swap(shared_ptr<T> &other) noexcept
  {
    shared_ptr<T> tmp{std::move(other)};

    wrapper().MoveTo(other.buffer_);
    tmp.wrapper().MoveTo(buffer_);
  }

  template <typename U>
  friend class shared_ptr;

private:
  PlacementBuffer buffer_;

  shared_ptr_wrapper &wrapper() noexcept
  {
    return *reinterpret_cast<shared_ptr_wrapper *>(buffer_.data);
  }

  const shared_ptr_wrapper &wrapper() const noexcept
  {
    return *reinterpret_cast<const shared_ptr_wrapper *>(buffer_.data);
  }
};

template <class T1, class T2>
bool operator!=(const shared_ptr<T1> &lhs, const shared_ptr<T2> &rhs) noexcept
{
  return lhs.get() != rhs.get();
}

template <class T1, class T2>
bool operator==(const shared_ptr<T1> &lhs, const shared_ptr<T2> &rhs) noexcept
{
  return lhs.get() == rhs.get();
}

template <class T>
inline bool operator==(const shared_ptr<T> &lhs, std::nullptr_t) noexcept
{
  return lhs.get() == nullptr;
}

template <class T>
inline bool operator==(std::nullptr_t, const shared_ptr<T> &rhs) noexcept
{
  return nullptr == rhs.get();
}

template <class T>
inline bool operator!=(const shared_ptr<T> &lhs, std::nullptr_t) noexcept
{
  return lhs.get() != nullptr;
}

template <class T>
inline bool operator!=(std::nullptr_t, const shared_ptr<T> &rhs) noexcept
{
  return nullptr != rhs.get();
}
}  // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif /* OPENTELEMETRY_HAVE_STD_SHARED_PTR */