aboutsummaryrefslogtreecommitdiff
path: root/source-common/ScopeGuard.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-05-30 17:03:20 -0700
committerrtk0c <[email protected]>2022-05-30 17:03:20 -0700
commite66286ebe30afc9acc4531fc2bea29b7fb924f93 (patch)
treefa6b76554c3eb88bc8f088fbab68e20c40118ca7 /source-common/ScopeGuard.hpp
parent366ef5a5450c6e0e680c924c3454943a9ae9814d (diff)
Changeset: 56 Buildsystem cleanup: change to layered structure for different targets
Diffstat (limited to 'source-common/ScopeGuard.hpp')
-rw-r--r--source-common/ScopeGuard.hpp60
1 files changed, 0 insertions, 60 deletions
diff --git a/source-common/ScopeGuard.hpp b/source-common/ScopeGuard.hpp
deleted file mode 100644
index 28f3385..0000000
--- a/source-common/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) = [&]()