From e1e75bd62c33395ca29e4ad456b416f8a6923d46 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sun, 3 Oct 2021 14:23:26 +0200 Subject: Cleanup: move resource scope method definitions out of class --- source/blender/blenlib/BLI_resource_scope.hh | 193 ++++++++++++++------------- 1 file changed, 100 insertions(+), 93 deletions(-) (limited to 'source/blender/blenlib/BLI_resource_scope.hh') diff --git a/source/blender/blenlib/BLI_resource_scope.hh b/source/blender/blenlib/BLI_resource_scope.hh index 761e1ef834c..8e88e251d30 100644 --- a/source/blender/blenlib/BLI_resource_scope.hh +++ b/source/blender/blenlib/BLI_resource_scope.hh @@ -56,106 +56,113 @@ class ResourceScope : NonCopyable, NonMovable { Vector resources_; public: - ResourceScope() = default; - - ~ResourceScope() - { - /* Free in reversed order. */ - for (int64_t i = resources_.size(); i--;) { - ResourceData &data = resources_[i]; - data.free(data.data); - } - } + ResourceScope(); + ~ResourceScope(); - /** - * Pass ownership of the resource to the ResourceScope. It will be destructed and freed when - * the collector is destructed. - */ - template T *add(std::unique_ptr resource) - { - T *ptr = resource.release(); - if (ptr == nullptr) { - return nullptr; - } - this->add(ptr, [](void *data) { - T *typed_data = reinterpret_cast(data); - delete typed_data; - }); - return ptr; - } + template T *add(std::unique_ptr resource); + template T *add(destruct_ptr resource); + void add(void *userdata, void (*free)(void *)); - /** - * Pass ownership of the resource to the ResourceScope. It will be destructed when the - * collector is destructed. - */ - template T *add(destruct_ptr resource) - { - 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) { - return ptr; - } - - this->add(ptr, [](void *data) { - T *typed_data = reinterpret_cast(data); - typed_data->~T(); - }); - return ptr; - } + template T &add_value(T &&value); + template void add_destruct_call(Func func); - /** - * Pass ownership of some resource to the ResourceScope. The given free function will be - * called when the collector is destructed. - */ - void add(void *userdata, void (*free)(void *)) - { - ResourceData data; - data.data = userdata; - data.free = free; - resources_.append(data); - } + template T &construct(Args &&...args); - /** - * Construct an object with the same value in the ResourceScope and return a reference to the - * new value. - */ - template T &add_value(T &&value) - { - return this->construct(std::forward(value)); - } + LinearAllocator<> &linear_allocator(); +}; - /** - * The passed in function will be called when the scope is destructed. - */ - template void add_destruct_call(Func func) - { - void *buffer = allocator_.allocate(sizeof(Func), alignof(Func)); - new (buffer) Func(std::move(func)); - this->add(buffer, [](void *data) { (*(Func *)data)(); }); - } +/* -------------------------------------------------------------------- + * #ResourceScope inline methods. + */ - /** - * Returns a reference to a linear allocator that is owned by the ResourcesCollector. Memory - * allocated through this allocator will be freed when the collector is destructed. - */ - LinearAllocator<> &linear_allocator() - { - return allocator_; +/** + * Pass ownership of the resource to the ResourceScope. It will be destructed and freed when + * the collector is destructed. + */ +template inline T *ResourceScope::add(std::unique_ptr resource) +{ + T *ptr = resource.release(); + if (ptr == nullptr) { + return nullptr; } - - /** - * Utility method to construct an instance of type T that will be owned by the ResourceScope. - */ - template T &construct(Args &&...args) - { - destruct_ptr value_ptr = allocator_.construct(std::forward(args)...); - T &value_ref = *value_ptr; - this->add(std::move(value_ptr)); - return value_ref; + this->add(ptr, [](void *data) { + T *typed_data = reinterpret_cast(data); + delete typed_data; + }); + return ptr; +} + +/** + * Pass ownership of the resource to the ResourceScope. It will be destructed when the + * collector is destructed. + */ +template inline T *ResourceScope::add(destruct_ptr resource) +{ + T *ptr = resource.release(); + if (ptr == nullptr) { + return nullptr; } -}; + /* There is no need to keep track of such types. */ + if constexpr (std::is_trivially_destructible_v) { + return ptr; + } + + this->add(ptr, [](void *data) { + T *typed_data = reinterpret_cast(data); + typed_data->~T(); + }); + return ptr; +} + +/** + * Pass ownership of some resource to the ResourceScope. The given free function will be + * called when the collector is destructed. + */ +inline void ResourceScope::add(void *userdata, void (*free)(void *)) +{ + ResourceData data; + data.data = userdata; + data.free = free; + resources_.append(data); +} + +/** + * Construct an object with the same value in the ResourceScope and return a reference to the + * new value. + */ +template inline T &ResourceScope::add_value(T &&value) +{ + return this->construct(std::forward(value)); +} + +/** + * The passed in function will be called when the scope is destructed. + */ +template inline void ResourceScope::add_destruct_call(Func func) +{ + void *buffer = allocator_.allocate(sizeof(Func), alignof(Func)); + new (buffer) Func(std::move(func)); + this->add(buffer, [](void *data) { (*(Func *)data)(); }); +} + +/** + * Utility method to construct an instance of type T that will be owned by the ResourceScope. + */ +template inline T &ResourceScope::construct(Args &&...args) +{ + destruct_ptr value_ptr = allocator_.construct(std::forward(args)...); + T &value_ref = *value_ptr; + this->add(std::move(value_ptr)); + return value_ref; +} + +/** + * Returns a reference to a linear allocator that is owned by the ResourcesCollector. Memory + * allocated through this allocator will be freed when the collector is destructed. + */ +inline LinearAllocator<> &ResourceScope::linear_allocator() +{ + return allocator_; +} } // namespace blender -- cgit v1.2.3