Blender V4.5
blender::ResourceScope Class Reference

#include <BLI_resource_scope.hh>

Inherits blender::NonCopyable, and blender::NonMovable.

Public Member Functions

 ResourceScope ()
 
 ~ResourceScope ()
 
#ResourceScope Inline Methods
template<typename T >
Tadd (std::unique_ptr< T > resource)
 
template<typename T >
Tadd (destruct_ptr< T > resource)
 
void add (void *userdata, void(*free)(void *))
 
template<typename T >
Tadd_value (T &&value)
 
template<typename Func >
void add_destruct_call (Func func)
 
template<typename T , typename... Args>
Tconstruct (Args &&...args)
 
void * allocate_owned (const CPPType &type)
 
LinearAllocatorallocator ()
 

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 at line 37 of file BLI_resource_scope.hh.

Constructor & Destructor Documentation

◆ ResourceScope()

blender::ResourceScope::ResourceScope ( )
default

◆ ~ResourceScope()

blender::ResourceScope::~ResourceScope ( )

Member Function Documentation

◆ add() [1/3]

template<typename T >
T * blender::ResourceScope::add ( destruct_ptr< T > resource)
inline

Pass ownership of the resource to the ResourceScope. It will be destructed when the ResourceScope is destructed.

Definition at line 115 of file BLI_resource_scope.hh.

References add(), data, ptr, and resource.

◆ add() [2/3]

template<typename T >
T * blender::ResourceScope::add ( std::unique_ptr< T > resource)
inline

Pass ownership of the resource to the ResourceScope. It will be destructed and freed when the ResourceScope is destructed.

Definition at line 102 of file BLI_resource_scope.hh.

References add(), data, ptr, and resource.

Referenced by add(), add(), add_destruct_call(), construct(), and blender::ed::spreadsheet::spreadsheet_main_region_draw().

◆ add() [3/3]

void blender::ResourceScope::add ( void * userdata,
void(* free )(void *) )
inline

Pass ownership of some resource to the ResourceScope. The given free function will be called when the ResourceScope is destructed.

Definition at line 133 of file BLI_resource_scope.hh.

References blender::Vector< T, InlineBufferCapacity, Allocator >::append(), data, and free().

◆ add_destruct_call()

template<typename Func >
void blender::ResourceScope::add_destruct_call ( Func func)
inline

◆ add_value()

template<typename T >
T & blender::ResourceScope::add_value ( T && value)
inline

Construct an object with the same value in the ResourceScope and return a reference to the new value.

Definition at line 141 of file BLI_resource_scope.hh.

References construct(), and value.

◆ allocate_owned()

void * blender::ResourceScope::allocate_owned ( const CPPType & type)
inline

◆ allocator()

◆ construct()

template<typename T , typename... Args>
T & blender::ResourceScope::construct ( Args &&... args)
inline

The documentation for this class was generated from the following files: