diff --git a/include/Void/Scope/Scope.hpp b/include/Void/Scope/Scope.hpp new file mode 100644 index 0000000..7c78dda --- /dev/null +++ b/include/Void/Scope/Scope.hpp @@ -0,0 +1,115 @@ +#pragma once + +#include +#include + +namespace Void::Scope +{ + +// ── ScopeExit — runs on scope exit unconditionally ─────────────────────────── + +template +class [[nodiscard]] ScopeExit +{ +public: + explicit ScopeExit(F f) noexcept(std::is_nothrow_move_constructible_v) + : f_{std::move(f)} + { + } + + ~ScopeExit() noexcept + { + if (active_) f_(); + } + + ScopeExit(const ScopeExit&) = delete; + ScopeExit& operator=(const ScopeExit&) = delete; + ScopeExit(ScopeExit&&) = delete; + ScopeExit& operator=(ScopeExit&&) = delete; + + void release() noexcept { active_ = false; } + +private: + F f_; + bool active_{true}; +}; + +// ── ScopeSuccess — runs only when scope exits normally (no exception) ───────── + +template +class [[nodiscard]] ScopeSuccess +{ +public: + explicit ScopeSuccess(F f) noexcept(std::is_nothrow_move_constructible_v) + : 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; } + +private: + F f_; + int exceptions_; + bool active_{true}; +}; + +// ── ScopeFail — runs only when scope exits via exception ────────────────────── + +template +class [[nodiscard]] ScopeFail +{ +public: + explicit ScopeFail(F f) noexcept(std::is_nothrow_move_constructible_v) + : 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; } + +private: + F f_; + int exceptions_; + bool active_{true}; +}; + +// ── Factory functions ───────────────────────────────────────────────────────── + +template +[[nodiscard]] auto on_exit(F&& f) +{ + return ScopeExit>{std::forward(f)}; +} + +template +[[nodiscard]] auto on_success(F&& f) +{ + return ScopeSuccess>{std::forward(f)}; +} + +template +[[nodiscard]] auto on_fail(F&& f) +{ + return ScopeFail>{std::forward(f)}; +} + +} // namespace Void::Scope diff --git a/include/Void/StateMachine/StateMachine.hpp b/include/Void/StateMachine/StateMachine.hpp new file mode 100644 index 0000000..3c892ce --- /dev/null +++ b/include/Void/StateMachine/StateMachine.hpp @@ -0,0 +1,209 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace Void::StateMachine +{ + +// ── Concepts ────────────────────────────────────────────────────────────────── + +template +concept State = std::is_default_constructible_v; + +template +concept Event = std::copyable; + +// ── Transition helper ───────────────────────────────────────────────────────── +// A transition captures one arc: From --[Ev]--> To. +// The user specializes `transitions` for their state machine type. + +template +struct Transition +{ + using from = From; + using event = Ev; + using to = To; +}; + +// ── StateMachine ────────────────────────────────────────────────────────────── +// +// Template parameters: +// States — type list of all states, first element is the initial state +// Transitions — type list of Transition arcs +// +// Usage: +// using MyFSM = StateMachine< +// std::tuple, +// std::tuple< +// Transition, +// Transition +// > +// >; + +template +class StateMachine; + +namespace Detail +{ + +// Collect all states from the transitions tuple +template +struct StateVariantOf; + +template +struct StateVariantOf> +{ + using type = std::variant; +}; + +// Does the Transition list contain a matching arc? +template +struct FindTransition; + +template +struct FindTransition> +{ + using to = void; // no match +}; + +template +struct FindTransition> +{ + using to = std::conditional_t< + std::is_same_v && + std::is_same_v, + typename Head::to, + typename FindTransition>::to>; +}; + +} // namespace Detail + +template +class StateMachine, std::tuple> +{ +public: + using StateVariant = std::variant; + using TransitionTuple = std::tuple; + + StateMachine() = default; + + // Current state index (0-based, matches variant index). + [[nodiscard]] std::size_t state_index() const noexcept + { + return state_.index(); + } + + // True if current state is S. + template + [[nodiscard]] bool is() const noexcept + { + return std::holds_alternative(state_); + } + + // Access current state as S (throws std::bad_variant_access if wrong). + template + [[nodiscard]] const S& as() const + { + return std::get(state_); + } + + template + [[nodiscard]] S& as() + { + return std::get(state_); + } + + // Try to access state, returns nullptr if wrong state. + template + [[nodiscard]] const S* try_as() const noexcept + { + return std::get_if(&state_); + } + + // Send an event. Returns true if a transition was found and taken. + template + bool send(const Ev& ev) + { + bool transitioned = false; + std::visit( + [&](CurState& cur) { + using To = + typename Detail::FindTransition::to; + if constexpr (!std::is_void_v) { + on_exit(cur); + state_ = To{}; + on_enter(std::get(state_), ev); + transitioned = true; + } + }, + state_); + return transitioned; + } + + // Register a callback fired when entering state S. + template F> + void on_enter(F&& f) + { + // Store as type-erased function in a small array indexed by variant alt. + entry_callbacks_[variant_index_of()] = + [fn = std::forward(f)](const StateVariant& v) { + fn(std::get(v)); + }; + } + + // Register a callback fired when leaving state S. + template F> + void on_exit(F&& f) + { + exit_callbacks_[variant_index_of()] = + [fn = std::forward(f)](const StateVariant& v) { + fn(std::get(v)); + }; + } + +private: + template + static constexpr std::size_t variant_index_of() noexcept + { + return [](std::index_sequence) { + std::size_t idx = 0; + ((std::is_same_v ? (idx = Is, true) : false) || ...); + return idx; + }(std::index_sequence_for{}); + } + + template + void on_exit(const S& s) + { + const auto idx = variant_index_of(); + if (exit_callbacks_[idx]) { + StateVariant tmp = s; + exit_callbacks_[idx](tmp); + } + } + + template + void on_enter(const S& s, const Ev&) + { + const auto idx = variant_index_of(); + if (entry_callbacks_[idx]) { + StateVariant tmp = s; + entry_callbacks_[idx](tmp); + } + } + + StateVariant state_{std::in_place_index<0>}; + std::function + entry_callbacks_[sizeof...(States)]{}; + std::function + exit_callbacks_[sizeof...(States)]{}; +}; + +} // namespace Void::StateMachine diff --git a/include/Void/TypeList/TypeList.hpp b/include/Void/TypeList/TypeList.hpp new file mode 100644 index 0000000..3371ac6 --- /dev/null +++ b/include/Void/TypeList/TypeList.hpp @@ -0,0 +1,280 @@ +#pragma once + +#include +#include +#include +#include + +namespace Void::TypeList +{ + +// ── TypeList ─────────────────────────────────────────────────────────── + +template +struct TypeList +{ + static constexpr std::size_t size = sizeof...(Ts); + using as_tuple = std::tuple; +}; + +// ── size_of ─────────────────────────────────────────────────────────────────── + +template +inline constexpr std::size_t size_of = 0; + +template +inline constexpr std::size_t size_of> = sizeof...(Ts); + +// ── at ─────────────────────────────────────────────────────────────── + +template +struct At; + +template +struct At> : At> {}; + +template +struct At<0, TypeList> +{ + using type = Head; +}; + +template +using at = typename At::type; + +// ── front / back ────────────────────────────────────────────────────────────── + +template +struct Front; + +template +struct Front> +{ + using type = Head; +}; + +template +using front = typename Front::type; + +template +struct Back; + +template +struct Back> +{ + using type = T; +}; + +template +struct Back> : Back> {}; + +template +using back = typename Back::type; + +// ── contains ───────────────────────────────────────────────────────── + +template +struct Contains; + +template +struct Contains> + : std::bool_constant<(std::is_same_v || ...)> +{}; + +template +inline constexpr bool contains = Contains::value; + +// ── index_of — first index, or size if not found ──────────────────── + +template +struct IndexOf; + +template +struct IndexOf> +{ + static constexpr std::size_t value = 0; +}; + +template +struct IndexOf> +{ + static constexpr std::size_t value = + std::is_same_v + ? 0 + : 1 + IndexOf>::value; +}; + +template +inline constexpr std::size_t index_of = IndexOf::value; + +// ── push_front / push_back ──────────────────────────────────────────────────── + +template +struct PushFront; + +template +struct PushFront> +{ + using type = TypeList; +}; + +template +using push_front = typename PushFront::type; + +template +struct PushBack; + +template +struct PushBack> +{ + using type = TypeList; +}; + +template +using push_back = typename PushBack::type; + +// ── pop_front ───────────────────────────────────────────────────────────────── + +template +struct PopFront; + +template +struct PopFront> +{ + using type = TypeList; +}; + +template +using pop_front = typename PopFront::type; + +// ── concat ───────────────────────────────────────────────────────────── + +template +struct Concat; + +template +struct Concat, TypeList> +{ + using type = TypeList; +}; + +template +using concat = typename Concat::type; + +// ── filter ──────────────────────────────────────────────────────── + +template