Function Guard

[中文]

Public header: #include "brookesia/lib_utils/function_guard.hpp"

Overview

FunctionGuard is an RAII guard that runs cleanup at scope exit to simplify resource release and rollback.

Features

  • Automatic callback on scope exit; optional release to cancel

  • Move semantics for transferring guards

  • Catches std::exception during cleanup to avoid breaking teardown

API Reference

Header File

Classes

template<typename T, typename ...Args>
class FunctionGuard

RAII helper that invokes a callable on destruction unless released.

Note

The destructor suppresses boost::thread_interrupted and prints other std::exception failures to stdout, so cleanup code should still be kept lightweight.

Template Parameters
  • T – Callable type stored by the guard.

  • Args – Argument types forwarded to the callable when the guard is destroyed.

Public Functions

inline FunctionGuard(T func, Args&&... args)

Construct a guard for a deferred callable invocation.

Parameters
  • func – Callable executed in the destructor unless release() is called.

  • args – Arguments stored and forwarded to func during destruction.

inline void release()

Disable the deferred invocation.

After calling this method, destroying the guard no longer executes the stored callable.