14#include <userver/compiler/impl/lifetime.hpp>
15#include <userver/components/component_fwd.hpp>
16#include <userver/utils/assert.hpp>
17#include <userver/utils/impl/internal_tag.hpp>
18#include <userver/utils/move_only_function.hpp>
20USERVER_NAMESPACE_BEGIN
27 virtual ~ScopeBase() =
default;
29 virtual void AfterConstruction() = 0;
32template <
typename Handle>
33class Scope
final :
public ScopeBase {
35 using AfterConstructionCallback =
utils::move_only_function<Handle()>;
37 explicit Scope(AfterConstructionCallback after_construction)
38 : after_construction_(std::move(after_construction))
41 void AfterConstruction()
override { before_destruction_.emplace(after_construction_()); }
44 AfterConstructionCallback after_construction_;
45 std::optional<Handle> before_destruction_;
49class Scope<
void>
final :
public ScopeBase {
51 using AfterConstructionCallback =
utils::move_only_function<
void()>;
53 explicit Scope(AfterConstructionCallback after_construction)
54 : after_construction_(std::move(after_construction))
57 void AfterConstruction()
override { after_construction_(); }
60 AfterConstructionCallback after_construction_;
67using ScopePtr = std::unique_ptr<impl::ScopeBase>;
74struct UnsafeManualResourceScopes
final {
75 constexpr explicit UnsafeManualResourceScopes()
noexcept =
default;
89class ResourceScopeStorage
final {
96 ResourceScopeStorage(ResourceScopeStorage&& other)
noexcept =
default;
97 ResourceScopeStorage& operator=(ResourceScopeStorage&& other)
noexcept =
default;
119 template <
std::invocable<> AfterConstructionCallback>
120 void Register(AfterConstructionCallback after_construction)
122 Register(
utils::impl::InternalTag{}, Priority{0}, std::move(after_construction));
128 using Priority = std::int32_t;
130 template <std::invocable<> AfterConstructionCallback>
131 void Register(
utils::impl::InternalTag, Priority priority, AfterConstructionCallback after_construction)
133 using Handle = std::invoke_result_t<AfterConstructionCallback>;
134 auto scope = std::make_unique<impl::Scope<Handle>>(std::move(after_construction));
135 DoRegister(std::move(scope), priority);
160 struct ScopeWithPriority {
161 Priority priority{0};
162 impl::ScopePtr scope;
165 void DoRegister(impl::ScopePtr resource_scope, Priority priority);
166 void OpenAndKeep(impl::ScopePtr resource_scope);
167 void OpenAndClose(impl::ScopePtr resource_scope);
168 static void SortByPriority(std::vector<ScopeWithPriority>& scopes)
noexcept;
170 std::vector<ScopeWithPriority> registered_scopes_;
171 std::vector<impl::ScopePtr> initialized_scopes_;
172 State state_{State::kConstruction};
180template <
typename Wrapped>
181class WithResourceScopes
final {
185 template <
typename... Args>
187 : wrapped_(resource_scope_storage_, std::forward<Args>(args)...)
193 WithResourceScopes(WithResourceScopes&&) =
delete;
194 WithResourceScopes& operator=(WithResourceScopes&&) =
delete;
199 Wrapped&
operator*() &
noexcept USERVER_IMPL_LIFETIME_BOUND {
return wrapped_; }
201 const Wrapped&
operator*()
const&
noexcept USERVER_IMPL_LIFETIME_BOUND {
return wrapped_; }
204 Wrapped*
operator->()
noexcept USERVER_IMPL_LIFETIME_BOUND {
return &wrapped_; }
206 const Wrapped*
operator->()
const noexcept USERVER_IMPL_LIFETIME_BOUND {
return &wrapped_; }
209 ResourceScopeStorage resource_scope_storage_
{UnsafeManualResourceScopes{}
};
220template <
typename Wrapped,
typename... Args>
222 auto holder = std::make_shared<WithResourceScopes<Wrapped>>(std::in_place, std::forward<Args>(args)...);
223 auto*
const wrapped = std::addressof(**holder);
224 return std::shared_ptr<Wrapped>(std::move(holder), wrapped);
229struct ResourceScopeStorageHolder {
230 ResourceScopeStorage scopes
{UnsafeManualResourceScopes{}
};
233template <
typename Wrapped>
235class UniqueWithResourceScopes
final :
private ResourceScopeStorageHolder,
public Wrapped {
237 template <
typename... Args>
238 explicit UniqueWithResourceScopes(Args&&... args)
239 : Wrapped(ResourceScopeStorageHolder::scopes, std::forward<Args>(args)...)
244 UniqueWithResourceScopes(UniqueWithResourceScopes&&) =
delete;
245 UniqueWithResourceScopes& operator=(UniqueWithResourceScopes&&) =
delete;
247 ~UniqueWithResourceScopes()
override { ResourceScopeStorageHolder::scopes
.BeforeDestruction(); }
264template <
typename Wrapped,
typename... Args>
267 !std::is_final_v<Wrapped>,
268 "utils::MakeUniqueWithResourceScopes<T> requires a non-final T; use utils::WithResourceScopes or "
269 "utils::MakeWithResourceScopes for final types"
272 std::has_virtual_destructor_v<Wrapped>,
273 "utils::MakeUniqueWithResourceScopes<T> requires T to have a virtual destructor for deletion through "
276 if constexpr (!std::is_final_v<Wrapped> && std::has_virtual_destructor_v<Wrapped>) {
277 return std::make_unique<impl::UniqueWithResourceScopes<Wrapped>>(std::forward<Args>(args)...);