userver
C++ Async Framework
Toggle main menu visibility
Documentation
API Groups
Namespaces
Reference
Class List
Class Index
File List
Macros
All
e
i
l
r
t
u
Functions
Macros
e
i
l
r
t
u
Examples
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Modules
Pages
Concepts
Loading...
Searching...
No Matches
local_variable.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/engine/task/local_variable.hpp
4
/// @brief @copybrief engine::TaskLocalVariable
5
6
#
include
<
type_traits
>
7
8
#
include
<
userver
/
engine
/
impl
/
task_local_storage
.
hpp
>
9
10
USERVER_NAMESPACE_BEGIN
11
12
namespace
engine {
13
14
/// @ingroup userver_concurrency
15
///
16
/// @brief TaskLocalVariable is a per-coroutine variable of arbitrary type.
17
///
18
/// It is an alternative to `thread_local`, but per-task instead of per-thread.
19
///
20
/// The order of destruction of task-local variables is inverse to the order of
21
/// initialization.
22
template
<
typename
T>
23
class
TaskLocalVariable final {
24
static_assert
(!std::is_reference_v<T>);
25
static_assert
(!std::is_const_v<T>);
26
27
public
:
28
/// @brief Get the instance of the variable for the current coroutine.
29
/// Initializes (default constructs) the variable if it was not previously
30
/// initialized.
31
/// @note Must be called from a coroutine, otherwise it is UB.
32
T&
operator
*();
33
34
/// @overload
35
T*
operator
->();
36
37
/// @brief Get the variable instance for the current task.
38
/// @returns the variable or `nullptr` if variable was not initialized.
39
T*
GetOptional
()
noexcept
{
40
return
impl::task_local::GetCurrentStorage().GetOptional<T, kVariableKind>(impl_.GetKey());
41
}
39
T*
GetOptional
()
noexcept
{
…
}
42
43
private
:
44
static
constexpr
auto
kVariableKind = impl::task_local::VariableKind::kNormal;
45
46
impl::task_local::Variable impl_;
47
};
23
class
TaskLocalVariable final {
…
};
48
49
template
<
typename
T>
50
T& TaskLocalVariable<T>::
operator
*() {
51
return
impl::task_local::GetCurrentStorage().GetOrEmplace<T, kVariableKind>(impl_.GetKey());
52
}
50
T& TaskLocalVariable<T>::
operator
*() {
…
}
53
54
template
<
typename
T>
55
T* TaskLocalVariable<T>::
operator
->() {
56
return
&(**
this
);
57
}
55
T* TaskLocalVariable<T>::
operator
->() {
…
}
58
59
}
// namespace engine
60
61
USERVER_NAMESPACE_END
userver
engine
task
local_variable.hpp
Generated on Thu Apr 24 2025 12:16:22 for userver by
Doxygen
1.13.2