Blender V5.3
BLI_implicit_sharing_ptr.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <memory>
12#include <utility>
13
15
16namespace blender {
17
23template<typename T = ImplicitSharingInfo, bool IsStrong = true> class ImplicitSharingPtr {
24 private:
25 const T *data_ = nullptr;
26
27 template<typename U, bool OtherIsStrong> friend class ImplicitSharingPtr;
28
29 public:
30 using element_type = T;
31
32 ImplicitSharingPtr() = default;
33
34 explicit ImplicitSharingPtr(const T *data) : data_(data) {}
35
36 /* Implicit conversion from nullptr. */
37 ImplicitSharingPtr(std::nullptr_t) : data_(nullptr) {}
38
39 ImplicitSharingPtr(const ImplicitSharingPtr &other) : data_(other.data_)
40 {
41 this->add_user(data_);
42 }
43
44 ImplicitSharingPtr(ImplicitSharingPtr &&other) : data_(other.data_)
45 {
46 other.data_ = nullptr;
47 }
48
49 template<typename U>
51 requires std::is_base_of_v<T, U>
52 : data_(static_cast<const U *>(other.data_))
53 {
54 this->add_user(data_);
55 }
56
57 template<typename U>
59 requires std::is_base_of_v<T, U>
60 : data_(static_cast<U *>(other.data_))
61 {
62 other.data_ = nullptr;
63 }
64
66 {
67 this->remove_user_and_delete_if_last(data_);
68 }
69
71 {
72 if (this == &other) {
73 return *this;
74 }
75
76 this->remove_user_and_delete_if_last(data_);
77 data_ = other.data_;
78 this->add_user(data_);
79 return *this;
80 }
81
83 {
84 if (this == &other) {
85 return *this;
86 }
87
88 this->remove_user_and_delete_if_last(data_);
89 data_ = other.data_;
90 other.data_ = nullptr;
91 return *this;
92 }
93
94 const T *operator->() const
95 {
96 BLI_assert(data_ != nullptr);
97 return data_;
98 }
99
100 const T &operator*() const
101 {
102 BLI_assert(data_ != nullptr);
103 return *data_;
104 }
105
106 operator bool() const
107 {
108 return data_ != nullptr;
109 }
110
111 const T *get() const
112 {
113 return data_;
114 }
115
116 const T *release()
117 {
118 const T *data = data_;
119 data_ = nullptr;
120 return data;
121 }
122
123 void reset()
124 {
125 this->remove_user_and_delete_if_last(data_);
126 data_ = nullptr;
127 }
128
129 bool has_value() const
130 {
131 return data_ != nullptr;
132 }
133
135 {
136 return get_default_hash(data_);
137 }
138
145 {
146 BLI_assert(data_);
147 if (!data_->is_mutable()) {
148 /* The data is shared and therefore immutable. Make a mutable copy. */
149 *this = data_->copy();
150 }
151 BLI_assert(data_->is_mutable());
152 data_->tag_ensured_mutable();
153 return const_cast<T &>(*data_);
154 }
155
156 static uint64_t hash_as(const T *data)
157 {
158 return get_default_hash(data);
159 }
160
161 friend bool operator==(const ImplicitSharingPtr &a, const ImplicitSharingPtr &b) = default;
162
163 friend bool operator==(const T *a, const ImplicitSharingPtr &b)
164 {
165 return a == b.data_;
166 }
167
168 friend bool operator==(const ImplicitSharingPtr &a, const T *b)
169 {
170 return a.data_ == b;
171 }
172
173 private:
174 static void add_user(const T *data)
175 {
176 if (data != nullptr) {
177 if constexpr (IsStrong) {
178 data->add_user();
179 }
180 else {
181 data->add_weak_user();
182 }
183 }
184 }
185
186 static void remove_user_and_delete_if_last(const T *data)
187 {
188 if (data != nullptr) {
189 if constexpr (IsStrong) {
190 data->remove_user_and_delete_if_last();
191 }
192 else {
193 data->remove_weak_user_and_delete_if_last();
194 }
195 }
196 }
197};
198
200
210 public:
212 const void *data = nullptr;
213
219
221
223 : sharing_info(std::move(other.sharing_info)), data(std::exchange(other.data, nullptr))
224 {
225 }
226
228 {
229 if (this == &other) {
230 return *this;
231 }
232 std::destroy_at(this);
233 new (this) ImplicitSharingPtrAndData(other);
234 return *this;
235 }
236
238 {
239 if (this == &other) {
240 return *this;
241 }
242 std::destroy_at(this);
243 new (this) ImplicitSharingPtrAndData(std::move(other));
244 return *this;
245 }
246
248 {
249 this->data = nullptr;
250 }
251
252 bool has_value() const
253 {
254 return this->sharing_info.has_value();
255 }
256};
257
258template<typename T> static constexpr bool is_ImplicitSharingPtr_strong_v = false;
259template<typename T>
261
262} // namespace blender
ImplicitSharingPtrAndData & operator=(const ImplicitSharingPtrAndData &other)
ImplicitSharingPtrAndData & operator=(ImplicitSharingPtrAndData &&other)
ImplicitSharingPtrAndData(ImplicitSharingPtr<> sharing_info, const void *data)
ImplicitSharingPtrAndData(const ImplicitSharingPtrAndData &other)=default
ImplicitSharingPtrAndData(ImplicitSharingPtrAndData &&other)
friend bool operator==(const ImplicitSharingPtr &a, const T *b)
ImplicitSharingPtr & operator=(ImplicitSharingPtr &&other)
friend bool operator==(const ImplicitSharingPtr &a, const ImplicitSharingPtr &b)=default
ImplicitSharingPtr(ImplicitSharingPtr &&other)
static uint64_t hash_as(const T *data)
friend bool operator==(const T *a, const ImplicitSharingPtr &b)
ImplicitSharingPtr & operator=(const ImplicitSharingPtr &other)
ImplicitSharingPtr(const ImplicitSharingPtr &other)
ImplicitSharingPtr(ImplicitSharingPtr< U, IsStrong > &&other)
ImplicitSharingPtr(const ImplicitSharingPtr< U, IsStrong > &other)
#define T
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
ImplicitSharingPtr< ImplicitSharingInfo, false > WeakImplicitSharingPtr
BMesh const char void * data
static constexpr bool is_ImplicitSharingPtr_strong_v
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252