diff options
author | Daniel Wilhelm <shieldwed@outlook.com> | 2017-02-13 21:25:04 -0700 |
---|---|---|
committer | Daniel Wilhelm <shieldwed@outlook.com> | 2017-02-13 21:25:04 -0700 |
commit | 9d071d2a2cec9a7662a02669488569a017f0ea35 (patch) | |
tree | c83a623fbdff098339b66d21ea2e81f3f67344ae /zen/scope_guard.h | |
parent | 8.8 (diff) | |
download | FreeFileSync-9d071d2a2cec9a7662a02669488569a017f0ea35.tar.gz FreeFileSync-9d071d2a2cec9a7662a02669488569a017f0ea35.tar.bz2 FreeFileSync-9d071d2a2cec9a7662a02669488569a017f0ea35.zip |
8.9
Diffstat (limited to 'zen/scope_guard.h')
-rwxr-xr-x[-rw-r--r--] | zen/scope_guard.h | 251 |
1 files changed, 121 insertions, 130 deletions
diff --git a/zen/scope_guard.h b/zen/scope_guard.h index 853f51b9..09a7fbdb 100644..100755 --- a/zen/scope_guard.h +++ b/zen/scope_guard.h @@ -1,130 +1,121 @@ -// ***************************************************************************** -// * This file is part of the FreeFileSync project. It is distributed under * -// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * -// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved * -// ***************************************************************************** - -#ifndef SCOPE_GUARD_H_8971632487321434 -#define SCOPE_GUARD_H_8971632487321434 - -#include <cassert> -#include <exception> -#include "type_tools.h" - - -#ifdef ZEN_WIN -inline int getUncaughtExceptionCount() { return std::uncaught_exceptions(); } - -#elif defined ZEN_LINUX || defined ZEN_MAC -//std::uncaught_exceptions() currently unsupported on GCC and Clang => clean up ASAP -#ifdef ZEN_LINUX - static_assert(__GNUC__ < 6 || (__GNUC__ == 6 && (__GNUC_MINOR__ < 2 || (__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ <= 1))), "check std::uncaught_exceptions support"); -#else //std::uncaught_exceptions() requires "mmacosx-version-min=10.12" - static_assert(__clang_major__ < 8 || (__clang_major__ == 8 && __clang_minor__ <= 0), "check std::uncaught_exceptions support"); -#endif - -namespace __cxxabiv1 -{ -struct __cxa_eh_globals; -extern "C" __cxa_eh_globals* __cxa_get_globals() noexcept; -} - -inline int getUncaughtExceptionCount() -{ - return *(reinterpret_cast<unsigned int*>(static_cast<char*>(static_cast<void*>(__cxxabiv1::__cxa_get_globals())) + sizeof(void*))); -} -#endif - -//best of Zen, Loki and C++17 - -namespace zen -{ -//Scope Guard -/* - auto guardAio = zen::makeGuard<ScopeGuardRunMode::ON_EXIT>([&] { ::CloseHandle(hDir); }); - ... - guardAio.dismiss(); - -Scope Exit: - ZEN_ON_SCOPE_EXIT(::CloseHandle(hDir)); - ZEN_ON_SCOPE_FAIL(UndoPreviousWork()); - ZEN_ON_SCOPE_SUCCESS(NotifySuccess()); -*/ - -enum class ScopeGuardRunMode -{ - ON_EXIT, - ON_SUCCESS, - ON_FAIL -}; - - -//partially specialize scope guard destructor code and get rid of those pesky MSVC "4127 conditional expression is constant" -template <typename F> inline -void runScopeGuardDestructor(F& fun, int /*exeptionCountOld*/, StaticEnum<ScopeGuardRunMode, ScopeGuardRunMode::ON_EXIT>) -{ - try { fun(); } - catch (...) { assert(false); } //consistency: don't expect exceptions for ON_EXIT even if "!failed"! -} - - -template <typename F> inline -void runScopeGuardDestructor(F& fun, int exeptionCountOld, StaticEnum<ScopeGuardRunMode, ScopeGuardRunMode::ON_SUCCESS>) -{ - const bool failed = getUncaughtExceptionCount() > exeptionCountOld; - if (!failed) - fun(); //throw X -} - - -template <typename F> inline -void runScopeGuardDestructor(F& fun, int exeptionCountOld, StaticEnum<ScopeGuardRunMode, ScopeGuardRunMode::ON_FAIL>) -{ - const bool failed = getUncaughtExceptionCount() > exeptionCountOld; - if (failed) - try { fun(); } - catch (...) { assert(false); } -} - - -template <ScopeGuardRunMode runMode, typename F> -class ScopeGuard -{ -public: - explicit ScopeGuard(const F& fun) : fun_(fun) {} - explicit ScopeGuard( F&& fun) : fun_(std::move(fun)) {} - - ScopeGuard(ScopeGuard&& other) : fun_(std::move(other.fun_)), - exeptionCount_(other.exeptionCount_), - dismissed_(other.dismissed_) { other.dismissed_ = true; } - - ~ScopeGuard() noexcept(runMode != ScopeGuardRunMode::ON_SUCCESS) - { - if (!dismissed_) - runScopeGuardDestructor(fun_, exeptionCount_, StaticEnum<ScopeGuardRunMode, runMode>()); - } - - void dismiss() { dismissed_ = true; } - -private: - ScopeGuard (const ScopeGuard&) = delete; - ScopeGuard& operator=(const ScopeGuard&) = delete; - - F fun_; - const int exeptionCount_ = getUncaughtExceptionCount(); - bool dismissed_ = false; -}; - - -template <ScopeGuardRunMode runMode, class F> inline -auto makeGuard(F&& fun) { return ScopeGuard<runMode, std::decay_t<F>>(std::forward<F>(fun)); } -} - -#define ZEN_CONCAT_SUB(X, Y) X ## Y -#define ZEN_CONCAT(X, Y) ZEN_CONCAT_SUB(X, Y) - -#define ZEN_ON_SCOPE_EXIT(X) auto ZEN_CONCAT(dummy, __LINE__) = zen::makeGuard<zen::ScopeGuardRunMode::ON_EXIT >([&]{ X; }); (void)ZEN_CONCAT(dummy, __LINE__); -#define ZEN_ON_SCOPE_FAIL(X) auto ZEN_CONCAT(dummy, __LINE__) = zen::makeGuard<zen::ScopeGuardRunMode::ON_FAIL >([&]{ X; }); (void)ZEN_CONCAT(dummy, __LINE__); -#define ZEN_ON_SCOPE_SUCCESS(X) auto ZEN_CONCAT(dummy, __LINE__) = zen::makeGuard<zen::ScopeGuardRunMode::ON_SUCCESS>([&]{ X; }); (void)ZEN_CONCAT(dummy, __LINE__); - -#endif //SCOPE_GUARD_H_8971632487321434 +// *****************************************************************************
+// * This file is part of the FreeFileSync project. It is distributed under *
+// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 *
+// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
+// *****************************************************************************
+
+#ifndef SCOPE_GUARD_H_8971632487321434
+#define SCOPE_GUARD_H_8971632487321434
+
+#include <cassert>
+#include <exception>
+#include "type_tools.h"
+
+
+//std::uncaught_exceptions() currently unsupported on GCC and Clang => clean up ASAP
+ static_assert(__GNUC__ < 6 || (__GNUC__ == 6 && (__GNUC_MINOR__ < 2 || (__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ <= 1))), "check std::uncaught_exceptions support");
+
+namespace __cxxabiv1
+{
+struct __cxa_eh_globals;
+extern "C" __cxa_eh_globals* __cxa_get_globals() noexcept;
+}
+
+inline int getUncaughtExceptionCount()
+{
+ return *(reinterpret_cast<unsigned int*>(static_cast<char*>(static_cast<void*>(__cxxabiv1::__cxa_get_globals())) + sizeof(void*)));
+}
+
+//best of Zen, Loki and C++17
+
+namespace zen
+{
+//Scope Guard
+/*
+ auto guardAio = zen::makeGuard<ScopeGuardRunMode::ON_EXIT>([&] { ::CloseHandle(hDir); });
+ ...
+ guardAio.dismiss();
+
+Scope Exit:
+ ZEN_ON_SCOPE_EXIT(::CloseHandle(hDir));
+ ZEN_ON_SCOPE_FAIL(UndoPreviousWork());
+ ZEN_ON_SCOPE_SUCCESS(NotifySuccess());
+*/
+
+enum class ScopeGuardRunMode
+{
+ ON_EXIT,
+ ON_SUCCESS,
+ ON_FAIL
+};
+
+
+//partially specialize scope guard destructor code and get rid of those pesky MSVC "4127 conditional expression is constant"
+template <typename F> inline
+void runScopeGuardDestructor(F& fun, int /*exeptionCountOld*/, StaticEnum<ScopeGuardRunMode, ScopeGuardRunMode::ON_EXIT>)
+{
+ try { fun(); }
+ catch (...) { assert(false); } //consistency: don't expect exceptions for ON_EXIT even if "!failed"!
+}
+
+
+template <typename F> inline
+void runScopeGuardDestructor(F& fun, int exeptionCountOld, StaticEnum<ScopeGuardRunMode, ScopeGuardRunMode::ON_SUCCESS>)
+{
+ const bool failed = getUncaughtExceptionCount() > exeptionCountOld;
+ if (!failed)
+ fun(); //throw X
+}
+
+
+template <typename F> inline
+void runScopeGuardDestructor(F& fun, int exeptionCountOld, StaticEnum<ScopeGuardRunMode, ScopeGuardRunMode::ON_FAIL>)
+{
+ const bool failed = getUncaughtExceptionCount() > exeptionCountOld;
+ if (failed)
+ try { fun(); }
+ catch (...) { assert(false); }
+}
+
+
+template <ScopeGuardRunMode runMode, typename F>
+class ScopeGuard
+{
+public:
+ explicit ScopeGuard(const F& fun) : fun_(fun) {}
+ explicit ScopeGuard( F&& fun) : fun_(std::move(fun)) {}
+
+ ScopeGuard(ScopeGuard&& other) : fun_(std::move(other.fun_)),
+ exeptionCount_(other.exeptionCount_),
+ dismissed_(other.dismissed_) { other.dismissed_ = true; }
+
+ ~ScopeGuard() noexcept(runMode != ScopeGuardRunMode::ON_SUCCESS)
+ {
+ if (!dismissed_)
+ runScopeGuardDestructor(fun_, exeptionCount_, StaticEnum<ScopeGuardRunMode, runMode>());
+ }
+
+ void dismiss() { dismissed_ = true; }
+
+private:
+ ScopeGuard (const ScopeGuard&) = delete;
+ ScopeGuard& operator=(const ScopeGuard&) = delete;
+
+ F fun_;
+ const int exeptionCount_ = getUncaughtExceptionCount();
+ bool dismissed_ = false;
+};
+
+
+template <ScopeGuardRunMode runMode, class F> inline
+auto makeGuard(F&& fun) { return ScopeGuard<runMode, std::decay_t<F>>(std::forward<F>(fun)); }
+}
+
+#define ZEN_CONCAT_SUB(X, Y) X ## Y
+#define ZEN_CONCAT(X, Y) ZEN_CONCAT_SUB(X, Y)
+
+#define ZEN_ON_SCOPE_EXIT(X) auto ZEN_CONCAT(dummy, __LINE__) = zen::makeGuard<zen::ScopeGuardRunMode::ON_EXIT >([&]{ X; }); (void)ZEN_CONCAT(dummy, __LINE__);
+#define ZEN_ON_SCOPE_FAIL(X) auto ZEN_CONCAT(dummy, __LINE__) = zen::makeGuard<zen::ScopeGuardRunMode::ON_FAIL >([&]{ X; }); (void)ZEN_CONCAT(dummy, __LINE__);
+#define ZEN_ON_SCOPE_SUCCESS(X) auto ZEN_CONCAT(dummy, __LINE__) = zen::makeGuard<zen::ScopeGuardRunMode::ON_SUCCESS>([&]{ X; }); (void)ZEN_CONCAT(dummy, __LINE__);
+
+#endif //SCOPE_GUARD_H_8971632487321434
|