Iros
 
Loading...
Searching...
No Matches
intrusive_ptr.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/types/prelude.h"
4#include "di/util/exchange.h"
6
7#if __GNUC__ >= 12
8#pragma GCC diagnostic push
9#pragma GCC diagnostic ignored "-Wuse-after-free"
10#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
11#endif
12
13namespace di::vocab {
15 RetainObject() = default;
16};
17constexpr inline auto retain_object = RetainObject {};
18
20 AdoptObject() = default;
21};
22constexpr inline auto adopt_object = AdoptObject {};
23
24template<typename T, typename Tag>
26public:
27 constexpr IntrusivePtr() : m_pointer(nullptr) {}
29
30 constexpr IntrusivePtr(IntrusivePtr const& other) { reset(other.get(), adopt_object); }
31 constexpr IntrusivePtr(IntrusivePtr&& other) : m_pointer(other.release()) {}
32
33 constexpr IntrusivePtr(T* pointer, RetainObject) : m_pointer(pointer) {
34 static_assert(detail::IntrusivePtrValid<T, Tag>, "Invalid intrusive pointer Tag type.");
35 }
36 constexpr IntrusivePtr(T* pointer, AdoptObject) {
37 static_assert(detail::IntrusivePtrValid<T, Tag>, "Invalid intrusive pointer Tag type.");
38 reset(pointer, adopt_object);
39 }
40
41 constexpr ~IntrusivePtr() { reset(); }
42
43 constexpr auto operator=(nullptr_t) -> IntrusivePtr& {
44 reset();
45 return *this;
46 }
47 constexpr auto operator=(IntrusivePtr const& other) -> IntrusivePtr& {
48 if (this != &other) {
49 reset(other.get(), adopt_object);
50 }
51 return *this;
52 }
53 constexpr auto operator=(IntrusivePtr&& other) -> IntrusivePtr& {
54 if (this != &other) {
55 reset();
56 m_pointer = other.release();
57 }
58 return *this;
59 }
60
61 constexpr explicit operator bool() const { return !!get(); }
62
63 constexpr auto operator*() const -> T& { return *get(); }
64 constexpr auto operator->() const -> T* { return get(); }
65 constexpr auto get() const -> T* { return m_pointer; }
66
67 [[nodiscard]] constexpr auto release() -> T* { return util::exchange(m_pointer, nullptr); }
68
69 constexpr void reset() {
70 auto* old = release();
71 if (old) {
73 }
74 }
75 constexpr void reset(T* pointer, RetainObject) {
76 reset();
77 m_pointer = pointer;
78 }
79 constexpr void reset(T* pointer, AdoptObject) {
80 reset();
81 m_pointer = pointer;
82 if (*this) {
84 }
85 }
86
87private:
88 constexpr friend auto operator==(IntrusivePtr const& a, IntrusivePtr const& b) -> bool {
89 return a.get() == b.get();
90 }
91 constexpr friend auto operator<=>(IntrusivePtr const& a, IntrusivePtr const& b) -> di::strong_ordering {
92 return a.get() <=> b.get();
93 }
94
95 T* m_pointer { nullptr };
96};
97}
98
99#if __GNUC__ >= 12
100#pragma GCC diagnostic pop
101#endif
102
103namespace di {
109}
Definition intrusive_ptr.h:25
constexpr friend auto operator==(IntrusivePtr const &a, IntrusivePtr const &b) -> bool
Definition intrusive_ptr.h:88
constexpr IntrusivePtr(IntrusivePtr &&other)
Definition intrusive_ptr.h:31
constexpr auto operator*() const -> T &
Definition intrusive_ptr.h:63
constexpr auto operator=(nullptr_t) -> IntrusivePtr &
Definition intrusive_ptr.h:43
constexpr auto operator->() const -> T *
Definition intrusive_ptr.h:64
constexpr IntrusivePtr(nullptr_t)
Definition intrusive_ptr.h:28
constexpr IntrusivePtr(IntrusivePtr const &other)
Definition intrusive_ptr.h:30
constexpr IntrusivePtr(T *pointer, RetainObject)
Definition intrusive_ptr.h:33
constexpr auto operator=(IntrusivePtr const &other) -> IntrusivePtr &
Definition intrusive_ptr.h:47
constexpr auto release() -> T *
Definition intrusive_ptr.h:67
constexpr void reset()
Definition intrusive_ptr.h:69
constexpr void reset(T *pointer, RetainObject)
Definition intrusive_ptr.h:75
constexpr void reset(T *pointer, AdoptObject)
Definition intrusive_ptr.h:79
constexpr IntrusivePtr(T *pointer, AdoptObject)
Definition intrusive_ptr.h:36
constexpr auto get() const -> T *
Definition intrusive_ptr.h:65
constexpr friend auto operator<=>(IntrusivePtr const &a, IntrusivePtr const &b) -> di::strong_ordering
Definition intrusive_ptr.h:91
constexpr ~IntrusivePtr()
Definition intrusive_ptr.h:41
constexpr IntrusivePtr()
Definition intrusive_ptr.h:27
constexpr auto operator=(IntrusivePtr &&other) -> IntrusivePtr &
Definition intrusive_ptr.h:53
Definition intrusive_ptr_cpo.h:30
std::nullptr_t nullptr_t
Definition nullptr_t.h:12
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
Definition lazy.h:165
constexpr auto adopt_object
Definition intrusive_ptr.h:22
constexpr auto intrusive_ptr_decrement
Definition intrusive_ptr_cpo.h:26
constexpr auto retain_object
Definition intrusive_ptr.h:17
constexpr auto intrusive_ptr_increment
Definition intrusive_ptr_cpo.h:25
Definition zstring_parser.h:9
constexpr auto in_place_type
Definition in_place_type.h:12
Definition intrusive_ptr.h:19
Definition intrusive_ptr.h:14