127 lines
3.4 KiB
C++
127 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "type.hpp"
|
|
|
|
namespace util {
|
|
|
|
// <memory>
|
|
|
|
// clang-format off
|
|
|
|
template <typename T>
|
|
util::enable_if_t<util::is_object_v<T>, T *> addressof(T &arg) noexcept
|
|
{
|
|
return reinterpret_cast<T *>(&const_cast<char &>(reinterpret_cast<const volatile char &>(arg)));
|
|
}
|
|
|
|
template <typename T>
|
|
util::enable_if_t<!util::is_object_v<T>, T *> addressof(T &arg) noexcept
|
|
{
|
|
return &arg;
|
|
}
|
|
|
|
// <utility>
|
|
|
|
template <typename T> util::add_rvalue_reference_t<T> declval() noexcept;
|
|
|
|
template <typename T>
|
|
inline constexpr util::remove_reference_t<T> &&move(T &&t) noexcept
|
|
{
|
|
return static_cast<util::remove_reference_t<T> &&>(t);
|
|
}
|
|
|
|
template <typename T>
|
|
inline constexpr T &&forward(util::remove_reference_t<T> &t) noexcept
|
|
{
|
|
return static_cast<T &&>(t);
|
|
}
|
|
|
|
template <typename T>
|
|
inline constexpr T &&forward(util::remove_reference_t<T> &&t) noexcept
|
|
{
|
|
static_assert(!util::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 <typename Fn, size_t... Ints>
|
|
auto for_constexpr(Fn &&func, index_sequence<Ints...>)
|
|
{
|
|
if constexpr (util::is_void_v<util::invoke_result_t<Fn, util::integral_constant<size_t, 0>>>) {
|
|
(func(util::integral_constant<size_t, Ints>{}), ...);
|
|
} else {
|
|
if ((func(util::integral_constant<size_t, Ints>{}) && ...))
|
|
return true;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// clang-format on
|
|
|
|
} // namespace util
|