Blender V4.5
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
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
183
185
187 : sharing_info(std::move(other.sharing_info)), data(std::exchange(other.data, nullptr))
188 {
189 }
190
192 {
193 if (this == &other) {
194 return *this;
195 }
196 std::destroy_at(this);
197 new (this) ImplicitSharingPtrAndData(other);
198 return *this;
199 }
200
202 {
203 if (this == &other) {
204 return *this;
205 }
206 std::destroy_at(this);
207 new (this) ImplicitSharingPtrAndData(std::move(other));
208 return *this;
209 }
210
212 {
213 this->data = nullptr;
214 }
215
216 bool has_value() const
217 {
218 return this->sharing_info.has_value();
219 }
220};
221
222} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_STRUCT_EQUALITY_OPERATORS_1(Type, m)
BMesh const char void * data
unsigned long long int uint64_t
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)
ImplicitSharingPtr(ImplicitSharingPtr &&other)
static uint64_t hash_as(const T *data)
ImplicitSharingPtr & operator=(const ImplicitSharingPtr &other)
ImplicitSharingPtr(const ImplicitSharingPtr &other)
#define T
ImplicitSharingPtr< ImplicitSharingInfo, false > WeakImplicitSharingPtr
uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:233