aboutsummaryrefslogtreecommitdiff
path: root/source/ScopeGuard.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-05-30 15:56:29 -0700
committerrtk0c <[email protected]>2022-05-30 15:56:29 -0700
commit80afa67d2b9f1c0605696a3fd69058544fe12fe4 (patch)
tree3dfe2a6f45e4f5ca8ad534baf06d76d9c558333b /source/ScopeGuard.hpp
parent0d92ecfdbfc875a099d9e83714b3a2209668fca5 (diff)
parent7d8bca09b3c4bf1418e758bd3bd0d6f85672153e (diff)
Changeset: 53
Diffstat (limited to 'source/ScopeGuard.hpp')
-rw-r--r--source/ScopeGuard.hpp60
1 files changed, 0 insertions, 60 deletions
diff --git a/source/ScopeGuard.hpp b/source/ScopeGuard.hpp
deleted file mode 100644
index 28f3385..0000000
--- a/source/ScopeGuard.hpp
+++ /dev/null
@@ -1,60 +0,0 @@
-#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) = [&]()