#include <userver/utils/resource_scopes.hpp>
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.
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. | |
|
inlineexplicitnoexcept |
Constructs an empty storage.
Prefer a scope wrapper, including in tests; see choosing scopes.
Definition at line 94 of file resource_scopes.hpp.
| void utils::ResourceScopeStorage::AfterConstruction | ( | ) |
Call all registered functors.
If a functor throws, already constructed resources are unregistered via BeforeDestruction and the exception is rethrown.
|
noexcept |
Unregister all previously registered resources.
Also drops factories that have not run AfterConstruction yet, so captured RAII handles unregister immediately.
|
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.
See where to obtain this storage and using Register directly for custom resource lifetimes.
Definition at line 120 of file resource_scopes.hpp.