diff options
author | rtk0c <[email protected]> | 2021-04-08 08:49:10 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-04-08 08:49:10 -0700 |
commit | ce8660cc5bfc12e6e3f75d4cce22492783ca9066 (patch) | |
tree | f5c1088def48e60570a1225d6440f304b37b5b5c /core/src/Utils | |
parent | 2f4b9db39239ed5150094a81743beea42a3eedc2 (diff) |
Initial work on table visualizer
Diffstat (limited to 'core/src/Utils')
-rw-r--r-- | core/src/Utils/Macros.hpp | 6 | ||||
-rw-r--r-- | core/src/Utils/ScopeGuard.hpp | 35 |
2 files changed, 41 insertions, 0 deletions
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 <utility> + +template <class TCleanupFunc> +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) = [&]() |