util/util.hpp

52 lines
1.2 KiB
C++
Raw Normal View History

2020-04-14 12:55:53 +02:00
#pragma once
2022-05-29 16:16:19 +02:00
#include <type_traits>
#include <utility>
2020-05-11 14:32:11 +02:00
2022-05-29 16:16:19 +02:00
#include <cstddef>
#include <cstdint>
2020-04-14 12:55:53 +02:00
namespace util {
2020-05-11 14:32:11 +02:00
namespace detail {
template <typename T, T Offset, T... Ints>
2022-05-29 16:16:19 +02:00
inline constexpr std::integer_sequence<T, (Offset + Ints)...> add_offset(std::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()
{
2022-05-29 16:16:19 +02:00
return detail::add_offset<T, Offset>(std::make_integer_sequence<T, N>{});
2020-05-11 14:32:11 +02:00
}
2022-05-29 16:16:19 +02:00
template <std::size_t Offset, std::size_t N>
2020-05-11 14:32:11 +02:00
inline constexpr auto make_offset_index_sequence()
{
2022-05-29 16:16:19 +02:00
return make_offset_integer_sequence<std::size_t, Offset, N>();
2020-05-11 14:32:11 +02:00
}
2022-05-29 16:16:19 +02:00
template <typename Fn, std::size_t... Ints>
constexpr auto for_constexpr(Fn &&func, std::index_sequence<Ints...>)
2020-05-11 17:49:00 +02:00
{
2022-05-29 16:16:19 +02:00
if constexpr (std::is_void_v<std::invoke_result_t<Fn, std::integral_constant<std::size_t, 0>>>) {
(func(std::integral_constant<std::size_t, Ints>{}), ...);
} else {
2022-05-29 16:16:19 +02:00
if ((func(std::integral_constant<std::size_t, Ints>{}) && ...))
return true;
return false;
}
2020-05-11 17:49:00 +02:00
}
2022-05-29 16:16:19 +02:00
template <typename...>
struct always_false : std::false_type {
};
template <typename... Ts>
inline constexpr auto always_false_v = always_false<Ts...>::value;
2020-04-14 12:55:53 +02:00
} // namespace util