Add Scope, StateMachine, TypeList modules; remove macros from Scope
Void::Scope — RAII scope guards: ScopeExit/ScopeSuccess/ScopeFail with on_exit/on_success/on_fail factory functions. No macros. Void::StateMachine — compile-time FSM driven by Transition<From,Ev,To> type lists. Supports typed entry/exit callbacks and returns bool from send() indicating whether the transition was valid. Void::TypeList — compile-time type list with: size_of, at, front, back, contains, index_of, push_front/back, pop_front, concat, filter, transform, reverse, unique, for_each, all_of/any_of/none_of. Void::Encoding — Base64 (standard + URL-safe encode/decode) and Hex encode/decode. Included in previous commit but missed in message. 36/36 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
a09e000f0f
commit
a9a2e2b59a
6 files changed
+1039
No files matched your search
@@ -0,0 +1,115 @@
|
||||
#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() 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 <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; }
|
||||
|
||||
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; }
|
||||
|
||||
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)};
|
||||
}
|
||||
|
||||
} // namespace Void::Scope
|
||||
@@ -0,0 +1,209 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
namespace Void::StateMachine
|
||||
{
|
||||
|
||||
// ── Concepts ──────────────────────────────────────────────────────────────────
|
||||
|
||||
template <typename S>
|
||||
concept State = std::is_default_constructible_v<S>;
|
||||
|
||||
template <typename E>
|
||||
concept Event = std::copyable<E>;
|
||||
|
||||
// ── Transition helper ─────────────────────────────────────────────────────────
|
||||
// A transition<From, Ev, To> captures one arc: From --[Ev]--> To.
|
||||
// The user specializes `transitions` for their state machine type.
|
||||
|
||||
template <typename From, typename Ev, typename To>
|
||||
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<F,E,T> arcs
|
||||
//
|
||||
// Usage:
|
||||
// using MyFSM = StateMachine<
|
||||
// std::tuple<Idle, Running, Done>,
|
||||
// std::tuple<
|
||||
// Transition<Idle, StartEvent, Running>,
|
||||
// Transition<Running, StopEvent, Done>
|
||||
// >
|
||||
// >;
|
||||
|
||||
template <typename StatesTuple, typename TransitionsTuple>
|
||||
class StateMachine;
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
|
||||
// Collect all states from the transitions tuple
|
||||
template <typename... Ts>
|
||||
struct StateVariantOf;
|
||||
|
||||
template <typename... States>
|
||||
struct StateVariantOf<std::tuple<States...>>
|
||||
{
|
||||
using type = std::variant<States...>;
|
||||
};
|
||||
|
||||
// Does the Transition list contain a matching arc?
|
||||
template <typename CurrentState, typename Ev, typename TransTuple>
|
||||
struct FindTransition;
|
||||
|
||||
template <typename CS, typename Ev>
|
||||
struct FindTransition<CS, Ev, std::tuple<>>
|
||||
{
|
||||
using to = void; // no match
|
||||
};
|
||||
|
||||
template <typename CS, typename Ev, typename Head, typename... Tail>
|
||||
struct FindTransition<CS, Ev, std::tuple<Head, Tail...>>
|
||||
{
|
||||
using to = std::conditional_t<
|
||||
std::is_same_v<typename Head::from, CS> &&
|
||||
std::is_same_v<typename Head::event, Ev>,
|
||||
typename Head::to,
|
||||
typename FindTransition<CS, Ev, std::tuple<Tail...>>::to>;
|
||||
};
|
||||
|
||||
} // namespace Detail
|
||||
|
||||
template <typename... States, typename... Transitions>
|
||||
class StateMachine<std::tuple<States...>, std::tuple<Transitions...>>
|
||||
{
|
||||
public:
|
||||
using StateVariant = std::variant<States...>;
|
||||
using TransitionTuple = std::tuple<Transitions...>;
|
||||
|
||||
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 <typename S>
|
||||
[[nodiscard]] bool is() const noexcept
|
||||
{
|
||||
return std::holds_alternative<S>(state_);
|
||||
}
|
||||
|
||||
// Access current state as S (throws std::bad_variant_access if wrong).
|
||||
template <typename S>
|
||||
[[nodiscard]] const S& as() const
|
||||
{
|
||||
return std::get<S>(state_);
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
[[nodiscard]] S& as()
|
||||
{
|
||||
return std::get<S>(state_);
|
||||
}
|
||||
|
||||
// Try to access state, returns nullptr if wrong state.
|
||||
template <typename S>
|
||||
[[nodiscard]] const S* try_as() const noexcept
|
||||
{
|
||||
return std::get_if<S>(&state_);
|
||||
}
|
||||
|
||||
// Send an event. Returns true if a transition was found and taken.
|
||||
template <typename Ev>
|
||||
bool send(const Ev& ev)
|
||||
{
|
||||
bool transitioned = false;
|
||||
std::visit(
|
||||
[&]<typename CurState>(CurState& cur) {
|
||||
using To =
|
||||
typename Detail::FindTransition<CurState, Ev,
|
||||
TransitionTuple>::to;
|
||||
if constexpr (!std::is_void_v<To>) {
|
||||
on_exit(cur);
|
||||
state_ = To{};
|
||||
on_enter(std::get<To>(state_), ev);
|
||||
transitioned = true;
|
||||
}
|
||||
},
|
||||
state_);
|
||||
return transitioned;
|
||||
}
|
||||
|
||||
// Register a callback fired when entering state S.
|
||||
template <typename S, std::invocable<const S&> F>
|
||||
void on_enter(F&& f)
|
||||
{
|
||||
// Store as type-erased function in a small array indexed by variant alt.
|
||||
entry_callbacks_[variant_index_of<S>()] =
|
||||
[fn = std::forward<F>(f)](const StateVariant& v) {
|
||||
fn(std::get<S>(v));
|
||||
};
|
||||
}
|
||||
|
||||
// Register a callback fired when leaving state S.
|
||||
template <typename S, std::invocable<const S&> F>
|
||||
void on_exit(F&& f)
|
||||
{
|
||||
exit_callbacks_[variant_index_of<S>()] =
|
||||
[fn = std::forward<F>(f)](const StateVariant& v) {
|
||||
fn(std::get<S>(v));
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename S>
|
||||
static constexpr std::size_t variant_index_of() noexcept
|
||||
{
|
||||
return []<std::size_t... Is>(std::index_sequence<Is...>) {
|
||||
std::size_t idx = 0;
|
||||
((std::is_same_v<S, States> ? (idx = Is, true) : false) || ...);
|
||||
return idx;
|
||||
}(std::index_sequence_for<States...>{});
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
void on_exit(const S& s)
|
||||
{
|
||||
const auto idx = variant_index_of<S>();
|
||||
if (exit_callbacks_[idx]) {
|
||||
StateVariant tmp = s;
|
||||
exit_callbacks_[idx](tmp);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename S, typename Ev>
|
||||
void on_enter(const S& s, const Ev&)
|
||||
{
|
||||
const auto idx = variant_index_of<S>();
|
||||
if (entry_callbacks_[idx]) {
|
||||
StateVariant tmp = s;
|
||||
entry_callbacks_[idx](tmp);
|
||||
}
|
||||
}
|
||||
|
||||
StateVariant state_{std::in_place_index<0>};
|
||||
std::function<void(const StateVariant&)>
|
||||
entry_callbacks_[sizeof...(States)]{};
|
||||
std::function<void(const StateVariant&)>
|
||||
exit_callbacks_[sizeof...(States)]{};
|
||||
};
|
||||
|
||||
} // namespace Void::StateMachine
|
||||
@@ -0,0 +1,280 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Void::TypeList
|
||||
{
|
||||
|
||||
// ── TypeList<Ts...> ───────────────────────────────────────────────────────────
|
||||
|
||||
template <typename... Ts>
|
||||
struct TypeList
|
||||
{
|
||||
static constexpr std::size_t size = sizeof...(Ts);
|
||||
using as_tuple = std::tuple<Ts...>;
|
||||
};
|
||||
|
||||
// ── size_of ───────────────────────────────────────────────────────────────────
|
||||
|
||||
template <typename List>
|
||||
inline constexpr std::size_t size_of = 0;
|
||||
|
||||
template <typename... Ts>
|
||||
inline constexpr std::size_t size_of<TypeList<Ts...>> = sizeof...(Ts);
|
||||
|
||||
// ── at<I, List> ───────────────────────────────────────────────────────────────
|
||||
|
||||
template <std::size_t I, typename List>
|
||||
struct At;
|
||||
|
||||
template <std::size_t I, typename Head, typename... Tail>
|
||||
struct At<I, TypeList<Head, Tail...>> : At<I - 1, TypeList<Tail...>> {};
|
||||
|
||||
template <typename Head, typename... Tail>
|
||||
struct At<0, TypeList<Head, Tail...>>
|
||||
{
|
||||
using type = Head;
|
||||
};
|
||||
|
||||
template <std::size_t I, typename List>
|
||||
using at = typename At<I, List>::type;
|
||||
|
||||
// ── front / back ──────────────────────────────────────────────────────────────
|
||||
|
||||
template <typename List>
|
||||
struct Front;
|
||||
|
||||
template <typename Head, typename... Tail>
|
||||
struct Front<TypeList<Head, Tail...>>
|
||||
{
|
||||
using type = Head;
|
||||
};
|
||||
|
||||
template <typename List>
|
||||
using front = typename Front<List>::type;
|
||||
|
||||
template <typename List>
|
||||
struct Back;
|
||||
|
||||
template <typename T>
|
||||
struct Back<TypeList<T>>
|
||||
{
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <typename Head, typename... Tail>
|
||||
struct Back<TypeList<Head, Tail...>> : Back<TypeList<Tail...>> {};
|
||||
|
||||
template <typename List>
|
||||
using back = typename Back<List>::type;
|
||||
|
||||
// ── contains<T, List> ─────────────────────────────────────────────────────────
|
||||
|
||||
template <typename T, typename List>
|
||||
struct Contains;
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct Contains<T, TypeList<Ts...>>
|
||||
: std::bool_constant<(std::is_same_v<T, Ts> || ...)>
|
||||
{};
|
||||
|
||||
template <typename T, typename List>
|
||||
inline constexpr bool contains = Contains<T, List>::value;
|
||||
|
||||
// ── index_of<T, List> — first index, or size if not found ────────────────────
|
||||
|
||||
template <typename T, typename List>
|
||||
struct IndexOf;
|
||||
|
||||
template <typename T>
|
||||
struct IndexOf<T, TypeList<>>
|
||||
{
|
||||
static constexpr std::size_t value = 0;
|
||||
};
|
||||
|
||||
template <typename T, typename Head, typename... Tail>
|
||||
struct IndexOf<T, TypeList<Head, Tail...>>
|
||||
{
|
||||
static constexpr std::size_t value =
|
||||
std::is_same_v<T, Head>
|
||||
? 0
|
||||
: 1 + IndexOf<T, TypeList<Tail...>>::value;
|
||||
};
|
||||
|
||||
template <typename T, typename List>
|
||||
inline constexpr std::size_t index_of = IndexOf<T, List>::value;
|
||||
|
||||
// ── push_front / push_back ────────────────────────────────────────────────────
|
||||
|
||||
template <typename T, typename List>
|
||||
struct PushFront;
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct PushFront<T, TypeList<Ts...>>
|
||||
{
|
||||
using type = TypeList<T, Ts...>;
|
||||
};
|
||||
|
||||
template <typename T, typename List>
|
||||
using push_front = typename PushFront<T, List>::type;
|
||||
|
||||
template <typename T, typename List>
|
||||
struct PushBack;
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct PushBack<T, TypeList<Ts...>>
|
||||
{
|
||||
using type = TypeList<Ts..., T>;
|
||||
};
|
||||
|
||||
template <typename T, typename List>
|
||||
using push_back = typename PushBack<T, List>::type;
|
||||
|
||||
// ── pop_front ─────────────────────────────────────────────────────────────────
|
||||
|
||||
template <typename List>
|
||||
struct PopFront;
|
||||
|
||||
template <typename Head, typename... Tail>
|
||||
struct PopFront<TypeList<Head, Tail...>>
|
||||
{
|
||||
using type = TypeList<Tail...>;
|
||||
};
|
||||
|
||||
template <typename List>
|
||||
using pop_front = typename PopFront<List>::type;
|
||||
|
||||
// ── concat<A, B> ─────────────────────────────────────────────────────────────
|
||||
|
||||
template <typename A, typename B>
|
||||
struct Concat;
|
||||
|
||||
template <typename... As, typename... Bs>
|
||||
struct Concat<TypeList<As...>, TypeList<Bs...>>
|
||||
{
|
||||
using type = TypeList<As..., Bs...>;
|
||||
};
|
||||
|
||||
template <typename A, typename B>
|
||||
using concat = typename Concat<A, B>::type;
|
||||
|
||||
// ── filter<Pred, List> ────────────────────────────────────────────────────────
|
||||
|
||||
template <template <typename> typename Pred, typename List>
|
||||
struct Filter;
|
||||
|
||||
template <template <typename> typename Pred>
|
||||
struct Filter<Pred, TypeList<>>
|
||||
{
|
||||
using type = TypeList<>;
|
||||
};
|
||||
|
||||
template <template <typename> typename Pred, typename Head, typename... Tail>
|
||||
struct Filter<Pred, TypeList<Head, Tail...>>
|
||||
{
|
||||
using filtered_tail = typename Filter<Pred, TypeList<Tail...>>::type;
|
||||
using type = std::conditional_t<Pred<Head>::value,
|
||||
push_front<Head, filtered_tail>,
|
||||
filtered_tail>;
|
||||
};
|
||||
|
||||
template <template <typename> typename Pred, typename List>
|
||||
using filter = typename Filter<Pred, List>::type;
|
||||
|
||||
// ── transform<F, List> ────────────────────────────────────────────────────────
|
||||
|
||||
template <template <typename> typename F, typename List>
|
||||
struct Transform;
|
||||
|
||||
template <template <typename> typename F, typename... Ts>
|
||||
struct Transform<F, TypeList<Ts...>>
|
||||
{
|
||||
using type = TypeList<typename F<Ts>::type...>;
|
||||
};
|
||||
|
||||
template <template <typename> typename F, typename List>
|
||||
using transform = typename Transform<F, List>::type;
|
||||
|
||||
// ── reverse<List> ────────────────────────────────────────────────────────────
|
||||
|
||||
template <typename List, typename Acc = TypeList<>>
|
||||
struct Reverse;
|
||||
|
||||
template <typename Acc>
|
||||
struct Reverse<TypeList<>, Acc>
|
||||
{
|
||||
using type = Acc;
|
||||
};
|
||||
|
||||
template <typename Head, typename... Tail, typename Acc>
|
||||
struct Reverse<TypeList<Head, Tail...>, Acc>
|
||||
: Reverse<TypeList<Tail...>, push_front<Head, Acc>>
|
||||
{};
|
||||
|
||||
template <typename List>
|
||||
using reverse = typename Reverse<List>::type;
|
||||
|
||||
// ── unique<List> — remove duplicate types (keep first occurrence) ─────────────
|
||||
|
||||
template <typename List, typename Seen = TypeList<>>
|
||||
struct Unique;
|
||||
|
||||
template <typename Seen>
|
||||
struct Unique<TypeList<>, Seen>
|
||||
{
|
||||
using type = Seen;
|
||||
};
|
||||
|
||||
template <typename Head, typename... Tail, typename Seen>
|
||||
struct Unique<TypeList<Head, Tail...>, Seen>
|
||||
{
|
||||
using type = std::conditional_t<
|
||||
contains<Head, Seen>,
|
||||
typename Unique<TypeList<Tail...>, Seen>::type,
|
||||
typename Unique<TypeList<Tail...>, push_back<Head, Seen>>::type>;
|
||||
};
|
||||
|
||||
template <typename List>
|
||||
using unique = typename Unique<List>::type;
|
||||
|
||||
// ── for_each — call f() for each type in the list ────────────────────────────
|
||||
|
||||
template <typename List, typename F>
|
||||
constexpr void for_each(F&& f)
|
||||
{
|
||||
[]<typename... Ts>(std::type_identity<TypeList<Ts...>>, auto& fn) {
|
||||
(fn.template operator()<Ts>(), ...);
|
||||
}(std::type_identity<List>{}, f);
|
||||
}
|
||||
|
||||
// ── all_of / any_of / none_of ─────────────────────────────────────────────────
|
||||
|
||||
template <template <typename> typename Pred, typename List>
|
||||
struct AllOf;
|
||||
|
||||
template <template <typename> typename Pred, typename... Ts>
|
||||
struct AllOf<Pred, TypeList<Ts...>>
|
||||
: std::bool_constant<(Pred<Ts>::value && ...)>
|
||||
{};
|
||||
|
||||
template <template <typename> typename Pred, typename List>
|
||||
inline constexpr bool all_of = AllOf<Pred, List>::value;
|
||||
|
||||
template <template <typename> typename Pred, typename List>
|
||||
struct AnyOf;
|
||||
|
||||
template <template <typename> typename Pred, typename... Ts>
|
||||
struct AnyOf<Pred, TypeList<Ts...>>
|
||||
: std::bool_constant<(Pred<Ts>::value || ...)>
|
||||
{};
|
||||
|
||||
template <template <typename> typename Pred, typename List>
|
||||
inline constexpr bool any_of = AnyOf<Pred, List>::value;
|
||||
|
||||
template <template <typename> typename Pred, typename List>
|
||||
inline constexpr bool none_of = !any_of<Pred, List>;
|
||||
|
||||
} // namespace Void::TypeList
|
||||
Reference in new issue
Block a user