BLI: support smart pointer for weak implicit sharing pointer
Previously, one always had to do manual user management for weak users.
This commit is contained in:
@@ -21,7 +21,7 @@ namespace blender {
|
||||
* types that derive from #ImplicitSharingMixin. It is fairly similar to #std::shared_ptr but
|
||||
* requires the reference count to be embedded in the data.
|
||||
*/
|
||||
template<typename T> class ImplicitSharingPtr {
|
||||
template<typename T, bool IsStrong = true> class ImplicitSharingPtr {
|
||||
private:
|
||||
const T *data_ = nullptr;
|
||||
|
||||
@@ -117,24 +117,51 @@ template<typename T> class ImplicitSharingPtr {
|
||||
return get_default_hash(data_);
|
||||
}
|
||||
|
||||
static uint64_t hash_as(const T *data)
|
||||
{
|
||||
return get_default_hash(data);
|
||||
}
|
||||
|
||||
BLI_STRUCT_EQUALITY_OPERATORS_1(ImplicitSharingPtr, data_)
|
||||
|
||||
friend bool operator==(const T *a, const ImplicitSharingPtr &b)
|
||||
{
|
||||
return a == b.data_;
|
||||
}
|
||||
|
||||
friend bool operator==(const ImplicitSharingPtr &a, const T *b)
|
||||
{
|
||||
return a.data_ == b;
|
||||
}
|
||||
|
||||
private:
|
||||
static void add_user(const T *data)
|
||||
{
|
||||
if (data != nullptr) {
|
||||
data->add_user();
|
||||
if constexpr (IsStrong) {
|
||||
data->add_user();
|
||||
}
|
||||
else {
|
||||
data->add_weak_user();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void remove_user_and_delete_if_last(const T *data)
|
||||
{
|
||||
if (data != nullptr) {
|
||||
data->remove_user_and_delete_if_last();
|
||||
if constexpr (IsStrong) {
|
||||
data->remove_user_and_delete_if_last();
|
||||
}
|
||||
else {
|
||||
data->remove_weak_user_and_delete_if_last();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
using WeakImplicitSharingPtr = ImplicitSharingPtr<ImplicitSharingInfo, false>;
|
||||
|
||||
/**
|
||||
* Utility struct to allow used #ImplicitSharingPtr when it's necessary to type-erase the backing
|
||||
* storage for user-exposed data. For example, #blender::Vector, or #std::vector might be used to
|
||||
|
||||
Reference in New Issue
Block a user