10#include <userver/components/component_fwd.hpp>
11#include <userver/utils/assert.hpp>
12#include <userver/utils/move_only_function.hpp>
14USERVER_NAMESPACE_BEGIN
21 virtual ~ScopeBase() =
default;
23 virtual void AfterConstruction() = 0;
26template <
typename Handle>
27class Scope
final :
public ScopeBase {
29 using AfterConstructionCallback =
utils::move_only_function<Handle()>;
31 explicit Scope(AfterConstructionCallback after_construction)
32 : after_construction_(std::move(after_construction))
35 void AfterConstruction()
override { before_destruction_.emplace(after_construction_()); }
38 AfterConstructionCallback after_construction_;
39 std::optional<Handle> before_destruction_;
43class Scope<
void>
final :
public ScopeBase {
45 using AfterConstructionCallback =
utils::move_only_function<
void()>;
47 explicit Scope(AfterConstructionCallback after_construction)
48 : after_construction_(std::move(after_construction))
51 void AfterConstruction()
override { after_construction_(); }
54 AfterConstructionCallback after_construction_;
61using ScopePtr = std::unique_ptr<impl::ScopeBase>;
68class ResourceScopeStorage
final {
82 template <
typename AfterConstructionCallback>
83 void Register(AfterConstructionCallback after_construction)
85 using Handle = std::invoke_result_t<AfterConstructionCallback>;
86 auto scope = std::make_unique<impl::Scope<Handle>>(std::move(after_construction));
87 DoRegister(std::move(scope));
97 void DoRegister(impl::ScopePtr resource_scope);
99 std::vector<impl::ScopePtr> registered_scopes_;
100 std::vector<impl::ScopePtr> initialized_scopes_;
101 bool scope_registration_finished_{
false};