From 7fe47a9d5b1727a61dc724523b530762f6d6ba19 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Thu, 30 Jun 2022 21:38:53 -0700 Subject: Restructure project --- app/source/Cplt/Utils/ScopeGuard.hpp | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 app/source/Cplt/Utils/ScopeGuard.hpp (limited to 'app/source/Cplt/Utils/ScopeGuard.hpp') diff --git a/app/source/Cplt/Utils/ScopeGuard.hpp b/app/source/Cplt/Utils/ScopeGuard.hpp new file mode 100644 index 0000000..f2b7f46 --- /dev/null +++ b/app/source/Cplt/Utils/ScopeGuard.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include + +#include + +template +class ScopeGuard +{ +private: + TCleanupFunc mFunc; + bool mDismissed = false; + +public: + /// Specifically left this implicit so that constructs like + /// \code + /// ScopeGuard sg = [&]() { res.Cleanup(); }; + /// \endcode + /// would work. It is highly discourage and unlikely that one would want to use ScopeGuard as a function + /// parameter, so the normal argument that implicit conversion are harmful doesn't really apply here. + ScopeGuard(TCleanupFunc func) + : mFunc{ std::move(func) } + { + } + + ~ScopeGuard() + { + if (!mDismissed) { + mFunc(); + } + } + + void Dismiss() noexcept + { + mDismissed = true; + } +}; + +#define DEFER ScopeGuard UNIQUE_NAME(scopeGuard) = [&]() -- cgit v1.2.3-70-g09d2