1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
//===-- gen/scope_exit.h - scope exit helper --------------------*- C++ -*-===//
//
// LDC - the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// SCOPE_EXIT helper construct.
//
//===----------------------------------------------------------------------===//
#pragma once
#include <utility>
#include <type_traits>
namespace details {
struct Ownership {
bool flag = false;
Ownership() = default;
Ownership(bool f): flag(f) {}
Ownership(const Ownership&) = delete;
Ownership& operator=(const Ownership&) = delete;
Ownership(Ownership&& rhs):
flag(rhs.flag) {
rhs.flag = false;
}
Ownership& operator=(Ownership&& rhs) {
flag = rhs.flag;
rhs.flag = false;
return *this;
}
operator bool() const {
return flag;
}
};
template<typename Func>
struct ScopeExit {
Func func;
Ownership active = false;
ScopeExit(Func&& f):
func(std::move(f)),
active(true) {}
~ScopeExit() {
if (active) {
func();
}
}
ScopeExit(const ScopeExit<Func>&) = delete;
ScopeExit<Func>& operator=(const ScopeExit<Func>&) = delete;
ScopeExit(ScopeExit<Func>&&) = default;
ScopeExit<Func>& operator=(ScopeExit<Func>&&) = default;
};
struct ScopeExitTag {};
template<typename Func>
inline ScopeExit<typename std::decay<Func>::type> operator<<(const ScopeExitTag&, Func&& func) {
return ScopeExit<typename std::decay<Func>::type>(std::forward<Func>(func));
}
}
#define LDC_STRINGIZE2(a,b) a##b
#define LDC_STRINGIZE(a,b) LDC_STRINGIZE2(a,b)
#define LDC_UNNAME_VAR(basename) LDC_STRINGIZE(basename, __LINE__)
#define SCOPE_EXIT auto LDC_UNNAME_VAR(scope_exit) = details::ScopeExitTag{} << [&]()
|