9#include <userver/engine/impl/task_local_storage.hpp>
11USERVER_NAMESPACE_BEGIN
24class TaskInheritedVariable final {
25 static_assert(!std::is_reference_v<T>);
26 static_assert(!std::is_const_v<T>);
32 return Storage().GetOptional<T, kVariableKind>(impl_.GetKey());
37 const T&
Get()
const {
38 return Storage().Get<T, kVariableKind>(impl_.GetKey());
42 template <
typename... Args>
44 Storage().Emplace<T, kVariableKind>(impl_.GetKey(),
45 std::forward<Args>(args)...);
49 void Set(T&& value) { Emplace(std::move(value)); }
52 void Set(
const T& value) { Emplace(value); }
57 void Erase() { Storage().Erase<T, kVariableKind>(impl_.GetKey()); }
62 void InheritTo(impl::task_local::Storage& other,
63 impl::task_local::InternalTag) {
64 other.InheritNodeIfExists(Storage(), impl_.GetKey());
69 static constexpr auto kVariableKind =
70 impl::task_local::VariableKind::kInherited;
72 static impl::task_local::Storage& Storage()
noexcept {
73 return impl::task_local::GetCurrentStorage();
76 impl::task_local::Variable impl_;