util/util.hpp

127 lines
3.4 KiB
C++
Raw Normal View History

2020-04-14 12:55:53 +02:00
#pragma once
2020-05-11 14:32:11 +02:00
#include <stdint.h>
2020-04-14 12:55:53 +02:00
#include "../type/type.hpp"
namespace util {
// <memory>
// clang-format off
template <typename T>
type::enable_if_t<type::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>
type::enable_if_t<!type::is_object_v<T>, T *> addressof(T &arg) noexcept
{
return &arg;
}
// <utility>
template <typename T> type::add_rvalue_reference_t<T> declval() noexcept;
2020-04-14 13:09:41 +02:00
template <typename T>
inline constexpr type::remove_reference_t<T> &&move(T &&t) noexcept
{
return static_cast<type::remove_reference_t<T> &&>(t);
}
2020-04-14 12:55:53 +02:00
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);
}
2020-05-11 14:32:11 +02:00
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...>;
2020-05-11 14:32:11 +02:00
namespace detail {
template <typename T, typename Seq1, typename Seq2> struct concat;
2020-05-11 14:32:11 +02:00
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)...>
{};
2020-05-11 14:32:11 +02:00
// 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>
{};
2020-05-11 14:32:11 +02:00
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> {};
2020-05-11 14:32:11 +02:00
} // 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>;
2020-05-11 14:32:11 +02:00
template <typename... T> using index_sequence_for = make_index_sequence<sizeof...(T)>;
2020-05-11 14:32:11 +02:00
// 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 {}; }
2020-05-11 14:32:11 +02:00
} // 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...>)
2020-05-11 17:49:00 +02:00
{
if constexpr (type::is_void_v<type::invoke_result_t<Fn, type::integral_constant<size_t, 0>>>) {
(func(type::integral_constant<size_t, Ints>{}), ...);
} else {
if ((func(type::integral_constant<size_t, Ints>{}) && ...))
return true;
return false;
}
2020-05-11 17:49:00 +02:00
}
// clang-format on
2020-04-14 12:55:53 +02:00
} // namespace util