Zepha/src/util/TupleSub.h

42 lines
1.4 KiB
C++

#pragma once
namespace {
/**
* Recursively generates a tuple out of a subsection of another tuple.
*
* @tparam T - The base tuple to pull the types from.
* @tparam N - The current index of the type to add to the resultant tuple.
* @tparam M - The end index of types to pull.
* @tparam Args - A pack of the args to add into the resultant tuple.
* @returns the subsection of the tuple T as specified by M and N.
*/
template<typename T, usize N, usize M, typename... Args>
inline constexpr auto tuple_sub_impl() {
if constexpr (N <= M) return tuple_sub_impl<T, N + 1, M, Args..., std::tuple_element_t<N, T>>();
else return std::tuple<Args...> {};
}
}
/**
* Struct containing a subsection of a tuple T and start index N and end index M.
*
* @tparam T - The tuple to take a subsection of.
* @tparam N - The start index of the tuple types to include.
* @tparam M - The end index of the tuple types to include - defaults to the tuple's size.
*/
template <typename T, usize N, usize M = std::tuple_size_v<T> - 1>
struct tuple_sub {
/** The value of the subsection of the tuple. */
typedef decltype(tuple_sub_impl<T, N, M>()) value_type;
};
/**
* Shorthand for the value type of tuple_sub with the provided template arguments.
*/
template <typename T, usize N, usize M = std::tuple_size_v<T> - 1>
using tuple_sub_t = typename tuple_sub<T, N, M>::value_type;