2020-12-30 08:40:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-30 08:40:21 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename... Types>
|
|
|
|
struct TypeList;
|
|
|
|
|
|
|
|
template<unsigned Index, typename List>
|
|
|
|
struct TypeListElement;
|
|
|
|
|
|
|
|
template<unsigned Index, typename Head, typename... Tail>
|
|
|
|
struct TypeListElement<Index, TypeList<Head, Tail...>>
|
|
|
|
: TypeListElement<Index - 1, TypeList<Tail...>> {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Head, typename... Tail>
|
|
|
|
struct TypeListElement<0, TypeList<Head, Tail...>> {
|
|
|
|
using Type = Head;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Types>
|
|
|
|
struct TypeList {
|
|
|
|
static constexpr unsigned size = sizeof...(Types);
|
|
|
|
|
|
|
|
template<unsigned N>
|
|
|
|
using Type = typename TypeListElement<N, TypeList<Types...>>::Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct TypeWrapper {
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
2021-04-29 23:23:52 +03:00
|
|
|
template<typename List, typename F, unsigned... Indices>
|
|
|
|
constexpr void for_each_type_impl(F&& f, IndexSequence<Indices...>)
|
2020-12-30 08:40:21 +03:00
|
|
|
{
|
2021-04-29 23:23:52 +03:00
|
|
|
(forward<F>(f)(TypeWrapper<typename List::template Type<Indices>> {}), ...);
|
2020-12-30 08:40:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename List, typename F>
|
|
|
|
constexpr void for_each_type(F&& f)
|
|
|
|
{
|
|
|
|
for_each_type_impl<List>(forward<F>(f), MakeIndexSequence<List::size> {});
|
|
|
|
}
|
|
|
|
|
2021-04-29 23:23:52 +03:00
|
|
|
template<typename ListA, typename ListB, typename F, unsigned... Indices>
|
|
|
|
constexpr void for_each_type_zipped_impl(F&& f, IndexSequence<Indices...>)
|
2020-12-30 08:40:21 +03:00
|
|
|
{
|
2021-04-29 23:23:52 +03:00
|
|
|
(forward<F>(f)(TypeWrapper<typename ListA::template Type<Indices>> {}, TypeWrapper<typename ListB::template Type<Indices>> {}), ...);
|
2020-12-30 08:40:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ListA, typename ListB, typename F>
|
|
|
|
constexpr void for_each_type_zipped(F&& f)
|
|
|
|
{
|
|
|
|
static_assert(ListA::size == ListB::size, "Can't zip TypeLists that aren't the same size!");
|
|
|
|
for_each_type_zipped_impl<ListA, ListB>(forward<F>(f), MakeIndexSequence<ListA::size> {});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-12-30 08:40:21 +03:00
|
|
|
using AK::for_each_type;
|
|
|
|
using AK::for_each_type_zipped;
|
|
|
|
using AK::TypeList;
|
|
|
|
using AK::TypeListElement;
|
|
|
|
using AK::TypeWrapper;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|