summaryrefslogtreecommitdiff
path: root/core/src/Utils/ScopeGuard.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-04-08 08:49:10 -0700
committerrtk0c <[email protected]>2021-04-08 08:49:10 -0700
commitce8660cc5bfc12e6e3f75d4cce22492783ca9066 (patch)
treef5c1088def48e60570a1225d6440f304b37b5b5c /core/src/Utils/ScopeGuard.hpp
parent2f4b9db39239ed5150094a81743beea42a3eedc2 (diff)
Initial work on table visualizer
Diffstat (limited to 'core/src/Utils/ScopeGuard.hpp')
-rw-r--r--core/src/Utils/ScopeGuard.hpp35
1 files changed, 35 insertions, 0 deletions
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) = [&]()