Blender V4.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
11#include <memory>
12#include <utility>
13
16
17namespace blender {
18
24template<typename T = ImplicitSharingInfo, bool IsStrong = true> class ImplicitSharingPtr {
25 private:
26 const T *data_ = nullptr;
27
28 public:
29 ImplicitSharingPtr() = default;
30
31 explicit ImplicitSharingPtr(const T *data) : data_(data) {}
32
33 /* Implicit conversion from nullptr. */
34 ImplicitSharingPtr(std::nullptr_t) : data_(nullptr) {}
35
36 ImplicitSharingPtr(const ImplicitSharingPtr &other) : data_(other.data_)
37 {
38 this->add_user(data_);
39 }
40
41 ImplicitSharingPtr(ImplicitSharingPtr &&other) : data_(other.data_)
42 {
43 other.data_ = nullptr;
44 }
45
47 {
48 this->remove_user_and_delete_if_last(data_);
49 }
50
52 {
53 if (this == &other) {
54 return *this;
55 }
56
57 this->remove_user_and_delete_if_last(data_);
58 data_ = other.data_;
59 this->add_user(data_);
60 return *this;
61 }
62
64 {
65 if (this == &other) {
66 return *this;
67 }
68
69 this->remove_user_and_delete_if_last(data_);
70 data_ = other.data_;
71 other.data_ = nullptr;
72 return *this;
73 }
74
75 const T *operator->() const
76 {
77 BLI_assert(data_ != nullptr);
78 return data_;
79 }
80
81 const T &operator*() const
82 {
83 BLI_assert(data_ != nullptr);
84 return *data_;
85 }
86
87 operator bool() const
88 {
89 return data_ != nullptr;
90 }
91
92 const T *get() const
93 {
94 return data_;
95 }
96
97 const T *release()
98 {
99 const T *data = data_;
100 data_ = nullptr;
101 return data;
102 }
103
104 void reset()
105 {
106 this->remove_user_and_delete_if_last(data_);
107 data_ = nullptr;
108 }
109
110 bool has_value() const
111 {
112 return data_ != nullptr;
113 }
114
116 {
117 return get_default_hash(data_);
118 }
119
120 static uint64_t hash_as(const T *data)
121 {
122 return get_default_hash(data);
123 }
124
126
127 friend bool operator==(const T *a, const ImplicitSharingPtr &b)
128 {
129 return a == b.data_;
130 }
131
132 friend bool operator==(const ImplicitSharingPtr &a, const T *b)
133 {
134 return a.data_ == b;
135 }
136
137 private:
138 static void add_user(const T *data)
139 {
140 if (data != nullptr) {
141 if constexpr (IsStrong) {
142 data->add_user();
143 }
144 else {
145 data->add_weak_user();
146 }
147 }
148 }
149
150 static void remove_user_and_delete_if_last(const T *data)
151 {
152 if (data != nullptr) {
153 if constexpr (IsStrong) {
154 data->remove_user_and_delete_if_last();
155 }
156 else {
157 data->remove_weak_user_and_delete_if_last();
158 }
159 }
160 }
161};
162
164
174 public:
176 const void *data = nullptr;
177
180 : sharing_info(std::move(sharing_info)), data(data)
181 {
182 }
183
185 : sharing_info(other.sharing_info), data(other.data)
186 {
187 }
188
190 : sharing_info(std::move(other.sharing_info)), data(std::exchange(other.data, nullptr))
191 {
192 }
193
195 {
196 if (this == &other) {
197 return *this;
198 }
199 std::destroy_at(this);
200 new (this) ImplicitSharingPtrAndData(other);
201 return *this;
202 }
203
205 {
206 if (this == &other) {
207 return *this;
208 }
209 std::destroy_at(this);
210 new (this) ImplicitSharingPtrAndData(std::move(other));
211 return *this;
212 }
213
215 {
216 this->data = nullptr;
217 }
218
219 bool has_value() const
220 {
221 return this->sharing_info.has_value();
222 }
223};
224
225} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_STRUCT_EQUALITY_OPERATORS_1(Type, m)
ImplicitSharingPtrAndData & operator=(const ImplicitSharingPtrAndData &other)
ImplicitSharingPtrAndData & operator=(ImplicitSharingPtrAndData &&other)
ImplicitSharingPtrAndData(ImplicitSharingPtr<> sharing_info, const void *data)
ImplicitSharingPtrAndData(const ImplicitSharingPtrAndData &other)
ImplicitSharingPtrAndData(ImplicitSharingPtrAndData &&other)
friend bool operator==(const ImplicitSharingPtr &a, const T *b)
ImplicitSharingPtr & operator=(ImplicitSharingPtr &&other)
ImplicitSharingPtr(ImplicitSharingPtr &&other)
static uint64_t hash_as(const T *data)
ImplicitSharingPtr & operator=(const ImplicitSharingPtr &other)
ImplicitSharingPtr(const ImplicitSharingPtr &other)
local_group_size(16, 16) .push_constant(Type b
uint64_t get_default_hash(const T &v)
Definition BLI_hash.hh:219
unsigned __int64 uint64_t
Definition stdint.h:90