dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Utils::ExecOnOOS< F > Struct Template Reference

Scope guard that executes a callable when it goes out of scope. More...

#include <dfx-utilities/ExecOnOOS.hpp>

Public Member Functions

 ExecOnOOS (bool exec, F &&f) noexcept
 Construct a scope guard.
 ExecOnOOS (F &&f) noexcept
 Construct an enabled scope guard.
 ~ExecOnOOS () noexcept(std::is_nothrow_invocable_v< F >)
 Destructor. Executes the stored callable if not cancelled.
void cancel () noexcept
 Prevent execution on scope exit.
bool willExec () const noexcept
 Return whether the callable will currently execute on scope exit.

Public Attributes

cb
 Stored callable executed on destruction if shouldExec is true.
bool shouldExec
 Whether the destructor should execute cb.

Detailed Description

template<typename F>
struct dfx::Utils::ExecOnOOS< F >

Scope guard that executes a callable when it goes out of scope.

ExecOnOOS ("execute on out-of-scope") is a tiny RAII helper: it stores a callable and calls it from the destructor if it has not been cancelled.

Typical use-cases:

  • rollback logic in the presence of early returns
  • temporary state changes (restore previous value on scope exit)
  • "commit/rollback" patterns (call cancel() once the operation is committed)
Template Parameters
FCallable type. Must be invocable with no arguments: cb().
Note
This is intentionally minimal:
  • It does not perform allocation.
  • It does not attempt to be thread-safe.
  • It does not guard against exceptions thrown by the callable; if cb() can throw, the destructor may potentially call std::terminate if an exception escapes.

The destructor's noexcept specification is conditional on whether F is nothrow-invocable.

Example
void writeFile(...)
{
bool opened = openSomething();
ExecOnOOS rollback([&]
{
// Executed unless cancelled:
if (opened) closeSomething();
});
// ... do work ...
rollback.cancel(); // commit: do not run rollback on scope exit
}
Scope guard that executes a callable when it goes out of scope.
Definition ExecOnOOS.hpp:59
ExecOnOOS(bool exec, F &&f) noexcept
Construct a scope guard.
Definition ExecOnOOS.hpp:66

Constructor & Destructor Documentation

◆ ExecOnOOS() [1/2]

template<typename F>
dfx::Utils::ExecOnOOS< F >::ExecOnOOS ( bool exec,
F && f )
inlinenoexcept

Construct a scope guard.

Parameters
execIf true, the callable will be executed on scope exit.
fCallable to store.
Note
The callable is stored by value in cb and is move-constructed.

◆ ExecOnOOS() [2/2]

template<typename F>
dfx::Utils::ExecOnOOS< F >::ExecOnOOS ( F && f)
inlinenoexcept

Construct an enabled scope guard.

Equivalent to ExecOnOOS(true, std::move(f)).

Parameters
fCallable to store.

◆ ~ExecOnOOS()

template<typename F>
dfx::Utils::ExecOnOOS< F >::~ExecOnOOS ( )
inlinenoexcept

Destructor. Executes the stored callable if not cancelled.

Calls cb() only if shouldExec is true at destruction time.

Warning
If cb() throws and the exception escapes, the program may terminate (especially if another exception is already active). Prefer making the callable noexcept or catching internally.

Member Function Documentation

◆ cancel()

template<typename F>
void dfx::Utils::ExecOnOOS< F >::cancel ( )
inlinenoexcept

Prevent execution on scope exit.

After calling this, the destructor will not invoke the callable.

◆ willExec()

template<typename F>
bool dfx::Utils::ExecOnOOS< F >::willExec ( ) const
inlinenoexcept

Return whether the callable will currently execute on scope exit.

Member Data Documentation

◆ cb

template<typename F>
F dfx::Utils::ExecOnOOS< F >::cb

Stored callable executed on destruction if shouldExec is true.

◆ shouldExec

template<typename F>
bool dfx::Utils::ExecOnOOS< F >::shouldExec

Whether the destructor should execute cb.


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