wwa-scope-action 1.0.0
Scope guard utilities for managing exit actions in C++
|
A scope guard that calls its exit function when a scope is exited via an exception. More...
#include <scope_action.h>
Public Member Functions | |
fail_action (fail_action &&other) noexcept(std::is_nothrow_move_constructible_v< ExitFunc >||std::is_nothrow_copy_constructible_v< ExitFunc >) | |
Move constructor. | |
template<typename Func > requires (detail::can_move_construct_from_noexcept<fail_action, ExitFunc, Func>) | |
fail_action (Func &&fn) noexcept | |
Constructs a new fail_action from an exit function of type Func. | |
template<typename Func > requires (detail::can_construct_from<fail_action, ExitFunc, Func>) | |
fail_action (Func &&fn) noexcept(std::is_nothrow_constructible_v< ExitFunc, Func >||std::is_nothrow_constructible_v< ExitFunc, Func & >) | |
Constructs a new fail_action from an exit function of type Func. | |
~fail_action () noexcept | |
Calls the exit function if the scope is exited via an exception and destroys the object. | |
void | release () noexcept |
Makes the fail_action object inactive. | |
A scope guard that calls its exit function when a scope is exited via an exception.
Like exit_action
, a fail_action
may be active or inactive. A fail_action
is active after construction from an exit function.
An fail_action
becomes inactive by calling release()
or a move constructor. An inactive fail_action
may also be obtained by initializing with another inactive fail_action
. Once an fail_action
is inactive, it cannot become active again.
Usage example:
ExitFunc | Exit function type. Func is either a Destructible FunctionObject type, or an lvalue reference to a FunctionObject or function. |
fail_action
of dynamic storage duration might lead to unexpected behavior. fail_action
from another fail_action
created in a different thread might also lead to unexpected behavior since the count of uncaught exceptions obtained in different threads may be compared during the destruction. Definition at line 246 of file scope_action.h.
|
inlineexplicitnoexcept |
Constructs a new fail_action from an exit function of type Func.
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 fail_action
is active.
If Func
is not an lvalue reference type, and std::is_nothrow_constructible_v<ExitFunc, Func>
is true
, the stored exit function is initialized with std::forward<Func>(fn)
; otherwise it is initialized with fn
. If initialization of the stored exit function throws an exception, calls fn()
.
This overload participates in overload resolution only if:
std::is_same_v<std::remove_cvref_t<Func>, fail_action>
is false
, andstd::is_constructible_v<ExitFunc, Func>
is true
.Func | Exit function type. Must be constructible from ExitFunc. |
fn | Exit function. |
anything | Any exception thrown during the initialization of the stored exit function. |
Definition at line 270 of file scope_action.h.
|
inlineexplicitnoexcept |
Constructs a new fail_action from an exit function of type Func.
Initializes the exit function with a function or function object fn
. The constructed fail_action
is active. The stored exit function is initialized with std::forward<Func>(fn)
.
This overload participates in overload resolution only if:
std::is_same_v<std::remove_cvref_t<Func>, fail_action>
is false
, andstd::is_lvalue_reference_v<Func>
is false
, andstd::is_nothrow_constructible_v<ExitFunc, Func>
is true
.Func | Exit function type. Must be constructible from ExitFunc. |
fn | Exit function. |
Definition at line 303 of file scope_action.h.
|
inlinenoexcept |
Move constructor.
Initializes the stored exit function with the one in other
, and initializes the counter of uncaught exceptions with the one in other
. The constructed fail_action
is active if and only if other
is active before the construction.
If std::is_nothrow_move_constructible_v<ExitFunc>
is true, initializes stored exit function (denoted by exitfun
) with std::forward<ExitFunc>(other.exitfun)
, otherwise initializes it with other.exitfun
.
After successful move construction, other.release()
is called and other
becomes inactive.
This overload participates in overload resolution only if:
std::is_nothrow_move_constructible_v<ExitFunc>
is true
, orstd::is_copy_constructible_v<ExitFunc>
is true
.other | fail_action to move from. |
anything | Any exception thrown during the initialization of the stored exit function. |
Definition at line 326 of file scope_action.h.
|
inlinenoexcept |
Calls the exit function if the scope is exited via an exception and destroys the object.
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 fail_action
is active; then destroys the object.
Definition at line 358 of file scope_action.h.
|
inlinenoexcept |
Makes the fail_action object inactive.
Once an fail_action is inactive, it cannot become active again, and it will not call its exit function upon destruction.
fail_action
's move constructor. Definition at line 374 of file scope_action.h.