diff options
author | Jacques Lucke <jacques@blender.org> | 2021-04-06 16:49:38 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-04-06 16:49:43 +0300 |
commit | b93c4458092357dd913477a41e201b425c35a898 (patch) | |
tree | 5f12509d15fea1ccd6dd87c7440fe18c082b0491 /source/blender/blenlib/BLI_resource_scope.hh | |
parent | 490801ba1d20bf9b2536ad08b8808393e9957c58 (diff) |
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.
Diffstat (limited to 'source/blender/blenlib/BLI_resource_scope.hh')
-rw-r--r-- | source/blender/blenlib/BLI_resource_scope.hh | 22 |
1 files 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<typename T> void add(std::unique_ptr<T> resource, const char *name) + template<typename T> T *add(std::unique_ptr<T> 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<T *>(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<typename T> void add(destruct_ptr<T> resource, const char *name) + template<typename T> T *add(destruct_ptr<T> 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<T>) { - resource.release(); - return; + return ptr; } - BLI_assert(resource.get() != nullptr); this->add( - resource.release(), + ptr, [](void *data) { T *typed_data = reinterpret_cast<T *>(data); typed_data->~T(); }, name); + return ptr; } /** |