userver: engine::TaskLocalVariable< T > Class Template Reference
Loading...
Searching...
No Matches
engine::TaskLocalVariable< T > Class Template Referencefinal

#include <userver/engine/task/local_variable.hpp>

Detailed Description

template<typename T>
class engine::TaskLocalVariable< T >

TaskLocalVariable is a per-coroutine variable of arbitrary type.

It is an alternative to thread_local, but per-task instead of per-thread.

The order of destruction of task-local variables is inverse to the order of initialization.

Definition at line 23 of file local_variable.hpp.

Public Member Functions

T & operator* ()
 Get the instance of the variable for the current coroutine. Initializes (default constructs) the variable if it was not previously initialized.
T * operator-> ()
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename... Args>
T & GetOrEmplace (Args &&... args)
 Get the variable instance for the current task, or emplace a new one with the given arguments.
T * GetOptional () noexcept
 Get the variable instance for the current task.

Member Function Documentation

◆ GetOptional()

template<typename T>
T * engine::TaskLocalVariable< T >::GetOptional ( )
inlinenoexcept

Get the variable instance for the current task.

Returns
the variable or nullptr if the variable was not initialized, was already destroyed, or is being destroyed right now. That is, a non-null result is guaranteed to point to a variable whose destruction has not started (the variable is unset before its destructor is invoked, as in POSIX pthread_getspecific).

Definition at line 52 of file local_variable.hpp.

◆ GetOrEmplace()

template<typename T>
template<typename... Args>
T & engine::TaskLocalVariable< T >::GetOrEmplace ( Args &&... args)
inline

Get the variable instance for the current task, or emplace a new one with the given arguments.

Note
Must be called from a coroutine, otherwise it is UB.

Definition at line 41 of file local_variable.hpp.

◆ operator*()

template<typename T>
T & engine::TaskLocalVariable< T >::operator* ( )

Get the instance of the variable for the current coroutine. Initializes (default constructs) the variable if it was not previously initialized.

Note
Must be called from a coroutine, otherwise it is UB.

Definition at line 63 of file local_variable.hpp.

◆ operator->()

template<typename T>
T * engine::TaskLocalVariable< T >::operator-> ( )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 68 of file local_variable.hpp.


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