-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope_guard.hxx
More file actions
114 lines (101 loc) · 3.82 KB
/
scope_guard.hxx
File metadata and controls
114 lines (101 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
/**
* @file scope_guard.hxx
* @brief RAII scope-guard that executes a callable on scope exit.
* @version 1.0.0
*
* @details
* Provides `ScopeGuard<F>` and the `ScopeStrategy` enum to control when
* the guarded action runs:
* - `EXIT` — always, regardless of exceptions (default)
* - `FAIL` — only when the scope exits via an exception
* - `SUCCESS` — only when the scope exits normally
*
* Exception detection is implemented with `std::uncaught_exceptions()`:
* on destruction the guard compares the current count against the count
* captured at construction, so nested exception scenarios are handled
* correctly.
*
* Three convenience factory functions wrap the most common cases:
* `on_scope_exit(f)`, `on_scope_fail(f)`, `on_scope_success(f)`.
*
* The guard can be dismissed early via `dismiss()`, which prevents
* the callable from running. Copy and move are both deleted.
*
* @author Matteo Zanella <matteozanella2@gmail.com>
* Copyright 2026 Matteo Zanella
*
* Repository: https://github.com/Zanzibarr/cpp_utils
*
* SPDX-License-Identifier: MIT
*/
#include <exception>
#include <iostream>
#include <utility>
/** Controls when the guarded callable is executed on scope exit. */
enum class ScopeStrategy {
EXIT, // Always run
FAIL, // Run only if an exception is thrown
SUCCESS // Run only if no exception is thrown
};
/**
* RAII guard that invokes a callable when the enclosing scope exits.
*
* The execution condition is governed by `ScopeStrategy` and detected via
* `std::uncaught_exceptions()` captured at construction time.
*
* @tparam F Callable type (function, lambda, or functor). Must be movable.
*/
template <typename F>
class ScopeGuard {
public:
/**
* @param func Callable to invoke on scope exit.
* @param strategy When to invoke `func` (default: always).
* ATTENTION: If the function throws, std:terminate is called
*/
ScopeGuard(F&& func, ScopeStrategy strategy = ScopeStrategy::EXIT)
: strategy_(strategy), func_(std::move(func)), initial_exceptions_(std::uncaught_exceptions()), active_(true) {}
~ScopeGuard() noexcept {
if (!active_) {
return;
}
int current_exceptions = std::uncaught_exceptions();
bool failed = current_exceptions > initial_exceptions_;
if (strategy_ == ScopeStrategy::EXIT || (strategy_ == ScopeStrategy::FAIL && failed) || (strategy_ == ScopeStrategy::SUCCESS && !failed)) {
try {
func_();
} catch (...) {
std::cerr << "ScopeGuard: callable threw, terminating\n";
std::terminate();
}
}
}
// Boilerplate: Disable copy and move
ScopeGuard(const ScopeGuard&) = delete;
ScopeGuard(ScopeGuard&&) = delete;
auto operator=(const ScopeGuard&) -> ScopeGuard& = delete;
auto operator=(ScopeGuard&&) -> ScopeGuard& = delete;
/** Cancels the guard; the callable will not run on destruction. */
void dismiss() noexcept { active_ = false; }
private:
ScopeStrategy strategy_;
F func_;
int initial_exceptions_;
bool active_{};
};
/** Returns a `ScopeGuard` that always calls `func` on scope exit. */
template <typename F>
[[nodiscard]] auto on_scope_exit(F&& func) {
return ScopeGuard<std::decay_t<F>>(std::forward<F>(func), ScopeStrategy::EXIT);
}
/** Returns a `ScopeGuard` that calls `func` only if the scope exits normally. */
template <typename F>
[[nodiscard]] auto on_scope_success(F&& func) {
return ScopeGuard<std::decay_t<F>>(std::forward<F>(func), ScopeStrategy::SUCCESS);
}
/** Returns a `ScopeGuard` that calls `func` only if the scope exits via an exception. */
template <typename F>
[[nodiscard]] auto on_scope_fail(F&& func) {
return ScopeGuard<std::decay_t<F>>(std::forward<F>(func), ScopeStrategy::FAIL);
}