aboutsummaryrefslogtreecommitdiff
path: root/source/Common/ScopeGuard.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-06-03 23:26:44 -0700
committerrtk0c <[email protected]>2022-06-03 23:26:44 -0700
commit60ccc62f4934e44ad5b905fdbcf458302b8d8a09 (patch)
tree02ec83cc8387abfd08bd5ee7ea4e8115f1bfb8d0 /source/Common/ScopeGuard.hpp
parentc2ef7737536bf1f8c81fcfae95c0183b21c9753f (diff)
Changeset: 63 [WIP] Rename directories
Diffstat (limited to 'source/Common/ScopeGuard.hpp')
-rw-r--r--source/Common/ScopeGuard.hpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/source/Common/ScopeGuard.hpp b/source/Common/ScopeGuard.hpp
new file mode 100644
index 0000000..28f3385
--- /dev/null
+++ b/source/Common/ScopeGuard.hpp
@@ -0,0 +1,60 @@
+#pragma once
+
+#include "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.
+ // Deliberately not explicit to allow usages like: ScopeGuard var = lambda;
+ ScopeGuard(TCleanupFunc&& function) noexcept
+ : mFunc{ std::move(function) } {
+ }
+
+ ~ScopeGuard() noexcept {
+ if (!mDismissed) {
+ mFunc();
+ }
+ }
+
+ ScopeGuard(const ScopeGuard&) = delete;
+ ScopeGuard& operator=(const ScopeGuard&) = delete;
+
+ ScopeGuard(ScopeGuard&& that) noexcept
+ : mFunc{ std::move(that.mFunc) } {
+ that.Cancel();
+ }
+
+ ScopeGuard& operator=(ScopeGuard&& that) noexcept {
+ if (!mDismissed) {
+ mFunc();
+ }
+ this->mFunc = std::move(that.mFunc);
+ this->cancelled = std::exchange(that.cancelled, true);
+ }
+
+ void Dismiss() noexcept {
+ mDismissed = true;
+ }
+};
+
+template <class T>
+auto GuardDeletion(T* ptr) {
+ return ScopeGuard([ptr]() {
+ delete ptr;
+ });
+}
+
+#define SCOPE_GUARD(name) ScopeGuard name = [&]()
+#define DEFER ScopeGuard UNIQUE_NAME(scopeGuard) = [&]()