Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arene::base::scope_guard< Func > Class Template Reference

RAII utility for executing logic on scope exit. More...

Public Member Functions

template<typename Func2, constraints< std::enable_if_t< std::is_constructible< Func, Func2 >::value >, std::enable_if_t<!std::is_same< remove_cvref_t< Func2 >, scope_guard >::value > > = nullptr>
 scope_guard (Func2 &&supplied_callable) noexcept(std::is_nothrow_constructible< Func, Func2 >::value)
 Construct a new scope_guard that decay-copies the function and ensure that it is called upon destruction of the scope_guard object.
 
 scope_guard (scope_guard &&other) noexcept
 Move constructor.
 
 scope_guard (scope_guard const &)=delete
 copy-construction is deleted as two guards cannot own the same function.
 
 ~scope_guard ()
 Destroy the scope guard and invoke the function if it should be executed.
 
void cancel () noexcept
 Declares the held function should not be invoked.
 
void invoke_now () noexcept
 Invokes the held function if it has not be otherwise canceled.
 
auto operator= (scope_guard &&other) -> scope_guard &=delete
 Move assignment is deleted as allowing it would overwrite the existing guard function without executing it.
 
auto operator= (scope_guard const &) -> scope_guard &=delete
 copy-assignment is deleted as two guards cannot own the same function.
 

Detailed Description

template<typename Func>
class arene::base::scope_guard< Func >

RAII utility for executing logic on scope exit.

The scope_guard type is a utility that helps writing exception-safe code by ensuring that some code gets run on scope-exit, regardless of how the scope is exited (return, exception, flowing off end, etc). The destructor of the scope_guard class invokes the function passed to the constructor.

See also
arene::base::on_scope_exit for a helper function for creating scope_guard instances.
Template Parameters
FuncThe functor to be called on scope exit. Must have signature void(void) noexcept . It also must be nothrow move constructable for scope_guard to be move-constructable.

Constructor & Destructor Documentation

◆ scope_guard() [1/3]

template<typename Func>
template<typename Func2, constraints< std::enable_if_t< std::is_constructible< Func, Func2 >::value >, std::enable_if_t<!std::is_same< remove_cvref_t< Func2 >, scope_guard >::value > > = nullptr>
arene::base::scope_guard< Func >::scope_guard ( Func2 && supplied_callable)
inlineexplicitnoexcept

Construct a new scope_guard that decay-copies the function and ensure that it is called upon destruction of the scope_guard object.

Template Parameters
Func2The type of the supplied callable
Parameters
supplied_callableThe supplied callable

◆ scope_guard() [2/3]

template<typename Func>
arene::base::scope_guard< Func >::scope_guard ( scope_guard< Func > && other)
inlinenoexcept

Move constructor.

Moves ownership of the function and responsibility for calling the function on destruction to the newly constructed scope_guard object.

Parameters
otherThe scope_guard to move from.
Postcondition
The held function will be the one from other
The cancel state will be the one from other
The moved-from scope_guard can be safely destructed.

◆ scope_guard() [3/3]

template<typename Func>
arene::base::scope_guard< Func >::scope_guard ( scope_guard< Func > const & )
delete

copy-construction is deleted as two guards cannot own the same function.

◆ ~scope_guard()

template<typename Func>
arene::base::scope_guard< Func >::~scope_guard ( )
inline

Destroy the scope guard and invoke the function if it should be executed.

Postcondition
The held function is executed as if via invoke_now() .

Member Function Documentation

◆ cancel()

template<typename Func>
void arene::base::scope_guard< Func >::cancel ( )
inlinenoexcept

Declares the held function should not be invoked.

Postcondition
Calls to invoke_now() will be noops.

◆ invoke_now()

template<typename Func>
void arene::base::scope_guard< Func >::invoke_now ( )
inlinenoexcept

Invokes the held function if it has not be otherwise canceled.

Postcondition
If cancel() has not been called, invokes the held functor.
Subsequent calls to invoke_now() are noops as if cancel() had been called.

◆ operator=() [1/2]

template<typename Func>
auto arene::base::scope_guard< Func >::operator= ( scope_guard< Func > && other) -> scope_guard &=delete
delete

Move assignment is deleted as allowing it would overwrite the existing guard function without executing it.

◆ operator=() [2/2]

template<typename Func>
auto arene::base::scope_guard< Func >::operator= ( scope_guard< Func > const & ) -> scope_guard &=delete
delete

copy-assignment is deleted as two guards cannot own the same function.


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