summaryrefslogtreecommitdiff
path: root/include/libHX/scope.hpp
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2025-03-27 13:19:31 +0100
committerJörg Frings-Fürst <debian@jff-webhosting.net>2025-03-27 13:19:31 +0100
commita81a93beca9a077b4254488ba5617b44fca8d0c1 (patch)
tree4eb17ff1b432f2768ab8c88c267c4984b776380e /include/libHX/scope.hpp
parent1c4b96855c415c48c0b3281b801c2e89fb935061 (diff)
parentfaa9f121de6d65800468d87d8eca0e3ff06b4ae0 (diff)
Merge branch 'release/debian/4.26-1'HEADdebian/4.26-1master
Diffstat (limited to 'include/libHX/scope.hpp')
-rw-r--r--include/libHX/scope.hpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/include/libHX/scope.hpp b/include/libHX/scope.hpp
new file mode 100644
index 0000000..0c0c70d
--- /dev/null
+++ b/include/libHX/scope.hpp
@@ -0,0 +1,35 @@
+#pragma once
+#include <exception>
+#include <utility>
+
+namespace HX {
+
+/*
+ * Modeled upon the C++ standards proposal P0052r10 / Library Fundamentals v3.
+ * Not yet present in GNU stdlibc++ or clang libc++.
+ */
+template<typename F> class scope_exit {
+ private:
+ F m_func;
+ bool m_eod = false;
+
+ public:
+ explicit scope_exit(F &&f) : m_func(std::move(f)), m_eod(true) {}
+ scope_exit(scope_exit &&o) : m_func(std::move(o.m_func)), m_eod(o.m_eod) {
+ o.m_eod = false;
+ }
+ ~scope_exit() try {
+ if (m_eod)
+ m_func();
+ } catch (...) {
+ }
+ void operator=(scope_exit &&) = delete;
+ void release() noexcept { m_eod = false; }
+};
+
+template<typename F> scope_exit<F> make_scope_exit(F &&f)
+{
+ return scope_exit<F>(std::move(f));
+}
+
+} /* namespace */