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()); }
60 static constexpr auto kVariableKind =
61 impl::task_local::VariableKind::kInherited;
63 static impl::task_local::Storage& Storage()
noexcept {
64 return impl::task_local::GetCurrentStorage();
67 impl::task_local::Variable impl_;