Sleipnir C++ API
Loading...
Searching...
No Matches
intrusive_shared_ptr.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <concepts>
6#include <cstddef>
7#include <memory>
8#include <utility>
9
10namespace slp {
11
28template <typename T>
30 public:
31 template <typename>
32 friend class IntrusiveSharedPtr;
33
37 constexpr IntrusiveSharedPtr() noexcept = default;
38
42 constexpr IntrusiveSharedPtr(std::nullptr_t) noexcept {} // NOLINT
43
50 explicit constexpr IntrusiveSharedPtr(T* ptr) noexcept : m_ptr{ptr} {
51 if (m_ptr != nullptr) {
52 inc_ref_count(m_ptr);
53 }
54 }
55
56 constexpr ~IntrusiveSharedPtr() {
57 if (m_ptr != nullptr) {
58 dec_ref_count(m_ptr);
59 }
60 }
61
67 constexpr IntrusiveSharedPtr(const IntrusiveSharedPtr<T>& rhs) noexcept
68 : m_ptr{rhs.m_ptr} {
69 if (m_ptr != nullptr) {
70 inc_ref_count(m_ptr);
71 }
72 }
73
79 template <typename U>
80 requires(!std::same_as<T, U> && std::convertible_to<U*, T*>)
81 constexpr IntrusiveSharedPtr( // NOLINT
82 const IntrusiveSharedPtr<U>& rhs) noexcept
83 : m_ptr{rhs.m_ptr} {
84 if (m_ptr != nullptr) {
85 inc_ref_count(m_ptr);
86 }
87 }
88
95 constexpr IntrusiveSharedPtr<T>& operator=( // NOLINT
96 const IntrusiveSharedPtr<T>& rhs) noexcept {
97 if (m_ptr == rhs.m_ptr) {
98 return *this;
99 }
100
101 if (m_ptr != nullptr) {
102 dec_ref_count(m_ptr);
103 }
104
105 m_ptr = rhs.m_ptr;
106
107 if (m_ptr != nullptr) {
108 inc_ref_count(m_ptr);
109 }
110
111 return *this;
112 }
113
120 template <typename U>
121 requires(!std::same_as<T, U> && std::convertible_to<U*, T*>)
122 constexpr IntrusiveSharedPtr<T>& operator=( // NOLINT
123 const IntrusiveSharedPtr<U>& rhs) noexcept {
124 if (m_ptr == rhs.m_ptr) {
125 return *this;
126 }
127
128 if (m_ptr != nullptr) {
129 dec_ref_count(m_ptr);
130 }
131
132 m_ptr = rhs.m_ptr;
133
134 if (m_ptr != nullptr) {
135 inc_ref_count(m_ptr);
136 }
137
138 return *this;
139 }
140
147 : m_ptr{std::exchange(rhs.m_ptr, nullptr)} {}
148
154 template <typename U>
155 requires(!std::same_as<T, U> && std::convertible_to<U*, T*>)
156 constexpr IntrusiveSharedPtr( // NOLINT
157 IntrusiveSharedPtr<U>&& rhs) noexcept
158 : m_ptr{std::exchange(rhs.m_ptr, nullptr)} {}
159
167 IntrusiveSharedPtr<T>&& rhs) noexcept {
168 if (m_ptr == rhs.m_ptr) {
169 return *this;
170 }
171
172 std::swap(m_ptr, rhs.m_ptr);
173
174 return *this;
175 }
176
183 template <typename U>
184 requires(!std::same_as<T, U> && std::convertible_to<U*, T*>)
185 constexpr IntrusiveSharedPtr<T>& operator=(
186 IntrusiveSharedPtr<U>&& rhs) noexcept {
187 if (m_ptr == rhs.m_ptr) {
188 return *this;
189 }
190
191 std::swap(m_ptr, rhs.m_ptr);
192
193 return *this;
194 }
195
201 constexpr T* get() const noexcept { return m_ptr; }
202
208 constexpr T& operator*() const noexcept { return *m_ptr; }
209
215 constexpr T* operator->() const noexcept { return m_ptr; }
216
222 explicit constexpr operator bool() const noexcept { return m_ptr != nullptr; }
223
231 friend constexpr bool operator==(const IntrusiveSharedPtr<T>& lhs,
232 const IntrusiveSharedPtr<T>& rhs) noexcept {
233 return lhs.m_ptr == rhs.m_ptr;
234 }
235
243 friend constexpr bool operator!=(const IntrusiveSharedPtr<T>& lhs,
244 const IntrusiveSharedPtr<T>& rhs) noexcept {
245 return lhs.m_ptr != rhs.m_ptr;
246 }
247
253 friend constexpr bool operator==(const IntrusiveSharedPtr<T>& lhs,
254 std::nullptr_t) noexcept {
255 return lhs.m_ptr == nullptr;
256 }
257
263 friend constexpr bool operator==(std::nullptr_t,
264 const IntrusiveSharedPtr<T>& rhs) noexcept {
265 return nullptr == rhs.m_ptr;
266 }
267
274 friend constexpr bool operator!=(const IntrusiveSharedPtr<T>& lhs,
275 std::nullptr_t) noexcept {
276 return lhs.m_ptr != nullptr;
277 }
278
285 friend constexpr bool operator!=(std::nullptr_t,
286 const IntrusiveSharedPtr<T>& rhs) noexcept {
287 return nullptr != rhs.m_ptr;
288 }
289
290 private:
291 T* m_ptr = nullptr;
292};
293
302template <typename T, typename... Args>
303IntrusiveSharedPtr<T> make_intrusive_shared(Args&&... args) {
304 return IntrusiveSharedPtr<T>{new T(std::forward<Args>(args)...)};
305}
306
318template <typename T, typename Alloc, typename... Args>
319IntrusiveSharedPtr<T> allocate_intrusive_shared(Alloc alloc, Args&&... args) {
320 auto ptr = std::allocator_traits<Alloc>::allocate(alloc, sizeof(T));
321 std::allocator_traits<Alloc>::construct(alloc, ptr,
322 std::forward<Args>(args)...);
323 return IntrusiveSharedPtr<T>{ptr};
324}
325
326} // namespace slp
Definition intrusive_shared_ptr.hpp:29
constexpr IntrusiveSharedPtr(IntrusiveSharedPtr< U > &&rhs) noexcept
Definition intrusive_shared_ptr.hpp:156
constexpr IntrusiveSharedPtr< T > & operator=(IntrusiveSharedPtr< T > &&rhs) noexcept
Definition intrusive_shared_ptr.hpp:166
friend constexpr bool operator==(std::nullptr_t, const IntrusiveSharedPtr< T > &rhs) noexcept
Definition intrusive_shared_ptr.hpp:263
constexpr T * get() const noexcept
Definition intrusive_shared_ptr.hpp:201
constexpr IntrusiveSharedPtr(const IntrusiveSharedPtr< T > &rhs) noexcept
Definition intrusive_shared_ptr.hpp:67
constexpr T * operator->() const noexcept
Definition intrusive_shared_ptr.hpp:215
friend constexpr bool operator!=(const IntrusiveSharedPtr< T > &lhs, std::nullptr_t) noexcept
Definition intrusive_shared_ptr.hpp:274
friend constexpr bool operator==(const IntrusiveSharedPtr< T > &lhs, std::nullptr_t) noexcept
Definition intrusive_shared_ptr.hpp:253
friend constexpr bool operator==(const IntrusiveSharedPtr< T > &lhs, const IntrusiveSharedPtr< T > &rhs) noexcept
Definition intrusive_shared_ptr.hpp:231
constexpr IntrusiveSharedPtr() noexcept=default
constexpr IntrusiveSharedPtr(T *ptr) noexcept
Definition intrusive_shared_ptr.hpp:50
friend constexpr bool operator!=(std::nullptr_t, const IntrusiveSharedPtr< T > &rhs) noexcept
Definition intrusive_shared_ptr.hpp:285
constexpr IntrusiveSharedPtr(IntrusiveSharedPtr< T > &&rhs) noexcept
Definition intrusive_shared_ptr.hpp:146
constexpr T & operator*() const noexcept
Definition intrusive_shared_ptr.hpp:208
constexpr IntrusiveSharedPtr(const IntrusiveSharedPtr< U > &rhs) noexcept
Definition intrusive_shared_ptr.hpp:81
friend constexpr bool operator!=(const IntrusiveSharedPtr< T > &lhs, const IntrusiveSharedPtr< T > &rhs) noexcept
Definition intrusive_shared_ptr.hpp:243
constexpr IntrusiveSharedPtr< T > & operator=(const IntrusiveSharedPtr< T > &rhs) noexcept
Definition intrusive_shared_ptr.hpp:95