TrinityCore
UniqueTrackablePtr.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_UNIQUE_TRACKABLE_PTR_H
19#define TRINITYCORE_UNIQUE_TRACKABLE_PTR_H
20
21#include <memory>
22
23namespace Trinity
24{
25template <typename T>
26class unique_trackable_ptr;
27
28template <typename T>
29class unique_weak_ptr;
30
31template <typename T>
32class unique_strong_ref_ptr;
33
39template <typename T>
41{
42public:
43 using element_type = T;
44 using pointer = T*;
45
47
49 : _ptr(ptr) { }
50
51 template <typename Deleter, std::enable_if_t<std::conjunction_v<std::is_move_constructible<Deleter>, std::is_invocable<Deleter&, T*&>>, int> = 0>
52 explicit unique_trackable_ptr(pointer ptr, Deleter deleter)
53 : _ptr(ptr, std::move(deleter)) { }
54
56
58 : _ptr(std::move(other._ptr)) { }
59
60 template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
62 : _ptr(std::move(other)._ptr) { }
63
65
67 {
68 _ptr = std::move(other._ptr);
69 return *this;
70 }
71
72 template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
74 {
75 _ptr = std::move(other)._ptr;
76 return *this;
77 }
78
80
82 {
83 reset();
84 return *this;
85 }
86
87 void swap(unique_trackable_ptr& other) noexcept
88 {
89 using std::swap;
90 swap(_ptr, other._ptr);
91 }
92
94 {
95 return *_ptr;
96 }
97
99 {
100 return _ptr.operator->();
101 }
102
103 pointer get() const
104 {
105 return _ptr.get();
106 }
107
108 explicit operator bool() const
109 {
110 return static_cast<bool>(_ptr);
111 }
112
113 void reset()
114 {
115 _ptr.reset();
116 }
117
118 void reset(pointer ptr)
119 {
120 _ptr.reset(ptr);
121 }
122
123 template <class Deleter, std::enable_if_t<std::conjunction_v<std::is_move_constructible<Deleter>, std::is_invocable<Deleter&, T*&>>, int> = 0>
124 void reset(pointer ptr, Deleter deleter)
125 {
126 _ptr.reset(ptr, std::move(deleter));
127 }
128
129private:
130 template <typename T0>
132
133 template <typename T0>
134 friend class unique_weak_ptr;
135
136 template <typename T0, typename... Args>
137 friend std::enable_if_t<!std::is_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable(Args&&... args);
138
139 template <typename T0>
140 friend std::enable_if_t<std::is_unbounded_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable(std::size_t N);
141
142 template <typename T0>
143 friend std::enable_if_t<std::is_unbounded_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable(std::size_t N, std::remove_extent_t<T0> const& val);
144
145 template <typename T0>
146 friend std::enable_if_t<std::is_bounded_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable();
147
148 template <typename T0>
149 friend std::enable_if_t<std::is_bounded_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable(std::remove_extent_t<T0> const& val);
150
151 std::shared_ptr<element_type> _ptr;
152};
153
158template <typename T>
160{
161public:
162 using element_type = T;
163 using pointer = T*;
164
165 unique_weak_ptr() = default;
166
168 : _ptr(trackable._ptr) { }
169
170 unique_weak_ptr(unique_weak_ptr const& other) = default;
171
172 template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
174 : _ptr(other._ptr) { }
175
176 unique_weak_ptr(unique_weak_ptr&& other) noexcept = default;
177
178 template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
180 : _ptr(std::move(other)._ptr) { }
181
183 {
184 _ptr = trackable._ptr;
185 return *this;
186 }
187
188 unique_weak_ptr& operator=(unique_weak_ptr const& other) = default;
189
190 template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
192 {
193 _ptr = std::move(other)._ptr;
194 return *this;
195 }
196
197 unique_weak_ptr& operator=(unique_weak_ptr&& other) noexcept = default;
198
199 ~unique_weak_ptr() = default;
200
201 void swap(unique_weak_ptr& other) noexcept
202 {
203 using std::swap;
204 swap(_ptr, other._ptr);
205 }
206
207 bool expired() const
208 {
209 return _ptr.expired();
210 }
211
213 {
215 }
216
217private:
218 template <typename T0>
219 friend class unique_weak_ptr;
220
221 template <typename T0>
223
224 template <class To, class From>
226
227 template <class To, class From>
229
230 template <class To, class From>
232
233 template <class To, class From>
235
236 std::weak_ptr<element_type> _ptr;
237};
238
245template <typename T>
247{
248public:
249 using element_type = T;
250 using pointer = T*;
251
256
258
260 {
261 return *_ptr;
262 }
263
265 {
266 return _ptr.operator->();
267 }
268
269 pointer get() const
270 {
271 return _ptr.get();
272 }
273
274 explicit operator bool() const
275 {
276 return static_cast<bool>(_ptr);
277 }
278
279 operator unique_weak_ptr<T>() const
280 {
282 weak._ptr = _ptr;
283 return weak;
284 }
285
286private:
287 template <typename T0>
288 friend class unique_weak_ptr;
289
290 template <class To, class From>
292
293 template <class To, class From>
295
296 template <class To, class From>
298
299 template <class To, class From>
301
302 template <class To, class From>
304
305 template <class To, class From>
307
308 template <class To, class From>
310
311 template <class To, class From>
313
314 unique_strong_ref_ptr(std::shared_ptr<element_type> ptr) : _ptr(std::move(ptr)) { }
315
316 std::shared_ptr<element_type> _ptr;
317};
318
319// unique_trackable_ptr funcions
320
321template <typename T1, typename T2>
323{
324 return left.get() == right.get();
325}
326
327template <typename T1, typename T2>
328std::strong_ordering operator<=>(unique_trackable_ptr<T1> const& left, unique_trackable_ptr<T2> const& right)
329{
330 return left.get() <=> right.get();
331}
332
333template <typename T1>
334bool operator==(unique_trackable_ptr<T1> const& left, std::nullptr_t)
335{
336 return left.get() == nullptr;
337}
338
339template <typename T1>
340std::strong_ordering operator<=>(unique_trackable_ptr<T1> const& left, std::nullptr_t)
341{
342 return left.get() <=> nullptr;
343}
344
345template <typename T, typename... Args>
346std::enable_if_t<!std::is_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable(Args&&... args)
347{
349 ptr._ptr = std::make_shared<T>(std::forward<Args>(args)...);
350 return ptr;
351}
352
353template <typename T>
354std::enable_if_t<std::is_unbounded_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable(std::size_t N)
355{
357 ptr._ptr = std::make_shared<T>(N);
358 return ptr;
359}
360
361template <typename T>
362std::enable_if_t<std::is_unbounded_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable(std::size_t N, std::remove_extent_t<T> const& val)
363{
365 ptr._ptr = std::make_shared<T>(N, val);
366 return ptr;
367}
368
369template <typename T>
370std::enable_if_t<std::is_bounded_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable()
371{
373 ptr._ptr = std::make_shared<T>();
374 return ptr;
375}
376
377template <typename T>
378std::enable_if_t<std::is_bounded_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable(std::remove_extent_t<T> const& val)
379{
381 ptr._ptr = std::make_shared<T>(val);
382 return ptr;
383}
384
385// unique_weak_ptr funcions
386
387template <class To, class From>
389{
391 to._ptr = std::static_pointer_cast<To>(other._ptr.lock());
392 return to;
393}
394
395template <class To, class From>
397{
399 to._ptr = std::const_pointer_cast<To>(other._ptr.lock());
400 return to;
401}
402
403template <class To, class From>
405{
407 to._ptr = std::reinterpret_pointer_cast<To>(other._ptr.lock());
408 return to;
409}
410
411template <class To, class From>
413{
415 to._ptr = std::dynamic_pointer_cast<To>(other._ptr.lock());
416 return to;
417}
418
419// unique_strong_ref_ptr funcions
420
421template <typename T1, typename T2>
423{
424 return left.get() == right.get();
425}
426
427template <typename T1, typename T2>
428std::strong_ordering operator<=>(unique_strong_ref_ptr<T1> const& left, unique_strong_ref_ptr<T2> const& right)
429{
430 return left.get() <=> right.get();
431}
432
433template <typename T1>
434bool operator==(unique_strong_ref_ptr<T1> const& left, std::nullptr_t)
435{
436 return left.get() == nullptr;
437}
438
439template <typename T1>
440std::strong_ordering operator<=>(unique_strong_ref_ptr<T1> const& left, std::nullptr_t)
441{
442 return left.get() <=> nullptr;
443}
444
445template <class To, class From>
447{
448 return unique_strong_ref_ptr<To>(std::static_pointer_cast<To>(other._ptr));
449}
450
451template <class To, class From>
453{
454 return unique_strong_ref_ptr<To>(std::static_pointer_cast<To>(std::move(other._ptr)));
455}
456
457template <class To, class From>
459{
460 return unique_strong_ref_ptr<To>(std::const_pointer_cast<To>(other._ptr));
461}
462
463template <class To, class From>
465{
466 return unique_strong_ref_ptr<To>(std::const_pointer_cast<To>(std::move(other._ptr)));
467}
468
469template <class To, class From>
471{
472 return unique_strong_ref_ptr<To>(std::reinterpret_pointer_cast<To>(other._ptr));
473}
474
475template <class To, class From>
477{
478 return unique_strong_ref_ptr<To>(std::reinterpret_pointer_cast<To>(std::move(other._ptr)));
479}
480
481template <class To, class From>
483{
484 return unique_strong_ref_ptr<To>(std::dynamic_pointer_cast<To>(other._ptr));
485}
486
487template <class To, class From>
489{
490 return unique_strong_ref_ptr<To>(std::dynamic_pointer_cast<To>(std::move(other._ptr)));
491}
492}
493
494#endif // TRINITYCORE_UNIQUE_TRACKABLE_PTR_H
Result of unique_weak_ptr::lock() function, this class holds a temporary strong reference to held obj...
unique_strong_ref_ptr & operator=(unique_strong_ref_ptr &&)=delete
unique_strong_ref_ptr(std::shared_ptr< element_type > ptr)
std::shared_ptr< element_type > _ptr
unique_strong_ref_ptr(unique_strong_ref_ptr const &)=delete
friend unique_strong_ref_ptr< To > dynamic_pointer_cast(unique_strong_ref_ptr< From > const &other)
friend unique_strong_ref_ptr< To > static_pointer_cast(unique_strong_ref_ptr< From > const &other)
unique_strong_ref_ptr & operator=(unique_strong_ref_ptr const &)=delete
friend unique_strong_ref_ptr< To > reinterpret_pointer_cast(unique_strong_ref_ptr< From > const &other)
unique_strong_ref_ptr(unique_strong_ref_ptr &&)=delete
friend unique_strong_ref_ptr< To > const_pointer_cast(unique_strong_ref_ptr< From > const &other)
element_type & operator*() const
Specialized variant of std::shared_ptr that enforces unique ownership and/or std::unique_ptr with std...
unique_trackable_ptr(unique_trackable_ptr const &)=delete
friend std::enable_if_t< std::is_bounded_array_v< T0 >, unique_trackable_ptr< T0 > > make_unique_trackable(std::remove_extent_t< T0 > const &val)
unique_trackable_ptr & operator=(unique_trackable_ptr const &)=delete
unique_trackable_ptr(unique_trackable_ptr< T2 > &&other) noexcept
std::shared_ptr< element_type > _ptr
void reset(pointer ptr, Deleter deleter)
friend std::enable_if_t< std::is_unbounded_array_v< T0 >, unique_trackable_ptr< T0 > > make_unique_trackable(std::size_t N, std::remove_extent_t< T0 > const &val)
friend std::enable_if_t<!std::is_array_v< T0 >, unique_trackable_ptr< T0 > > make_unique_trackable(Args &&... args)
unique_trackable_ptr & operator=(unique_trackable_ptr &&other) noexcept
element_type & operator*() const
friend std::enable_if_t< std::is_bounded_array_v< T0 >, unique_trackable_ptr< T0 > > make_unique_trackable()
unique_trackable_ptr(pointer ptr, Deleter deleter)
unique_trackable_ptr & operator=(std::nullptr_t)
unique_trackable_ptr(unique_trackable_ptr &&other) noexcept
friend std::enable_if_t< std::is_unbounded_array_v< T0 >, unique_trackable_ptr< T0 > > make_unique_trackable(std::size_t N)
unique_trackable_ptr & operator=(unique_trackable_ptr< T2 > &&other) noexcept
void swap(unique_trackable_ptr &other) noexcept
Trinity::unique_trackable_ptr companion class, replicating what std::weak_ptr is to std::shared_ptr.
unique_weak_ptr & operator=(unique_weak_ptr &&other) noexcept=default
unique_strong_ref_ptr< element_type > lock() const
unique_weak_ptr & operator=(unique_weak_ptr< T2 > &&other)
friend unique_weak_ptr< To > dynamic_pointer_cast(unique_weak_ptr< From > const &other)
unique_weak_ptr(unique_weak_ptr &&other) noexcept=default
std::weak_ptr< element_type > _ptr
unique_weak_ptr & operator=(unique_weak_ptr const &other)=default
friend unique_weak_ptr< To > static_pointer_cast(unique_weak_ptr< From > const &other)
unique_weak_ptr(unique_trackable_ptr< T > const &trackable)
unique_weak_ptr(unique_weak_ptr< T2 > &&other) noexcept
friend unique_weak_ptr< To > reinterpret_pointer_cast(unique_weak_ptr< From > const &other)
unique_weak_ptr & operator=(unique_trackable_ptr< T > const &trackable)
unique_weak_ptr(unique_weak_ptr const &other)=default
friend unique_weak_ptr< To > const_pointer_cast(unique_weak_ptr< From > const &other)
void swap(unique_weak_ptr &other) noexcept
unique_weak_ptr(unique_weak_ptr< T2 > const &other) noexcept
unique_weak_ptr< To > reinterpret_pointer_cast(unique_weak_ptr< From > const &other)
bool operator==(unique_trackable_ptr< T1 > const &left, unique_trackable_ptr< T2 > const &right)
unique_weak_ptr< To > const_pointer_cast(unique_weak_ptr< From > const &other)
std::enable_if_t<!std::is_array_v< T >, unique_trackable_ptr< T > > make_unique_trackable(Args &&... args)
unique_weak_ptr< To > static_pointer_cast(unique_weak_ptr< From > const &other)
unique_weak_ptr< To > dynamic_pointer_cast(unique_weak_ptr< From > const &other)
std::strong_ordering operator<=>(unique_trackable_ptr< T1 > const &left, unique_trackable_ptr< T2 > const &right)
STL namespace.