util/util.hpp
2020-05-16 17:29:54 +02:00

111 lines
2.7 KiB
C++

#pragma once
#include <stdint.h>
#include "../type/type.hpp"
namespace util {
template <typename T>
inline constexpr type::remove_reference_t<T> &&move(T &&t) noexcept
{
return static_cast<type::remove_reference_t<T> &&>(t);
}
template <typename T>
inline constexpr T &&forward(type::remove_reference_t<T> &t) noexcept
{
return static_cast<T &&>(t);
}
template <typename T>
inline constexpr T &&forward(type::remove_reference_t<T> &&t) noexcept
{
static_assert(!type::is_lvalue_reference_v<T>, "Can not forward an rvalue as an lvalue.");
return static_cast<T &&>(t);
}
template <typename T, T... Ints>
struct integer_sequence {
using __type = integer_sequence;
using value_type = T;
static constexpr size_t size() noexcept
{
return sizeof...(Ints);
}
};
template <size_t... Ints>
using index_sequence = integer_sequence<size_t, Ints...>;
namespace detail {
template <typename T, typename Seq1, typename Seq2>
struct concat;
template <typename T, T... Ints1, T... Ints2>
struct concat<T, integer_sequence<T, Ints1...>, integer_sequence<T, Ints2...>>
: integer_sequence<T, Ints1..., (sizeof...(Ints1) + Ints2)...> {
};
// uint64_t because this must be able to hold all possible sizes and cannot be T, because then the specialization for 0
// and 1 are not possible
template <typename T, uint64_t N>
struct gen_integer_sequence : detail::concat<T, typename gen_integer_sequence<T, N / 2>::__type,
typename gen_integer_sequence<T, N - N / 2>::__type> {
};
template <typename T>
struct gen_integer_sequence<T, 0> : integer_sequence<T> {
};
template <typename T>
struct gen_integer_sequence<T, 1> : integer_sequence<T, 0> {
};
} // namespace detail
template <typename T, T N>
struct make_integer_sequence : detail::gen_integer_sequence<T, N> {
static_assert(N >= 0, "Integer sequence cannot be negative");
};
template <size_t N>
using make_index_sequence = make_integer_sequence<size_t, N>;
template <typename... T>
using index_sequence_for = make_index_sequence<sizeof...(T)>;
// Not part of <utility>, but very useful
namespace detail {
template <typename T, T Offset, T... Ints>
inline constexpr integer_sequence<T, (Offset + Ints)...> add_offset(integer_sequence<T, Ints...>)
{
return {};
}
} // namespace detail
template <typename T, T Offset, T N>
inline constexpr auto make_offset_integer_sequence()
{
return detail::add_offset<T, Offset>(make_integer_sequence<T, N>{});
}
template <size_t Offset, size_t N>
inline constexpr auto make_offset_index_sequence()
{
return make_offset_integer_sequence<size_t, Offset, N>();
}
template <class Fn, size_t... Ints>
void for_constexpr(Fn &&func, index_sequence<Ints...>)
{
(func(type::integral_constant<size_t, Ints>{}), ...);
}
} // namespace util