MySQL 9.1.0
Source Code Documentation
Scope_guard< TLambda > Class Template Reference

A Lambda to be called at scope exit. More...

#include <scope_guard.h>

Public Member Functions

 Scope_guard (const TLambda &cleanup_lambda)
 
 Scope_guard (const Scope_guard< TLambda > &)=delete
 
 Scope_guard (Scope_guard< TLambda > &&moved)
 
 ~Scope_guard ()
 
void release ()
 Releases the scope guard. More...
 
void reset ()
 Calls the cleanup lambda and releases the scope guard. More...
 

Private Attributes

bool m_is_released
 If true the cleanup is not going to be called. More...
 
const TLambda m_cleanup_lambda
 The cleanup to be called. More...
 

Detailed Description

template<typename TLambda>
class Scope_guard< TLambda >

A Lambda to be called at scope exit.

Used as std::scope_exit of sorts. Useful if you can't use unique_ptr to install a specific deleter but still want to do automatic cleanup at scope exit.

Note
Always use create_scope_guard() instead of this template directly!

Typical use is:

...
foo_init();
auto cleanup_foo = create_scope_guard([&] {
foo_deinit();
}
...
if (some_error)
return; // cleanup_foo calls foo_deinit()
...
// foo_deinit is not going to be called past this point
cleanup_foo.release();
return; // return with foo initialized.
...
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Create a scope guard object.
Definition: scope_guard.h:113

Constructor & Destructor Documentation

◆ Scope_guard() [1/3]

template<typename TLambda >
Scope_guard< TLambda >::Scope_guard ( const TLambda &  cleanup_lambda)
inline

◆ Scope_guard() [2/3]

template<typename TLambda >
Scope_guard< TLambda >::Scope_guard ( const Scope_guard< TLambda > &  )
delete

◆ Scope_guard() [3/3]

template<typename TLambda >
Scope_guard< TLambda >::Scope_guard ( Scope_guard< TLambda > &&  moved)
inline

◆ ~Scope_guard()

template<typename TLambda >
Scope_guard< TLambda >::~Scope_guard ( )
inline

Member Function Documentation

◆ release()

template<typename TLambda >
void Scope_guard< TLambda >::release ( )
inline

Releases the scope guard.

Makes sure that when scope guard goes out of scope the cleanup lambda is not going to be called.

◆ reset()

template<typename TLambda >
void Scope_guard< TLambda >::reset ( )
inline

Calls the cleanup lambda and releases the scope guard.

Useful if you want to explicitly provoke the cleanup earlier than when going out of scope.

Member Data Documentation

◆ m_cleanup_lambda

template<typename TLambda >
const TLambda Scope_guard< TLambda >::m_cleanup_lambda
private

The cleanup to be called.

◆ m_is_released

template<typename TLambda >
bool Scope_guard< TLambda >::m_is_released
private

If true the cleanup is not going to be called.


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