From ce8660cc5bfc12e6e3f75d4cce22492783ca9066 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Thu, 8 Apr 2021 08:49:10 -0700 Subject: Initial work on table visualizer --- core/src/Utils/Macros.hpp | 6 ++++++ core/src/Utils/ScopeGuard.hpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 core/src/Utils/Macros.hpp create mode 100644 core/src/Utils/ScopeGuard.hpp (limited to 'core/src/Utils') diff --git a/core/src/Utils/Macros.hpp b/core/src/Utils/Macros.hpp new file mode 100644 index 0000000..cb949b2 --- /dev/null +++ b/core/src/Utils/Macros.hpp @@ -0,0 +1,6 @@ +#pragma once + +#define CONCAT_IMPL(a, b) a##b +#define CONCAT(a, b) CONCAT_IMPL(a, b) + +#define UNIQUE_NAME(prefix) CONCAT(prefix, __LINE__) diff --git a/core/src/Utils/ScopeGuard.hpp b/core/src/Utils/ScopeGuard.hpp new file mode 100644 index 0000000..ed8d4ea --- /dev/null +++ b/core/src/Utils/ScopeGuard.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "Utils/Macros.hpp" + +#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