Blender  V3.3
Classes | Namespaces
BLI_resource_scope.hh File Reference
#include "BLI_linear_allocator.hh"
#include "BLI_utility_mixins.hh"
#include "BLI_vector.hh"

Go to the source code of this file.

Classes

class  blender::ResourceScope
 

Namespaces

 blender
 

Detailed Description

A ResourceScope takes ownership of arbitrary data/resources. Those resources will be destructed and/or freed when the ResourceScope is destructed. Destruction happens in reverse order. That allows resources do depend on other resources that have been added before.

A ResourceScope can also be thought of as a dynamic/runtime version of normal scopes in C++ that are surrounded by braces.

The main purpose of a ResourceScope is to allow functions to inject data into the scope of the caller. Traditionally, that can only be done by returning a value that owns everything it needs. This is fine until one has to deal with optional ownership. There are many ways to have a type optionally own something else, all of which are fairly annoying. A ResourceScope can be used to avoid having to deal with optional ownership. If some value would be owned, it can just be added to the resource scope, otherwise not.

When a function takes a ResourceScope as parameter, it usually means that its return value will live at least as long as the passed in resources scope. However, it might also live longer. That can happen when the function returns a reference to statically allocated data or dynamically allocated data depending on some condition.

Definition in file BLI_resource_scope.hh.