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>);
31 const T*
GetOptional()
const noexcept {
return Storage().GetOptional<T, kVariableKind>(impl_.GetKey()); }
35 const T&
Get()
const {
return Storage().Get<T, kVariableKind>(impl_.GetKey()); }
38 template <
typename... Args>
40 Storage().Emplace<T, kVariableKind>(impl_.GetKey(), std::forward<Args>(args)...);
44 void Set(T&& value) { Emplace(std::move(value)); }
47 void Set(
const T& value) { Emplace(value); }
52 void Erase() { Storage().Erase<T, kVariableKind>(impl_.GetKey()); }
57 void InheritTo(impl::task_local::Storage& other, impl::task_local::InternalTag) {
58 other.InheritNodeIfExists(Storage(), impl_.GetKey());
63 static constexpr auto kVariableKind = impl::task_local::VariableKind::kInherited;
65 static impl::task_local::Storage& Storage()
noexcept {
return impl::task_local::GetCurrentStorage(); }
67 impl::task_local::Variable impl_;