Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now an alias for Edit<string> so count_op/apply work on line diffs; Memoized uses std::apply instead of type-based get to handle duplicate arg types. 874/877 tests pass (3 are platform-gated plugin tests). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
114 lines
3.2 KiB
C++
114 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include <exception>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
namespace Void::ScopeGuard
|
|
{
|
|
|
|
// ── ScopeGuard ────────────────────────────────────────────────────────────────
|
|
// RAII wrapper that calls a cleanup function on scope exit.
|
|
// Always fires, regardless of whether the scope exits normally or via exception.
|
|
//
|
|
// auto guard = scope_exit([&] { file.close(); });
|
|
// guard.dismiss(); // cancel the cleanup
|
|
|
|
template <typename F>
|
|
class ScopeGuard
|
|
{
|
|
public:
|
|
explicit ScopeGuard(F&& fn) : fn_{std::forward<F>(fn)}, active_{true} {}
|
|
|
|
ScopeGuard(ScopeGuard&& o) noexcept
|
|
: fn_{std::move(o.fn_)}, active_{o.active_}
|
|
{
|
|
o.active_ = false;
|
|
}
|
|
|
|
ScopeGuard(const ScopeGuard&) = delete;
|
|
ScopeGuard& operator=(const ScopeGuard&) = delete;
|
|
ScopeGuard& operator=(ScopeGuard&&) = delete;
|
|
|
|
~ScopeGuard() { if (active_) fn_(); }
|
|
|
|
void dismiss() noexcept { active_ = false; }
|
|
[[nodiscard]] bool active() const noexcept { return active_; }
|
|
|
|
private:
|
|
F fn_;
|
|
bool active_;
|
|
};
|
|
|
|
// ── ScopeSuccess ──────────────────────────────────────────────────────────────
|
|
// Fires only when the scope exits normally (no exception in flight).
|
|
|
|
template <typename F>
|
|
class ScopeSuccess
|
|
{
|
|
public:
|
|
explicit ScopeSuccess(F&& fn)
|
|
: fn_{std::forward<F>(fn)},
|
|
exceptions_at_entry_{std::uncaught_exceptions()}
|
|
{}
|
|
|
|
~ScopeSuccess()
|
|
{
|
|
if (std::uncaught_exceptions() == exceptions_at_entry_) fn_();
|
|
}
|
|
|
|
ScopeSuccess(const ScopeSuccess&) = delete;
|
|
ScopeSuccess& operator=(const ScopeSuccess&) = delete;
|
|
|
|
private:
|
|
F fn_;
|
|
int exceptions_at_entry_;
|
|
};
|
|
|
|
// ── ScopeFailure ──────────────────────────────────────────────────────────────
|
|
// Fires only when the scope exits via an exception.
|
|
|
|
template <typename F>
|
|
class ScopeFailure
|
|
{
|
|
public:
|
|
explicit ScopeFailure(F&& fn)
|
|
: fn_{std::forward<F>(fn)},
|
|
exceptions_at_entry_{std::uncaught_exceptions()}
|
|
{}
|
|
|
|
~ScopeFailure()
|
|
{
|
|
if (std::uncaught_exceptions() > exceptions_at_entry_) fn_();
|
|
}
|
|
|
|
ScopeFailure(const ScopeFailure&) = delete;
|
|
ScopeFailure& operator=(const ScopeFailure&) = delete;
|
|
|
|
private:
|
|
F fn_;
|
|
int exceptions_at_entry_;
|
|
};
|
|
|
|
// ── factory functions ─────────────────────────────────────────────────────────
|
|
|
|
template <typename F>
|
|
[[nodiscard]] auto scope_exit(F&& fn)
|
|
{
|
|
return ScopeGuard<std::decay_t<F>>{std::forward<F>(fn)};
|
|
}
|
|
|
|
template <typename F>
|
|
[[nodiscard]] auto scope_success(F&& fn)
|
|
{
|
|
return ScopeSuccess<std::decay_t<F>>{std::forward<F>(fn)};
|
|
}
|
|
|
|
template <typename F>
|
|
[[nodiscard]] auto scope_failure(F&& fn)
|
|
{
|
|
return ScopeFailure<std::decay_t<F>>{std::forward<F>(fn)};
|
|
}
|
|
|
|
} // namespace Void::ScopeGuard
|