Template Class ScopeFail#
Defined in File UniqueResource.h
Class Documentation#
-
template<typename EF>
class ScopeFail# General-purpose scope guard intended to call its exit function when a scope is exited via an exception.
A ScopeFail may be either active, i.e. calls its exit function on destruction, or inactive, i.e. does nothing on destruction. A ScopeFail is active after constructed from an exit function.
A ScopeFail can become inactive by calling Release() on it either manually or automatically (by the move constructor). An inactive ScopeFail may also be obtained by initializing with another inactive ScopeFail. Once a ScopeFail is inactive, it cannot become active again.
A ScopeFail effectively holds an EF and a bool flag indicating if it is active, along with a counter of uncaught exceptions used for detecting whether the destructor is called during stack unwinding.
- Template Parameters:
EF – Type of stored exit function.
Public Functions
-
template<typename EFPtr>
inline explicit constexpr ScopeFail(EFPtr &&f) noexcept(std::is_nothrow_constructible_v<EF, EFPtr&>)# Creates a ScopeFail from a function, a function object or another ScopeFail. Initializes the exit function with a function or function object, and initializes the counter of uncaught exceptions as if with std::uncaught_exceptions(). The constructed ScopeFail is active.
The stored EF is initialized with f. The behavior is undefined if calling f throws an exception or results in undefined behavior, even if f has not been called.
- Parameters:
f – Function or function object used for initializing the stored EF.
-
template<typename EFPtr>
inline explicit constexpr ScopeFail(EFPtr &&f) noexcept Creates a ScopeFail from a function, a function object or another ScopeFail. Initializes the exit function with a function or function object, and initializes the counter of uncaught exceptions as if with std::uncaught_exceptions(). The constructed ScopeFail is active.
The stored EF is initialized with std::forward<EFPtr>(f). The behavior is undefined if calling f throws an exception or results in undefined behavior, even if f has not been called.
- Parameters:
f – Function or function object used for initializing the stored EF.
-
inline constexpr ScopeFail(ScopeFail &&rhs) noexcept#
Creates a ScopeFail from a function, a function object or another ScopeFail. Move constructor. Initializes the stored EF with the one in rhs, and initializes the counter of uncaught exceptions with the one in rhs. The constructed ScopeFail is active if and only if rhs is active before the construction.
Initializes stored EF (denoted by exitFunction) with std::forward<EF>(rhs.exitFunction). After successful move construction, rhs.Release() is called and other becomes inactive.
- Parameters:
rhs – ScopeFail to move from.
-
inline constexpr ScopeFail(ScopeFail &&rhs) noexcept(std::is_nothrow_copy_constructible_v<EF>)
Creates a ScopeFail from a function, a function object or another ScopeFail. Move constructor. Initializes the stored EF with the one in rhs, and initializes the counter of uncaught exceptions with the one in rhs. The constructed ScopeFail is active if and only if rhs is active before the construction.
Initializes stored EF (denoted by exitFunction) with rhs.exitFunction. After successful move construction, rhs.Release() is called and other becomes inactive.
- Parameters:
rhs – ScopeFail to move from.
-
inline ~ScopeFail() noexcept(noexcept(this->m_exitFunction))#
Calls the exit function if the result of std::uncaught_exceptions() is greater than the counter of uncaught exceptions (typically on stack unwinding) and the ScopeFail is active, then destroys the stored EF (if it is a function object) and any other non-static data members.
Note
Whether the destructor is called on stack unwinding can be detected by the comparison of the result of std::uncaught_exceptions() and the counter of uncaught exceptions in the ScopeFail.