OpenVPN 3 Core Library
Loading...
Searching...
No Matches
openvpn::CleanupType< F > Class Template Reference

A scope guard that runs a callable on destruction unless dismissed or released. More...

#include <cleanup.hpp>

Public Member Functions

 CleanupType (F method) noexcept(std::is_nothrow_move_constructible_v< F >)
 
 CleanupType (const CleanupType &)=delete
 
CleanupTypeoperator= (const CleanupType &)=delete
 
 CleanupType (CleanupType &&other) noexcept
 
CleanupTypeoperator= (CleanupType &&other) noexcept
 
 ~CleanupType () noexcept
 Destructor that executes the cleanup action if not dismissed or released.
 
void dismiss () noexcept
 Dismiss the cleanup action, preventing it from being executed on destruction.
 
std::optional< std::function< void()> > release () noexcept
 Release the cleanup action, returning the callable and preventing it from being executed on destruction.
 

Private Attributes

std::optional< F > clean
 

Detailed Description

template<typename F>
requires std::invocable<F>
class openvpn::CleanupType< F >

A scope guard that runs a callable on destruction unless dismissed or released.

Template Parameters
FThe type of the callable to run on destruction.

The Cleanup class template provides a mechanism to ensure that a specified callable is executed when the Cleanup object goes out of scope, unless it has been dismissed or released. This is useful for resource management and cleanup tasks that need to be performed when exiting a scope.

Todo:
Keep std::scope_exit in mind when moving to C++23.

Definition at line 35 of file cleanup.hpp.

Constructor & Destructor Documentation

◆ CleanupType() [1/3]

template<typename F >
openvpn::CleanupType< F >::CleanupType ( method)
inlineexplicitnoexcept

Definition at line 38 of file cleanup.hpp.

◆ CleanupType() [2/3]

template<typename F >
openvpn::CleanupType< F >::CleanupType ( const CleanupType< F > &  )
delete

◆ CleanupType() [3/3]

template<typename F >
openvpn::CleanupType< F >::CleanupType ( CleanupType< F > &&  other)
inlinenoexcept

Definition at line 44 of file cleanup.hpp.

◆ ~CleanupType()

template<typename F >
openvpn::CleanupType< F >::~CleanupType ( )
inlinenoexcept

Destructor that executes the cleanup action if not dismissed or released.

When the CleanupType object is destroyed, if the cleanup action has not been dismissed or released, it will be invoked. Any exceptions thrown by the cleanup action are caught and swallowed to prevent exceptions from escaping the destructor.

Definition at line 64 of file cleanup.hpp.

Member Function Documentation

◆ dismiss()

template<typename F >
void openvpn::CleanupType< F >::dismiss ( )
inlinenoexcept

Dismiss the cleanup action, preventing it from being executed on destruction.

After calling dismiss, the cleanup action will not be invoked when the CleanupType object is destroyed.

Definition at line 85 of file cleanup.hpp.

◆ operator=() [1/2]

template<typename F >
CleanupType & openvpn::CleanupType< F >::operator= ( CleanupType< F > &&  other)
inlinenoexcept

Definition at line 48 of file cleanup.hpp.

◆ operator=() [2/2]

template<typename F >
CleanupType & openvpn::CleanupType< F >::operator= ( const CleanupType< F > &  )
delete

◆ release()

template<typename F >
std::optional< std::function< void()> > openvpn::CleanupType< F >::release ( )
inlinenoexcept

Release the cleanup action, returning the callable and preventing it from being executed on destruction.

Returns
An optional containing the callable if it was set, or std::nullopt if it was dismissed or already released.

After calling release, the cleanup action will not be invoked when the CleanupType object is destroyed, and the caller takes ownership of the callable. This allows the caller to manage the callable's lifetime independently, including calling it earlier or later than it might be otherwise called.

Definition at line 99 of file cleanup.hpp.

Member Data Documentation

◆ clean

template<typename F >
std::optional<F> openvpn::CleanupType< F >::clean
private

Definition at line 111 of file cleanup.hpp.


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