diff options
author | FearlessTobi <thm.frey@gmail.com> | 2024-02-19 16:00:46 +0100 |
---|---|---|
committer | FearlessTobi <thm.frey@gmail.com> | 2024-02-19 16:00:46 +0100 |
commit | 310c1f50beb77fc5c6f9075029973161d4e51a4a (patch) | |
tree | 43a5699123e4930560fc5016faac7efb15b63f4e /src/common/scope_exit.h | |
parent | core/CMakeLists: Sort alphabetically (diff) | |
download | yuzu-310c1f50beb77fc5c6f9075029973161d4e51a4a.tar yuzu-310c1f50beb77fc5c6f9075029973161d4e51a4a.tar.gz yuzu-310c1f50beb77fc5c6f9075029973161d4e51a4a.tar.bz2 yuzu-310c1f50beb77fc5c6f9075029973161d4e51a4a.tar.lz yuzu-310c1f50beb77fc5c6f9075029973161d4e51a4a.tar.xz yuzu-310c1f50beb77fc5c6f9075029973161d4e51a4a.tar.zst yuzu-310c1f50beb77fc5c6f9075029973161d4e51a4a.zip |
Diffstat (limited to 'src/common/scope_exit.h')
-rw-r--r-- | src/common/scope_exit.h | 66 |
1 files changed, 46 insertions, 20 deletions
diff --git a/src/common/scope_exit.h b/src/common/scope_exit.h index e9c789c88..f3e88cde9 100644 --- a/src/common/scope_exit.h +++ b/src/common/scope_exit.h @@ -7,29 +7,61 @@ #include "common/common_funcs.h" namespace detail { -template <typename Func> -struct ScopeExitHelper { - explicit ScopeExitHelper(Func&& func_) : func(std::move(func_)) {} - ~ScopeExitHelper() { +template <class F> +class ScopeGuard { + YUZU_NON_COPYABLE(ScopeGuard); + +private: + F f; + bool active; + +public: + constexpr ScopeGuard(F f_) : f(std::move(f_)), active(true) {} + constexpr ~ScopeGuard() { if (active) { - func(); + f(); } } - - void Cancel() { + constexpr void Cancel() { active = false; } - Func func; - bool active{true}; + constexpr ScopeGuard(ScopeGuard&& rhs) : f(std::move(rhs.f)), active(rhs.active) { + rhs.Cancel(); + } + + ScopeGuard& operator=(ScopeGuard&& rhs) = delete; }; -template <typename Func> -ScopeExitHelper<Func> ScopeExit(Func&& func) { - return ScopeExitHelper<Func>(std::forward<Func>(func)); +template <class F> +constexpr ScopeGuard<F> MakeScopeGuard(F f) { + return ScopeGuard<F>(std::move(f)); } + +enum class ScopeGuardOnExit {}; + +template <typename F> +constexpr ScopeGuard<F> operator+(ScopeGuardOnExit, F&& f) { + return ScopeGuard<F>(std::forward<F>(f)); +} + } // namespace detail +#define CONCATENATE_IMPL(s1, s2) s1##s2 +#define CONCATENATE(s1, s2) CONCATENATE_IMPL(s1, s2) + +#ifdef __COUNTER__ +#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __COUNTER__) +#else +#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __LINE__) +#endif + +/** + * This macro is similar to SCOPE_EXIT, except the object is caller managed. This is intended to be + * used when the caller might want to cancel the ScopeExit. + */ +#define SCOPE_GUARD detail::ScopeGuardOnExit() + [&]() + /** * This macro allows you to conveniently specify a block of code that will run on scope exit. Handy * for doing ad-hoc clean-up tasks in a function with multiple returns. @@ -38,7 +70,7 @@ ScopeExitHelper<Func> ScopeExit(Func&& func) { * \code * const int saved_val = g_foo; * g_foo = 55; - * SCOPE_EXIT({ g_foo = saved_val; }); + * SCOPE_EXIT{ g_foo = saved_val; }; * * if (Bar()) { * return 0; @@ -47,10 +79,4 @@ ScopeExitHelper<Func> ScopeExit(Func&& func) { * } * \endcode */ -#define SCOPE_EXIT(body) auto CONCAT2(scope_exit_helper_, __LINE__) = detail::ScopeExit([&]() body) - -/** - * This macro is similar to SCOPE_EXIT, except the object is caller managed. This is intended to be - * used when the caller might want to cancel the ScopeExit. - */ -#define SCOPE_GUARD(body) detail::ScopeExit([&]() body) +#define SCOPE_EXIT auto ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE_) = SCOPE_GUARD |