aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/UniqueTrackablePtr.h
blob: 6c90dfd2752182d8fa693f013c19cc0e6be472ff (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRINITYCORE_UNIQUE_TRACKABLE_PTR_H
#define TRINITYCORE_UNIQUE_TRACKABLE_PTR_H

#include <memory>

namespace Trinity
{
template <typename T>
class unique_trackable_ptr;

template <typename T>
class unique_weak_ptr;

template <typename T>
class unique_strong_ref_ptr;

/**
 * \brief Specialized variant of std::shared_ptr that enforces unique ownership and/or std::unique_ptr with std::weak_ptr capabilities
 * Implementation has the same overhead as a std::shared_ptr, that is, a separate allocation for control block that holds use counters
 * \tparam T Type of held object
 */
template <typename T>
class unique_trackable_ptr
{
public:
    using element_type = T;
    using pointer = T*;

    unique_trackable_ptr() : _ptr() { }

    explicit unique_trackable_ptr(pointer ptr)
        : _ptr(ptr) { }

    template <typename Deleter, std::enable_if_t<std::conjunction_v<std::is_move_constructible<Deleter>, std::is_invocable<Deleter&, T*&>>, int> = 0>
    explicit unique_trackable_ptr(pointer ptr, Deleter deleter)
        : _ptr(ptr, std::move(deleter)) { }

    unique_trackable_ptr(unique_trackable_ptr const&) = delete;

    unique_trackable_ptr(unique_trackable_ptr&& other) noexcept
        : _ptr(std::move(other._ptr)) { }

    template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
    unique_trackable_ptr(unique_trackable_ptr<T2>&& other) noexcept
        : _ptr(std::move(other)._ptr) { }

    unique_trackable_ptr& operator=(unique_trackable_ptr const&) = delete;

    unique_trackable_ptr& operator=(unique_trackable_ptr&& other) noexcept
    {
        _ptr = std::move(other._ptr);
        return *this;
    }

    template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
    unique_trackable_ptr& operator=(unique_trackable_ptr<T2>&& other) noexcept
    {
        _ptr = std::move(other)._ptr;
        return *this;
    }

    ~unique_trackable_ptr() = default;

    unique_trackable_ptr& operator=(std::nullptr_t)
    {
        reset();
        return *this;
    }

    void swap(unique_trackable_ptr& other) noexcept
    {
        using std::swap;
        swap(_ptr, other._ptr);
    }

    element_type& operator*() const
    {
        return *_ptr;
    }

    pointer operator->() const
    {
        return _ptr.operator->();
    }

    pointer get() const
    {
        return _ptr.get();
    }

    explicit operator bool() const
    {
        return static_cast<bool>(_ptr);
    }

    void reset()
    {
        _ptr.reset();
    }

    void reset(pointer ptr)
    {
        _ptr.reset(ptr);
    }

    template <class Deleter, std::enable_if_t<std::conjunction_v<std::is_move_constructible<Deleter>, std::is_invocable<Deleter&, T*&>>, int> = 0>
    void reset(pointer ptr, Deleter deleter)
    {
        _ptr.reset(ptr, std::move(deleter));
    }

private:
    template <typename T0>
    friend class unique_trackable_ptr;

    template <typename T0>
    friend class unique_weak_ptr;

    template <typename T0, typename... Args>
    friend std::enable_if_t<!std::is_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable(Args&&... args);

    template <typename T0>
    friend std::enable_if_t<std::is_unbounded_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable(std::size_t N);

    template <typename T0>
    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);

    template <typename T0>
    friend std::enable_if_t<std::is_bounded_array_v<T0>, unique_trackable_ptr<T0>> make_unique_trackable();

    template <typename T0>
    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);

    std::shared_ptr<element_type> _ptr;
};

/**
 * \brief Trinity::unique_trackable_ptr companion class, replicating what std::weak_ptr is to std::shared_ptr
 * \tparam T Type of held object
 */
template <typename T>
class unique_weak_ptr
{
public:
    using element_type = T;
    using pointer = T*;

    unique_weak_ptr() = default;

    unique_weak_ptr(unique_trackable_ptr<T> const& trackable)
        : _ptr(trackable._ptr) { }

    unique_weak_ptr(unique_weak_ptr const& other) = default;

    template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
    unique_weak_ptr(unique_weak_ptr<T2> const& other) noexcept
        : _ptr(other._ptr) { }

    unique_weak_ptr(unique_weak_ptr&& other) noexcept = default;

    template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
    unique_weak_ptr(unique_weak_ptr<T2>&& other) noexcept
        : _ptr(std::move(other)._ptr) { }

    unique_weak_ptr& operator=(unique_trackable_ptr<T> const& trackable)
    {
        _ptr = trackable._ptr;
        return *this;
    }

    unique_weak_ptr& operator=(unique_weak_ptr const& other) = default;

    template <typename T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
    unique_weak_ptr& operator=(unique_weak_ptr<T2>&& other)
    {
        _ptr = std::move(other)._ptr;
        return *this;
    }

    unique_weak_ptr& operator=(unique_weak_ptr&& other) noexcept = default;

    ~unique_weak_ptr() = default;

    unique_weak_ptr& operator=(std::nullptr_t) noexcept
    {
        _ptr.reset();
        return *this;
    }

    void swap(unique_weak_ptr& other) noexcept
    {
        using std::swap;
        swap(_ptr, other._ptr);
    }

    bool expired() const
    {
        return _ptr.expired();
    }

    unique_strong_ref_ptr<element_type> lock() const
    {
        return unique_strong_ref_ptr<element_type>(_ptr.lock());
    }

private:
    template <typename T0>
    friend class unique_weak_ptr;

    template <typename T0>
    friend class unique_strong_ref_ptr;

    template <class To, class From>
    friend unique_weak_ptr<To> static_pointer_cast(unique_weak_ptr<From> const& other);

    template <class To, class From>
    friend unique_weak_ptr<To> const_pointer_cast(unique_weak_ptr<From> const& other);

    template <class To, class From>
    friend unique_weak_ptr<To> reinterpret_pointer_cast(unique_weak_ptr<From> const& other);

    template <class To, class From>
    friend unique_weak_ptr<To> dynamic_pointer_cast(unique_weak_ptr<From> const& other);

    std::weak_ptr<element_type> _ptr;
};

/**
 * \brief Result of unique_weak_ptr::lock() function, this class holds a temporary strong reference to held object
 * to prevent it from being deallocated by another thread while it is being actively accessed.
 * This class is non-movable and non-copypable and is intended only for short lived local variables
 * \tparam T Type of held object
 */
template <typename T>
class unique_strong_ref_ptr
{
public:
    using element_type = T;
    using pointer = T*;

    unique_strong_ref_ptr(unique_strong_ref_ptr const&) = delete;
    unique_strong_ref_ptr(unique_strong_ref_ptr&&) = delete;
    unique_strong_ref_ptr& operator=(unique_strong_ref_ptr const&) = delete;
    unique_strong_ref_ptr& operator=(unique_strong_ref_ptr&&) = delete;

    ~unique_strong_ref_ptr() = default;

    element_type& operator*() const
    {
        return *_ptr;
    }

    pointer operator->() const
    {
        return _ptr.operator->();
    }

    pointer get() const
    {
        return _ptr.get();
    }

    explicit operator bool() const
    {
        return static_cast<bool>(_ptr);
    }

    operator unique_weak_ptr<T>() const
    {
        unique_weak_ptr<T> weak;
        weak._ptr = _ptr;
        return weak;
    }

private:
    template <typename T0>
    friend class unique_weak_ptr;

    template <class To, class From>
    friend unique_strong_ref_ptr<To> static_pointer_cast(unique_strong_ref_ptr<From> const& other);

    template <class To, class From>
    friend unique_strong_ref_ptr<To> static_pointer_cast(unique_strong_ref_ptr<From>&& other);

    template <class To, class From>
    friend unique_strong_ref_ptr<To> const_pointer_cast(unique_strong_ref_ptr<From> const& other);

    template <class To, class From>
    friend unique_strong_ref_ptr<To> const_pointer_cast(unique_strong_ref_ptr<From>&& other);

    template <class To, class From>
    friend unique_strong_ref_ptr<To> reinterpret_pointer_cast(unique_strong_ref_ptr<From> const& other);

    template <class To, class From>
    friend unique_strong_ref_ptr<To> reinterpret_pointer_cast(unique_strong_ref_ptr<From>&& other);

    template <class To, class From>
    friend unique_strong_ref_ptr<To> dynamic_pointer_cast(unique_strong_ref_ptr<From> const& other);

    template <class To, class From>
    friend unique_strong_ref_ptr<To> dynamic_pointer_cast(unique_strong_ref_ptr<From>&& other);

    unique_strong_ref_ptr(std::shared_ptr<element_type> ptr) : _ptr(std::move(ptr)) { }

    std::shared_ptr<element_type> _ptr;
};

// unique_trackable_ptr funcions

template <typename T1, typename T2>
bool operator==(unique_trackable_ptr<T1> const& left, unique_trackable_ptr<T2> const& right)
{
    return left.get() == right.get();
}

template <typename T1, typename T2>
std::strong_ordering operator<=>(unique_trackable_ptr<T1> const& left, unique_trackable_ptr<T2> const& right)
{
    return left.get() <=> right.get();
}

template <typename T1>
bool operator==(unique_trackable_ptr<T1> const& left, std::nullptr_t)
{
    return left.get() == nullptr;
}

template <typename T1>
std::strong_ordering operator<=>(unique_trackable_ptr<T1> const& left, std::nullptr_t)
{
    return left.get() <=> nullptr;
}

template <typename T, typename... Args>
std::enable_if_t<!std::is_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable(Args&&... args)
{
    unique_trackable_ptr<T> ptr;
    ptr._ptr = std::make_shared<T>(std::forward<Args>(args)...);
    return ptr;
}

template <typename T>
std::enable_if_t<std::is_unbounded_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable(std::size_t N)
{
    unique_trackable_ptr<T> ptr;
    ptr._ptr = std::make_shared<T>(N);
    return ptr;
}

template <typename T>
std::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)
{
    unique_trackable_ptr<T> ptr;
    ptr._ptr = std::make_shared<T>(N, val);
    return ptr;
}

template <typename T>
std::enable_if_t<std::is_bounded_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable()
{
    unique_trackable_ptr<T> ptr;
    ptr._ptr = std::make_shared<T>();
    return ptr;
}

template <typename T>
std::enable_if_t<std::is_bounded_array_v<T>, unique_trackable_ptr<T>> make_unique_trackable(std::remove_extent_t<T> const& val)
{
    unique_trackable_ptr<T> ptr;
    ptr._ptr = std::make_shared<T>(val);
    return ptr;
}

// unique_weak_ptr funcions

template <class To, class From>
unique_weak_ptr<To> static_pointer_cast(unique_weak_ptr<From> const& other)
{
    unique_weak_ptr<To> to;
    to._ptr = std::static_pointer_cast<To>(other._ptr.lock());
    return to;
}

template <class To, class From>
unique_weak_ptr<To> const_pointer_cast(unique_weak_ptr<From> const& other)
{
    unique_weak_ptr<To> to;
    to._ptr = std::const_pointer_cast<To>(other._ptr.lock());
    return to;
}

template <class To, class From>
unique_weak_ptr<To> reinterpret_pointer_cast(unique_weak_ptr<From> const& other)
{
    unique_weak_ptr<To> to;
    to._ptr = std::reinterpret_pointer_cast<To>(other._ptr.lock());
    return to;
}

template <class To, class From>
unique_weak_ptr<To> dynamic_pointer_cast(unique_weak_ptr<From> const& other)
{
    unique_weak_ptr<To> to;
    to._ptr = std::dynamic_pointer_cast<To>(other._ptr.lock());
    return to;
}

// unique_strong_ref_ptr funcions

template <typename T1, typename T2>
bool operator==(unique_strong_ref_ptr<T1> const& left, unique_strong_ref_ptr<T2> const& right)
{
    return left.get() == right.get();
}

template <typename T1, typename T2>
std::strong_ordering operator<=>(unique_strong_ref_ptr<T1> const& left, unique_strong_ref_ptr<T2> const& right)
{
    return left.get() <=> right.get();
}

template <typename T1>
bool operator==(unique_strong_ref_ptr<T1> const& left, std::nullptr_t)
{
    return left.get() == nullptr;
}

template <typename T1>
std::strong_ordering operator<=>(unique_strong_ref_ptr<T1> const& left, std::nullptr_t)
{
    return left.get() <=> nullptr;
}

template <class To, class From>
unique_strong_ref_ptr<To> static_pointer_cast(unique_strong_ref_ptr<From> const& other)
{
    return unique_strong_ref_ptr<To>(std::static_pointer_cast<To>(other._ptr));
}

template <class To, class From>
unique_strong_ref_ptr<To> static_pointer_cast(unique_strong_ref_ptr<From>&& other)
{
    return unique_strong_ref_ptr<To>(std::static_pointer_cast<To>(std::move(other._ptr)));
}

template <class To, class From>
unique_strong_ref_ptr<To> const_pointer_cast(unique_strong_ref_ptr<From> const& other)
{
    return unique_strong_ref_ptr<To>(std::const_pointer_cast<To>(other._ptr));
}

template <class To, class From>
unique_strong_ref_ptr<To> const_pointer_cast(unique_strong_ref_ptr<From>&& other)
{
    return unique_strong_ref_ptr<To>(std::const_pointer_cast<To>(std::move(other._ptr)));
}

template <class To, class From>
unique_strong_ref_ptr<To> reinterpret_pointer_cast(unique_strong_ref_ptr<From> const& other)
{
    return unique_strong_ref_ptr<To>(std::reinterpret_pointer_cast<To>(other._ptr));
}

template <class To, class From>
unique_strong_ref_ptr<To> reinterpret_pointer_cast(unique_strong_ref_ptr<From>&& other)
{
    return unique_strong_ref_ptr<To>(std::reinterpret_pointer_cast<To>(std::move(other._ptr)));
}

template <class To, class From>
unique_strong_ref_ptr<To> dynamic_pointer_cast(unique_strong_ref_ptr<From> const& other)
{
    return unique_strong_ref_ptr<To>(std::dynamic_pointer_cast<To>(other._ptr));
}

template <class To, class From>
unique_strong_ref_ptr<To> dynamic_pointer_cast(unique_strong_ref_ptr<From>&& other)
{
    return unique_strong_ref_ptr<To>(std::dynamic_pointer_cast<To>(std::move(other._ptr)));
}
}

#endif // TRINITYCORE_UNIQUE_TRACKABLE_PTR_H