userver: userver/engine/task/inherited_variable.hpp Source File
Loading...
Searching...
No Matches
inherited_variable.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <type_traits>
7#include <utility>
8
9#include <userver/engine/impl/task_local_storage.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace engine {
15
27template <typename T>
29 static_assert(!std::is_reference_v<T>);
30 static_assert(!std::is_const_v<T>);
31
32public:
37
40 : impl_(priority)
41 {}
42
45 const T* GetOptional() const noexcept { return Storage().GetOptional<T, kVariableKind>(impl_.GetKey()); }
46
49 const T& Get() const { return Storage().Get<T, kVariableKind>(impl_.GetKey()); }
50
52 template <typename... Args>
53 void Emplace(Args&&... args) {
54 Storage().Emplace<T, kVariableKind>(impl_.GetKey(), std::forward<Args>(args)...);
55 }
56
58 void Set(T&& value) { Emplace(std::move(value)); }
59
61 void Set(const T& value) { Emplace(value); }
62
66 void Erase() { Storage().Erase<T, kVariableKind>(impl_.GetKey()); }
67
68private:
69 static constexpr auto kVariableKind = impl::task_local::VariableKind::kInherited;
70
71 static impl::task_local::Storage& Storage() noexcept { return impl::task_local::GetCurrentStorage(); }
72
73 impl::task_local::Variable impl_;
74};
75
76} // namespace engine
77
78USERVER_NAMESPACE_END