[pkg-d-commits] [ldc] 10/149: SCOPE_EXIT
Matthias Klumpp
mak at moszumanska.debian.org
Sun Apr 23 22:36:53 UTC 2017
This is an automated email from the git hooks/post-receive script.
mak pushed a commit to annotated tag v1.2.0
in repository ldc.
commit e0685f8485f761e38c9c2f4a88675cd46c7cb3fb
Author: Ivan <ivan.butygin at gmail.com>
Date: Sat Dec 10 19:49:50 2016 +0300
SCOPE_EXIT
---
gen/helpers.h | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
diff --git a/gen/helpers.h b/gen/helpers.h
new file mode 100644
index 0000000..bae624a
--- /dev/null
+++ b/gen/helpers.h
@@ -0,0 +1,84 @@
+//===-- gen/helpers.h - General helpers -------------------------*- C++ -*-===//
+//
+// LDC - the LLVM D compiler
+//
+// This file is distributed under the BSD-style LDC license. See the LICENSE
+// file for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// General helper constructs.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef HELPERS_H
+#define HELPERS_H
+
+#include <utility>
+#include <type_traits>
+
+namespace details {
+
+struct Ownership {
+ bool flag = false;
+
+ Ownership() = default;
+ Ownership(bool f): flag(f) {}
+
+ Ownership(const Ownership&) = delete;
+ Ownership& operator=(const Ownership&) = delete;
+
+ Ownership(Ownership&& rhs):
+ flag(rhs.flag) {
+ rhs.flag = false;
+ }
+
+ Ownership& operator=(Ownership&& rhs) {
+ flag = rhs.flag;
+ rhs.flag = false;
+ return *this;
+ }
+
+ operator bool() const {
+ return flag;
+ }
+};
+
+template<typename Func>
+struct ScopeExit {
+ Func func;
+ Ownership active = false;
+
+ ScopeExit(Func&& f):
+ func(std::move(f)),
+ active(true) {}
+
+ ~ScopeExit() {
+ if (active) {
+ func();
+ }
+ }
+
+ ScopeExit(const ScopeExit<Func>&) = delete;
+ ScopeExit<Func>& operator=(const ScopeExit<Func>&) = delete;
+
+ ScopeExit(ScopeExit<Func>&&) = default;
+ ScopeExit<Func>& operator=(ScopeExit<Func>&&) = default;
+};
+
+struct ScopeExitTag {};
+
+template<typename Func>
+inline ScopeExit<typename std::decay<Func>::type> operator<<(const ScopeExitTag&, Func&& func) {
+ return ScopeExit<typename std::decay<Func>::type>(std::forward<Func>(func));
+}
+
+}
+
+#define LDC_STRINGIZE2(a,b) a##b
+#define LDC_STRINGIZE(a,b) LDC_STRINGIZE2(a,b)
+#define LDC_UNNAME_VAR(basename) LDC_STRINGIZE(basename, __LINE__)
+
+#define SCOPE_EXIT auto LDC_UNNAME_VAR(scope_exit) = details::ScopeExitTag{} << [&]()
+
+#endif // HELPERS_H
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git
More information about the pkg-d-commits
mailing list