aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/Guard.h
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-06-01 17:09:44 +0800
committercrupest <crupest@outlook.com>2024-06-08 17:01:55 +0800
commitd1f409530db6f9b712fd672c4c3154cac7eebad1 (patch)
tree1e48a3e2ff14e66aac2503a74df28cca7d85d02c /include/cru/common/Guard.h
parentad796a167e33b54c7fa23ea21c73d57dba4fc928 (diff)
downloadcru-d1f409530db6f9b712fd672c4c3154cac7eebad1.tar.gz
cru-d1f409530db6f9b712fd672c4c3154cac7eebad1.tar.bz2
cru-d1f409530db6f9b712fd672c4c3154cac7eebad1.zip
HALF WORK: refactor something and implement part of subprocess.
Diffstat (limited to 'include/cru/common/Guard.h')
-rw-r--r--include/cru/common/Guard.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/include/cru/common/Guard.h b/include/cru/common/Guard.h
new file mode 100644
index 00000000..5a9f9c5d
--- /dev/null
+++ b/include/cru/common/Guard.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <functional>
+
+namespace cru {
+struct Guard {
+ using ExitFunc = std::function<void()>;
+
+ Guard() = default;
+ explicit Guard(const ExitFunc& f) : on_exit(f) {}
+ explicit Guard(ExitFunc&& f) : on_exit(std::move(f)) {}
+ Guard(const Guard&) = delete;
+ Guard(Guard&&) = default;
+ Guard& operator=(const Guard&) = delete;
+ Guard& operator=(Guard&&) = default;
+ ~Guard() {
+ if (on_exit) {
+ on_exit();
+ }
+ }
+
+ void Drop() { on_exit = {}; }
+
+ ExitFunc on_exit;
+};
+} // namespace cru