From ed50041589974d31296cb30dc1897f7fba6336c2 Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 20 Nov 2019 08:36:44 -0500 Subject: add upstream 10.18 --- zen/scope_guard.h | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'zen/scope_guard.h') diff --git a/zen/scope_guard.h b/zen/scope_guard.h index 3a79d841..5d3ac411 100644 --- a/zen/scope_guard.h +++ b/zen/scope_guard.h @@ -18,7 +18,7 @@ namespace zen { //Scope Guard /* - auto guardAio = zen::makeGuard([&] { ::CloseHandle(hDir); }); + auto guardAio = zen::makeGuard([&] { ::CloseHandle(hDir); }); ... guardAio.dismiss(); @@ -30,34 +30,35 @@ Scope Exit: enum class ScopeGuardRunMode { - ON_EXIT, - ON_SUCCESS, - ON_FAIL + onExit, + onSuccess, + onFail }; //partially specialize scope guard destructor code and get rid of those pesky MSVC "4127 conditional expression is constant" template inline -void runScopeGuardDestructor(F& fun, int /*exeptionCountOld*/, std::integral_constant) noexcept +void runScopeGuardDestructor(F& fun, bool failed, std::integral_constant) { - try { fun(); } - catch (...) { assert(false); } //consistency: don't expect exceptions for ON_EXIT even if "!failed"! + if (!failed) + fun(); //throw X + else + try { fun(); } + catch (...) { assert(false); } } template inline -void runScopeGuardDestructor(F& fun, int exeptionCountOld, std::integral_constant) +void runScopeGuardDestructor(F& fun, bool failed, std::integral_constant) { - const bool failed = std::uncaught_exceptions() > exeptionCountOld; if (!failed) fun(); //throw X } template inline -void runScopeGuardDestructor(F& fun, int exeptionCountOld, std::integral_constant) noexcept +void runScopeGuardDestructor(F& fun, bool failed, std::integral_constant) noexcept { - const bool failed = std::uncaught_exceptions() > exeptionCountOld; if (failed) try { fun(); } catch (...) { assert(false); } @@ -75,10 +76,13 @@ public: exeptionCount_(other.exeptionCount_), dismissed_(other.dismissed_) { other.dismissed_ = true; } - ~ScopeGuard() noexcept(runMode != ScopeGuardRunMode::ON_SUCCESS) + ~ScopeGuard() noexcept(runMode == ScopeGuardRunMode::onFail) { if (!dismissed_) - runScopeGuardDestructor(fun_, exeptionCount_, std::integral_constant()); + { + const bool failed = std::uncaught_exceptions() > exeptionCount_; + runScopeGuardDestructor(fun_, failed, std::integral_constant()); + } } void dismiss() { dismissed_ = true; } @@ -104,8 +108,8 @@ auto makeGuard(F&& fun) { return ScopeGuard>(std::forwa #define ZEN_CHECK_CASE_FOR_CONSTANT_IMPL(X) L ## X -#define ZEN_ON_SCOPE_EXIT(X) [[maybe_unused]] auto ZEN_CONCAT(scopeGuard, __LINE__) = zen::makeGuard([&]{ X; }); -#define ZEN_ON_SCOPE_FAIL(X) [[maybe_unused]] auto ZEN_CONCAT(scopeGuard, __LINE__) = zen::makeGuard([&]{ X; }); -#define ZEN_ON_SCOPE_SUCCESS(X) [[maybe_unused]] auto ZEN_CONCAT(scopeGuard, __LINE__) = zen::makeGuard([&]{ X; }); +#define ZEN_ON_SCOPE_EXIT(X) [[maybe_unused]] auto ZEN_CONCAT(scopeGuard, __LINE__) = zen::makeGuard([&]{ X; }); +#define ZEN_ON_SCOPE_FAIL(X) [[maybe_unused]] auto ZEN_CONCAT(scopeGuard, __LINE__) = zen::makeGuard([&]{ X; }); +#define ZEN_ON_SCOPE_SUCCESS(X) [[maybe_unused]] auto ZEN_CONCAT(scopeGuard, __LINE__) = zen::makeGuard([&]{ X; }); #endif //SCOPE_GUARD_H_8971632487321434 -- cgit