16#define DEFER_CONCAT_(a, b) a##b
17#define DEFER_CONCAT(a, b) DEFER_CONCAT_(a, b)
19#define DEFER_CALL(fn) _ScopeGuard DEFER_CONCAT(__defer__, __LINE__) = [&]() { fn; };
24 template<
class Callable>
25 _ScopeGuard(Callable &&fn) : fn_(std::forward<Callable>(fn))
29 _ScopeGuard(_ScopeGuard &&other) : fn_(std::move(other.fn_)) { other.fn_ =
nullptr; }
37 _ScopeGuard(
const _ScopeGuard &) =
delete;
38 void operator=(
const _ScopeGuard &) =
delete;
41 std::function<void()> fn_;
44#define IF_REENTERED(fn) \
45 static int DEFER_CONCAT(__reenter_guard_depth__, __LINE__) = 0; \
46 _ReentrancyGuard DEFER_CONCAT(__reenter_guard__, __LINE__)(DEFER_CONCAT(__reenter_guard_depth__, __LINE__), [&]() { fn; });
51 template<
class Callable>
52 _ReentrancyGuard(
int &entered, Callable &&fn) : depth(entered)
58 _ReentrancyGuard(_ReentrancyGuard &&other) : depth(other.depth) {}
60 ~_ReentrancyGuard() { depth--; }
62 _ReentrancyGuard(
const _ReentrancyGuard &) =
delete;
63 void operator=(
const _ReentrancyGuard &) =
delete;