aboutsummaryrefslogtreecommitdiff
path: root/source/10-common/ScopeGuard.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2023-10-19 22:50:07 -0700
committerrtk0c <[email protected]>2025-08-16 11:31:16 -0700
commit297232d21594b138bb368a42b5b0d085ff9ed6aa (patch)
tree075d5407e1e12a9d35cbee6e4c20ad34e0765c42 /source/10-common/ScopeGuard.hpp
parentd5cd34ff69f7fd134d5450696f298af1a864afbc (diff)
The great renaming: switch to "module style"
Diffstat (limited to 'source/10-common/ScopeGuard.hpp')
-rw-r--r--source/10-common/ScopeGuard.hpp60
1 files changed, 0 insertions, 60 deletions
diff --git a/source/10-common/ScopeGuard.hpp b/source/10-common/ScopeGuard.hpp
deleted file mode 100644
index 4e1a348..0000000
--- a/source/10-common/ScopeGuard.hpp
+++ /dev/null
@@ -1,60 +0,0 @@
-#pragma once
-
-#include "Macros.hpp"
-
-#include <utility>
-
-template <typename 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 <typename T>
-auto GuardDeletion(T* ptr) {
- return ScopeGuard([ptr]() {
- delete ptr;
- });
-}
-
-#define SCOPE_GUARD(name) ScopeGuard name = [&]()
-#define DEFER ScopeGuard UNIQUE_NAME(scopeGuard) = [&]()