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:
faumarayandClaude Sonnet 4.6 committed 2026-05-14 22:49:22 +03:00
1 parent a09e000f0f
commit a9a2e2b59a
6 files changed
+1039

No files matched your search

+115
View File
@@ -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
+209
View File
@@ -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
+280
View File
@@ -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
+121
View File
@@ -0,0 +1,121 @@
#include <Void/Scope/Scope.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <stdexcept>
#include <string>
// ── on_exit ───────────────────────────────────────────────────────────────────
Void::Testing::Test on_exit_runs_on_normal_exit()
{
bool ran = false;
{
auto guard = Void::Scope::on_exit([&] { ran = true; });
}
Void::Testing::expect(ran);
}
Void::Testing::Test on_exit_runs_on_exception()
{
bool ran = false;
try {
auto guard = Void::Scope::on_exit([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(ran);
}
Void::Testing::Test on_exit_release_prevents_run()
{
bool ran = false;
{
auto guard = Void::Scope::on_exit([&] { ran = true; });
guard.release();
}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_exit_lifo_order()
{
std::string order;
{
auto a = Void::Scope::on_exit([&] { order += "A"; });
auto b = Void::Scope::on_exit([&] { order += "B"; });
auto c = Void::Scope::on_exit([&] { order += "C"; });
}
Void::Testing::expect_eq(order, std::string{"CBA"});
}
Void::Testing::Test on_exit_captures_by_ref()
{
int count = 0;
{
auto guard = Void::Scope::on_exit([&] { count += 10; });
count = 5;
}
Void::Testing::expect_eq(count, 15);
}
// ── on_success ────────────────────────────────────────────────────────────────
Void::Testing::Test on_success_runs_on_normal_exit()
{
bool ran = false;
{
auto guard = Void::Scope::on_success([&] { ran = true; });
}
Void::Testing::expect(ran);
}
Void::Testing::Test on_success_does_not_run_on_exception()
{
bool ran = false;
try {
auto guard = Void::Scope::on_success([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_success_release_prevents_run()
{
bool ran = false;
{
auto guard = Void::Scope::on_success([&] { ran = true; });
guard.release();
}
Void::Testing::expect(!ran);
}
// ── on_fail ───────────────────────────────────────────────────────────────────
Void::Testing::Test on_fail_does_not_run_on_normal_exit()
{
bool ran = false;
{
auto guard = Void::Scope::on_fail([&] { ran = true; });
}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_fail_runs_on_exception()
{
bool ran = false;
try {
auto guard = Void::Scope::on_fail([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(ran);
}
Void::Testing::Test on_fail_release_prevents_run()
{
bool ran = false;
try {
auto guard = Void::Scope::on_fail([&] { ran = true; });
guard.release();
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(!ran);
}
@@ -0,0 +1,114 @@
#include <Void/StateMachine/StateMachine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
// ── Simple traffic-light FSM ──────────────────────────────────────────────────
struct Red {};
struct Green {};
struct Yellow {};
struct GoEvent {};
struct SlowEvent {};
struct StopEvent {};
using TrafficLight = Void::StateMachine::StateMachine<
std::tuple<Red, Green, Yellow>,
std::tuple<
Void::StateMachine::Transition<Red, GoEvent, Green>,
Void::StateMachine::Transition<Green, SlowEvent, Yellow>,
Void::StateMachine::Transition<Yellow, StopEvent, Red>
>>;
Void::Testing::Test initial_state_is_first()
{
TrafficLight fsm;
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test valid_transition_taken()
{
TrafficLight fsm;
const bool ok = fsm.send(GoEvent{});
Void::Testing::expect(ok);
Void::Testing::expect(fsm.is<Green>());
}
Void::Testing::Test invalid_event_not_taken()
{
TrafficLight fsm;
const bool ok = fsm.send(StopEvent{}); // Red has no StopEvent arc
Void::Testing::expect(!ok);
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test full_cycle()
{
TrafficLight fsm;
Void::Testing::expect(fsm.send(GoEvent{}));
Void::Testing::expect(fsm.send(SlowEvent{}));
Void::Testing::expect(fsm.send(StopEvent{}));
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test entry_callback_fired()
{
TrafficLight fsm;
bool fired = false;
fsm.on_enter<Green>([&](const Green&) { fired = true; });
fsm.send(GoEvent{});
Void::Testing::expect(fired);
}
Void::Testing::Test exit_callback_fired()
{
TrafficLight fsm;
bool fired = false;
fsm.on_exit<Red>([&](const Red&) { fired = true; });
fsm.send(GoEvent{});
Void::Testing::expect(fired);
}
Void::Testing::Test callbacks_not_fired_on_invalid_transition()
{
TrafficLight fsm;
bool entry = false, exit_cb = false;
fsm.on_enter<Green>([&](const Green&) { entry = true; });
fsm.on_exit<Red>([&](const Red&) { exit_cb = true; });
fsm.send(StopEvent{}); // invalid from Red
Void::Testing::expect(!entry);
Void::Testing::expect(!exit_cb);
}
Void::Testing::Test state_index_matches_variant_order()
{
TrafficLight fsm;
Void::Testing::expect_eq(fsm.state_index(), std::size_t{0}); // Red is first
fsm.send(GoEvent{});
Void::Testing::expect_eq(fsm.state_index(), std::size_t{1}); // Green is second
}
// ── Two-state toggle ──────────────────────────────────────────────────────────
struct Off {};
struct On {};
struct Toggle {};
using Switch = Void::StateMachine::StateMachine<
std::tuple<Off, On>,
std::tuple<
Void::StateMachine::Transition<Off, Toggle, On>,
Void::StateMachine::Transition<On, Toggle, Off>
>>;
Void::Testing::Test toggle_back_and_forth()
{
Switch sw;
Void::Testing::expect(sw.is<Off>());
sw.send(Toggle{});
Void::Testing::expect(sw.is<On>());
sw.send(Toggle{});
Void::Testing::expect(sw.is<Off>());
}
+200
View File
@@ -0,0 +1,200 @@
#include <Void/TypeList/TypeList.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstddef>
#include <string>
#include <type_traits>
using namespace Void::TypeList;
using List3 = TypeList<int, float, double>;
using Empty = TypeList<>;
// ── size_of ───────────────────────────────────────────────────────────────────
Void::Testing::Test size_of_empty()
{
static_assert(size_of<Empty> == 0);
Void::Testing::expect_eq(size_of<Empty>, std::size_t{0});
}
Void::Testing::Test size_of_nonempty()
{
static_assert(size_of<List3> == 3);
Void::Testing::expect_eq(size_of<List3>, std::size_t{3});
}
// ── at ────────────────────────────────────────────────────────────────────────
Void::Testing::Test at_first()
{
static_assert(std::is_same_v<at<0, List3>, int>);
Void::Testing::expect(true);
}
Void::Testing::Test at_last()
{
static_assert(std::is_same_v<at<2, List3>, double>);
Void::Testing::expect(true);
}
// ── front / back ─────────────────────────────────────────────────────────────
Void::Testing::Test front_of_list()
{
static_assert(std::is_same_v<front<List3>, int>);
Void::Testing::expect(true);
}
Void::Testing::Test back_of_list()
{
static_assert(std::is_same_v<back<List3>, double>);
Void::Testing::expect(true);
}
// ── contains ─────────────────────────────────────────────────────────────────
Void::Testing::Test contains_present()
{
static_assert(contains<float, List3>);
Void::Testing::expect(true);
}
Void::Testing::Test contains_absent()
{
static_assert(!contains<char, List3>);
Void::Testing::expect(true);
}
// ── index_of ─────────────────────────────────────────────────────────────────
Void::Testing::Test index_of_first()
{
static_assert(index_of<int, List3> == 0);
Void::Testing::expect_eq(index_of<int, List3>, std::size_t{0});
}
Void::Testing::Test index_of_last()
{
static_assert(index_of<double, List3> == 2);
Void::Testing::expect_eq(index_of<double, List3>, std::size_t{2});
}
// ── push_front / push_back ───────────────────────────────────────────────────
Void::Testing::Test push_front_adds_at_head()
{
using L = push_front<char, List3>;
static_assert(std::is_same_v<front<L>, char>);
static_assert(size_of<L> == 4);
Void::Testing::expect(true);
}
Void::Testing::Test push_back_adds_at_tail()
{
using L = push_back<char, List3>;
static_assert(std::is_same_v<back<L>, char>);
static_assert(size_of<L> == 4);
Void::Testing::expect(true);
}
// ── pop_front ─────────────────────────────────────────────────────────────────
Void::Testing::Test pop_front_removes_head()
{
using L = pop_front<List3>;
static_assert(std::is_same_v<front<L>, float>);
static_assert(size_of<L> == 2);
Void::Testing::expect(true);
}
// ── concat ───────────────────────────────────────────────────────────────────
Void::Testing::Test concat_two_lists()
{
using A = TypeList<int, float>;
using B = TypeList<double, char>;
using C = concat<A, B>;
static_assert(size_of<C> == 4);
static_assert(std::is_same_v<at<2, C>, double>);
Void::Testing::expect(true);
}
// ── filter ───────────────────────────────────────────────────────────────────
Void::Testing::Test filter_keeps_matching()
{
using L = filter<std::is_integral, TypeList<int, float, char, double>>;
static_assert(size_of<L> == 2);
static_assert(std::is_same_v<at<0, L>, int>);
static_assert(std::is_same_v<at<1, L>, char>);
Void::Testing::expect(true);
}
// ── transform ────────────────────────────────────────────────────────────────
Void::Testing::Test transform_applies_to_all()
{
using L = transform<std::add_pointer, TypeList<int, float>>;
static_assert(std::is_same_v<at<0, L>, int*>);
static_assert(std::is_same_v<at<1, L>, float*>);
Void::Testing::expect(true);
}
// ── reverse ──────────────────────────────────────────────────────────────────
Void::Testing::Test reverse_flips_order()
{
using R = reverse<List3>;
static_assert(std::is_same_v<at<0, R>, double>);
static_assert(std::is_same_v<at<2, R>, int>);
Void::Testing::expect(true);
}
// ── unique ───────────────────────────────────────────────────────────────────
Void::Testing::Test unique_removes_duplicates()
{
using L = TypeList<int, float, int, double, float>;
using U = unique<L>;
static_assert(size_of<U> == 3);
static_assert(std::is_same_v<at<0, U>, int>);
Void::Testing::expect(true);
}
// ── for_each ─────────────────────────────────────────────────────────────────
Void::Testing::Test for_each_visits_all_types()
{
std::size_t count = 0;
for_each<TypeList<int, float, double>>(
[&]<typename T>() { ++count; });
Void::Testing::expect_eq(count, std::size_t{3});
}
// ── all_of / any_of / none_of ────────────────────────────────────────────────
Void::Testing::Test all_of_true()
{
static_assert(all_of<std::is_integral, TypeList<int, char, long>>);
Void::Testing::expect(true);
}
Void::Testing::Test all_of_false()
{
static_assert(!all_of<std::is_integral, TypeList<int, float>>);
Void::Testing::expect(true);
}
Void::Testing::Test any_of_true()
{
static_assert(any_of<std::is_floating_point, TypeList<int, float>>);
Void::Testing::expect(true);
}
Void::Testing::Test none_of_true()
{
static_assert(none_of<std::is_pointer, TypeList<int, float>>);
Void::Testing::expect(true);
}