Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Deferred< F > Struct Template Reference

Executes a function at end of current scope (in the spirit of Zig defer keyword). More...

#include <Deferred.h>

Public Member Functions

 Deferred (F &&f)
 Constructs Deferred object with a functor F. More...
 
 ~Deferred ()
 Invokes the function F upon destruction, if disarm() has not been previously called. More...
 
void disarm ()
 Disarms the Deferred object, preventing function invocation on destruction. More...
 

Detailed Description

template<typename F>
struct SC::Deferred< F >

Executes a function at end of current scope (in the spirit of Zig defer keyword).

Template Parameters
FThe lambda / function to execute

Example:

HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_DUP_HANDLE, FALSE, processId);
if (processHandle == nullptr)
{
return false;
}
auto deferDeleteProcessHandle = SC::MakeDeferred(
[&] // Function (or lambda) that will be invoked at the end of scope
{
CloseHandle(processHandle);
processHandle = nullptr;
});
// Use processHandle that will be disposed at end of scope by the Deferred
// ...
// Deferred can be disarmed, meaning that the dispose function will not be executed
deferDeleteProcessHandle.disarm()
Deferred< F > MakeDeferred(F &&f)
Creates a Deferred object holding a function that will be invoked at end of current scope.
Definition: Deferred.h:61

Constructor & Destructor Documentation

◆ Deferred()

template<typename F >
SC::Deferred< F >::Deferred ( F &&  f)
inline

Constructs Deferred object with a functor F.

◆ ~Deferred()

template<typename F >
SC::Deferred< F >::~Deferred ( )
inline

Invokes the function F upon destruction, if disarm() has not been previously called.

Member Function Documentation

◆ disarm()

template<typename F >
void SC::Deferred< F >::disarm ( )
inline

Disarms the Deferred object, preventing function invocation on destruction.


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