Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
132 lines
3.6 KiB
C++
132 lines
3.6 KiB
C++
#pragma once
|
|
|
|
#include <exception>
|
|
#include <utility>
|
|
|
|
namespace Void::Scope
|
|
{
|
|
|
|
// ── ScopeExit — runs on scope exit unconditionally ───────────────────────────
|
|
|
|
template <std::invocable F>
|
|
class [[nodiscard]] ScopeExit
|
|
{
|
|
public:
|
|
explicit ScopeExit(F f) noexcept(std::is_nothrow_move_constructible_v<F>)
|
|
: f_{std::move(f)}
|
|
{
|
|
}
|
|
|
|
ScopeExit(ScopeExit&& o) noexcept
|
|
: f_{std::move(o.f_)}, active_{o.active_}
|
|
{
|
|
o.active_ = false;
|
|
}
|
|
|
|
~ScopeExit() noexcept { if (active_) f_(); }
|
|
|
|
ScopeExit(const ScopeExit&) = delete;
|
|
ScopeExit& operator=(const ScopeExit&) = delete;
|
|
ScopeExit& operator=(ScopeExit&&) = delete;
|
|
|
|
void release() noexcept { active_ = false; }
|
|
void dismiss() noexcept { active_ = false; }
|
|
[[nodiscard]] bool active() const noexcept { return active_; }
|
|
|
|
private:
|
|
F f_;
|
|
bool active_{true};
|
|
};
|
|
|
|
// ── ScopeSuccess — runs only when scope exits normally (no exception) ─────────
|
|
|
|
template <std::invocable F>
|
|
class [[nodiscard]] ScopeSuccess
|
|
{
|
|
public:
|
|
explicit ScopeSuccess(F f) noexcept(std::is_nothrow_move_constructible_v<F>)
|
|
: f_{std::move(f)}, exceptions_{std::uncaught_exceptions()}
|
|
{
|
|
}
|
|
|
|
~ScopeSuccess() noexcept
|
|
{
|
|
if (active_ && std::uncaught_exceptions() == exceptions_) f_();
|
|
}
|
|
|
|
ScopeSuccess(const ScopeSuccess&) = delete;
|
|
ScopeSuccess& operator=(const ScopeSuccess&) = delete;
|
|
ScopeSuccess(ScopeSuccess&&) = delete;
|
|
ScopeSuccess& operator=(ScopeSuccess&&) = delete;
|
|
|
|
void release() noexcept { active_ = false; }
|
|
void dismiss() noexcept { active_ = false; }
|
|
|
|
private:
|
|
F f_;
|
|
int exceptions_;
|
|
bool active_{true};
|
|
};
|
|
|
|
// ── ScopeFail — runs only when scope exits via exception ──────────────────────
|
|
|
|
template <std::invocable F>
|
|
class [[nodiscard]] ScopeFail
|
|
{
|
|
public:
|
|
explicit ScopeFail(F f) noexcept(std::is_nothrow_move_constructible_v<F>)
|
|
: f_{std::move(f)}, exceptions_{std::uncaught_exceptions()}
|
|
{
|
|
}
|
|
|
|
~ScopeFail() noexcept
|
|
{
|
|
if (active_ && std::uncaught_exceptions() > exceptions_) f_();
|
|
}
|
|
|
|
ScopeFail(const ScopeFail&) = delete;
|
|
ScopeFail& operator=(const ScopeFail&) = delete;
|
|
ScopeFail(ScopeFail&&) = delete;
|
|
ScopeFail& operator=(ScopeFail&&) = delete;
|
|
|
|
void release() noexcept { active_ = false; }
|
|
void dismiss() noexcept { active_ = false; }
|
|
|
|
private:
|
|
F f_;
|
|
int exceptions_;
|
|
bool active_{true};
|
|
};
|
|
|
|
// ── Factory functions ─────────────────────────────────────────────────────────
|
|
|
|
template <std::invocable F>
|
|
[[nodiscard]] auto on_exit(F&& f)
|
|
{
|
|
return ScopeExit<std::decay_t<F>>{std::forward<F>(f)};
|
|
}
|
|
|
|
template <std::invocable F>
|
|
[[nodiscard]] auto on_success(F&& f)
|
|
{
|
|
return ScopeSuccess<std::decay_t<F>>{std::forward<F>(f)};
|
|
}
|
|
|
|
template <std::invocable F>
|
|
[[nodiscard]] auto on_fail(F&& f)
|
|
{
|
|
return ScopeFail<std::decay_t<F>>{std::forward<F>(f)};
|
|
}
|
|
|
|
// Aliases matching std::scope_{exit,success,failure} proposal naming.
|
|
template <std::invocable F>
|
|
[[nodiscard]] auto scope_exit(F&& f) { return on_exit(std::forward<F>(f)); }
|
|
|
|
template <std::invocable F>
|
|
[[nodiscard]] auto scope_success(F&& f) { return on_success(std::forward<F>(f)); }
|
|
|
|
template <std::invocable F>
|
|
[[nodiscard]] auto scope_failure(F&& f) { return on_fail(std::forward<F>(f)); }
|
|
|
|
} // namespace Void::Scope
|