userver: utils::ResourceScopeStorage Class Reference
Loading...
Searching...
No Matches
utils::ResourceScopeStorage Class Referencefinal

#include <userver/utils/resource_scopes.hpp>

Detailed Description

Runs resource setup after object construction and cleanup before object destruction.

During construction, use utils::ResourceScopeStorage::Register to queue setup functions. The component system or scope wrapper calls utils::ResourceScopeStorage::AfterConstruction after all constructors, including derived constructors, finish. It calls utils::ResourceScopeStorage::BeforeDestruction to destroy the returned RAII handles before the object's destructor starts.

See choosing scopes for ownership and wrapper selection, and using Register directly for custom resources.

static std::vector<int> trace;
// Reset static variables for --gtest_repeat.
trace = {};
class ComponentWithResource final : public components::ComponentBase {
public:
ComponentWithResource(const components::ComponentConfig& config, const components::ComponentContext& context)
: components::ComponentBase(config, context)
{
trace.push_back(0);
context.Scopes().Register([] {
trace.push_back(1);
return utils::FastScopeGuard([]() noexcept { trace.push_back(2); });
});
context.Scopes().Register([] {
trace.push_back(3);
return utils::FastScopeGuard([]() noexcept { trace.push_back(4); });
});
}
};
auto component_list = components::MinimalComponentList().Append<ComponentWithResource>("component");
components::InMemoryConfig{tests::MergeYaml(tests::kMinimalStaticConfig, kConfig)},
component_list
);
EXPECT_THAT(trace, ::testing::ElementsAre(0, 1, 3, 4, 2));

Definition at line 89 of file resource_scopes.hpp.

Public Member Functions

 ResourceScopeStorage (UnsafeManualResourceScopes) noexcept
 Constructs an empty storage.
 ResourceScopeStorage (ResourceScopeStorage &&other) noexcept=default
ResourceScopeStorage & operator= (ResourceScopeStorage &&other) noexcept=default
template<std::invocable<> AfterConstructionCallback>
void Register (AfterConstructionCallback after_construction)
 Registers a functor to register some resource that will be called after the component is successfully created (including all class descendants) or after the component creation is emulated in unit tests. The functor must return a RAII-style handle object that unregisters the previously registered resource. The returned handle's destructor is called just before the component destructor is called.
void AfterConstruction ()
 Call all registered functors.
void BeforeDestruction () noexcept
 Unregister all previously registered resources.

Constructor & Destructor Documentation

◆ ResourceScopeStorage()

utils::ResourceScopeStorage::ResourceScopeStorage ( UnsafeManualResourceScopes )
inlineexplicitnoexcept

Constructs an empty storage.

Prefer a scope wrapper, including in tests; see choosing scopes.

Definition at line 94 of file resource_scopes.hpp.

Member Function Documentation

◆ AfterConstruction()

void utils::ResourceScopeStorage::AfterConstruction ( )

Call all registered functors.

If a functor throws, already constructed resources are unregistered via BeforeDestruction and the exception is rethrown.

◆ BeforeDestruction()

void utils::ResourceScopeStorage::BeforeDestruction ( )
noexcept

Unregister all previously registered resources.

Also drops factories that have not run AfterConstruction yet, so captured RAII handles unregister immediately.

◆ Register()

template<std::invocable<> AfterConstructionCallback>
void utils::ResourceScopeStorage::Register ( AfterConstructionCallback after_construction)
inline

Registers a functor to register some resource that will be called after the component is successfully created (including all class descendants) or after the component creation is emulated in unit tests. The functor must return a RAII-style handle object that unregisters the previously registered resource. The returned handle's destructor is called just before the component destructor is called.

During construction the callback is queued and runs from AfterConstruction. If Register is called from another scope's opening callback, or after AfterConstruction has completed, the new scope is opened immediately. Its destructor runs in reverse opening-completion order.

Note
A queued callback is not called if the component is not created OR any previously registered callback throws an exception.
if you don't have an existing RAII-ish class, but still want to do a cleanup, you might want to use utils::FastScopeGuard to wrap the cleanup function.

See where to obtain this storage and using Register directly for custom resource lifetimes.

Definition at line 120 of file resource_scopes.hpp.


The documentation for this class was generated from the following file: