aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/scope.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/generic/scope.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/generic/scope.h')
-rw-r--r--util/generic/scope.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/util/generic/scope.h b/util/generic/scope.h
new file mode 100644
index 0000000000..b2c33af61e
--- /dev/null
+++ b/util/generic/scope.h
@@ -0,0 +1,65 @@
+#pragma once
+
+#include <util/system/compiler.h>
+#include <util/system/defaults.h>
+
+#include <utility>
+
+namespace NPrivate {
+ template <typename F>
+ class TScopeGuard {
+ public:
+ TScopeGuard(const F& function)
+ : Function_{function}
+ {
+ }
+
+ TScopeGuard(F&& function)
+ : Function_{std::move(function)}
+ {
+ }
+
+ TScopeGuard(TScopeGuard&&) = default;
+ TScopeGuard(const TScopeGuard&) = default;
+
+ ~TScopeGuard() {
+ Function_();
+ }
+
+ private:
+ F Function_;
+ };
+
+ struct TMakeGuardHelper {
+ template <class F>
+ TScopeGuard<F> operator|(F&& function) const {
+ return std::forward<F>(function);
+ }
+ };
+}
+
+// \brief `Y_SCOPE_EXIT(captures) { body };`
+//
+// General implementaion of RAII idiom (resource acquisition is initialization). Executes
+// function upon return from the current scope.
+//
+// @note expects `body` to provide no-throw guarantee, otherwise whenever an exception
+// is thrown and leaves the outermost block of `body`, the function `std::terminate` is called.
+// @see http://drdobbs.com/184403758 for detailed motivation.
+#define Y_SCOPE_EXIT(...) const auto Y_GENERATE_UNIQUE_ID(scopeGuard) Y_DECLARE_UNUSED = ::NPrivate::TMakeGuardHelper{} | [__VA_ARGS__]() mutable -> void
+
+// \brief `Y_DEFER { body };`
+//
+// Same as `Y_SCOPE_EXIT` but doesn't require user to provide capture-list explicitly (it
+// implicitly uses `[&]` capture). Have same requirements for `body`.
+//
+// Inspired by `defer` statement in languages like Swift and Go.
+//
+// \code
+// auto item = s.pop();
+// bool ok = false;
+// Y_DEFER { if (!ok) { s.push(std::move(item)); } };
+// ... try handle `item` ...
+// ok = true;
+// \endcode
+#define Y_DEFER Y_SCOPE_EXIT(&)