函数守卫

[English]

公共头文件: #include "brookesia/lib_utils/function_guard.hpp"

概述

FunctionGuard 提供 RAII 风格的函数守卫,用于在作用域结束时自动执行清理逻辑, 降低资源释放与流程回滚的复杂度。

特性

  • 作用域退出自动执行回调,可显式 release 取消

  • 支持移动语义,便于在函数间转移守卫

  • 执行回调时捕获 std::exception,避免异常传播破坏清理流程

API 参考

Header File

Classes

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

RAII helper that invokes a callable on destruction unless released.

备注

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

模板参数
  • 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.

参数
  • 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.