1#ifndef LIBSBX_UTILITY_MAKE_ARRAY_HPP_
2#define LIBSBX_UTILITY_MAKE_ARRAY_HPP_
9namespace sbx::utility {
13template<
bool Condition,
typename Type, std::convertible_to<Type> Other = Type>
14constexpr auto conditional_value(Other value)
noexcept -> Type {
15 if constexpr (Condition) {
16 return static_cast<Type
>(value);
22template<
typename Type, std::size_t... Indices, std::convertible_to<Type>... Args>
23requires (
sizeof...(Args) ==
sizeof...(Indices))
24constexpr auto make_array_impl(std::index_sequence<Indices...>, Args&&... args) -> std::array<Type,
sizeof...(Indices)> {
25 return std::array<Type,
sizeof...(Indices)>{
static_cast<Type
>(
static_cast<void>(Indices), std::forward<Args>(args))... };
28template<
typename Type, std::size_t... Indices, std::convertible_to<Type> Other = Type>
29constexpr auto make_array_impl(std::index_sequence<Indices...>,
const Other& value) -> std::array<Type,
sizeof...(Indices)> {
30 return std::array<Type,
sizeof...(Indices)>{
static_cast<Type
>(
static_cast<void>(Indices), value)... };
33template<std::size_t Index,
typename Type, std::size_t... Indices, std::convertible_to<Type> Other = Type>
34requires (Index >= 0 && Index <
sizeof...(Indices))
35constexpr auto make_array_impl(std::index_sequence<Indices...>,
const Other& value) -> std::array<Type,
sizeof...(Indices)> {
36 return std::array<Type,
sizeof...(Indices)>{ conditional_value<(Indices == Index), Type>(value)... };
39template<
typename Type, std::size_t... Indices, std::convertible_to<Type> Other = Type>
40constexpr auto make_array_impl(std::index_sequence<Indices...>,
const std::array<Other,
sizeof...(Indices)>& array) -> std::array<Type,
sizeof...(Indices)> {
41 return std::array<Type,
sizeof...(Indices)>{
static_cast<Type
>(array[Indices])... };
46template<
typename Type, std::
size_t Size, std::convertible_to<Type>... Args>
47requires (
sizeof...(Args) == Size)
48constexpr auto make_array(Args&&... args) -> std::array<Type, Size> {
49 return detail::make_array_impl<Type>(std::make_index_sequence<Size>(), std::forward<Args>(args)...);
52template<
typename Type, std::
size_t Size, std::convertible_to<Type> Other = Type>
53constexpr auto make_array(
const Other& value) -> std::array<Type, Size> {
54 return detail::make_array_impl<Type>(std::make_index_sequence<Size>(), value);
57template<
typename Type, std::
size_t Size, std::
size_t Index, std::convertible_to<Type> Other = Type>
58constexpr auto make_array(
const Other& value) -> std::array<Type, Size> {
59 return detail::make_array_impl<Index, Type>(std::make_index_sequence<Size>(), value);
62template<
typename Type, std::
size_t Size, std::convertible_to<Type> Other = Type>
63constexpr auto make_array(
const std::array<Other, Size>& array) -> std::array<Type, Size> {
64 return detail::make_array_impl<Type>(std::make_index_sequence<Size>(), array);