From 37ca8347a2876ba8484ebaca5ef9fd0786df2714 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 16 Aug 2024 17:16:40 +0200 Subject: [PATCH] BLI: support smart pointer for weak implicit sharing pointer Previously, one always had to do manual user management for weak users. --- .../blenlib/BLI_implicit_sharing_ptr.hh | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/source/blender/blenlib/BLI_implicit_sharing_ptr.hh b/source/blender/blenlib/BLI_implicit_sharing_ptr.hh index ca73c0354ef..66654cce407 100644 --- a/source/blender/blenlib/BLI_implicit_sharing_ptr.hh +++ b/source/blender/blenlib/BLI_implicit_sharing_ptr.hh @@ -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 class ImplicitSharingPtr { +template class ImplicitSharingPtr { private: const T *data_ = nullptr; @@ -117,24 +117,51 @@ template 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; + /** * 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