Blender V5.0
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

References free(), resource, and T.

◆ ~ResourceScope()

blender::ResourceScope::~ResourceScope ( )

Definition at line 15 of file resource_scope.cc.

References data, and i.

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, resource, and T.

◆ 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, resource, and T.

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 data, and free().

◆ add_destruct_call()

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

The passed in function will be called when the scope is destructed.

Definition at line 146 of file BLI_resource_scope.hh.

References add(), and data.

Referenced by allocate_owned(), blender::bke::defer_free_node_type(), blender::bke::defer_free_socket_type(), blender::bke::defer_free_tree_type(), and blender::fn::evaluate_fields().

◆ 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 T.

Referenced by blender::ed::space_node::node_find_update_fn().

◆ allocate_owned()

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

Allocate a buffer for the given type. The caller is responsible for initializing it before the ResourceScope is destructed. The value in the returned buffer is destructed automatically.

Definition at line 161 of file BLI_resource_scope.hh.

References add_destruct_call(), blender::CPPType::destruct(), and blender::CPPType::is_trivially_destructible.

Referenced by blender::fn::evaluate_fields(), and blender::nodes::socket_usage_inference::infer_group_interface_inputs_usage().

◆ allocator()

◆ construct()


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