From b93c4458092357dd913477a41e201b425c35a898 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 6 Apr 2021 15:49:38 +0200 Subject: BLI: return pointer to added resource Without this, the caller often has to get the pointer to the resource before adding it to the resource scope. --- source/blender/blenlib/BLI_resource_scope.hh | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/source/blender/blenlib/BLI_resource_scope.hh b/source/blender/blenlib/BLI_resource_scope.hh index f606dc0c0a1..e5a698f25f1 100644 --- a/source/blender/blenlib/BLI_resource_scope.hh +++ b/source/blender/blenlib/BLI_resource_scope.hh @@ -72,38 +72,46 @@ class ResourceScope : NonCopyable, NonMovable { * Pass ownership of the resource to the ResourceScope. It will be destructed and freed when * the collector is destructed. */ - template void add(std::unique_ptr resource, const char *name) + template T *add(std::unique_ptr resource, const char *name) { BLI_assert(resource.get() != nullptr); + T *ptr = resource.release(); + if (ptr == nullptr) { + return nullptr; + } this->add( - resource.release(), + ptr, [](void *data) { T *typed_data = reinterpret_cast(data); delete typed_data; }, name); + return ptr; } /** * Pass ownership of the resource to the ResourceScope. It will be destructed when the * collector is destructed. */ - template void add(destruct_ptr resource, const char *name) + template T *add(destruct_ptr resource, const char *name) { + T *ptr = resource.release(); + if (ptr == nullptr) { + return nullptr; + } /* There is no need to keep track of such types. */ if (std::is_trivially_destructible_v) { - resource.release(); - return; + return ptr; } - BLI_assert(resource.get() != nullptr); this->add( - resource.release(), + ptr, [](void *data) { T *typed_data = reinterpret_cast(data); typed_data->~T(); }, name); + return ptr; } /** -- cgit v1.2.3