draconisplusplus/include/rfl/internal/to_std_array.hpp

58 lines
1.5 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_
#define RFL_INTERNAL_TO_STD_ARRAY_HPP_
#include <array>
#include <cstdint>
#include <type_traits>
namespace rfl::internal {
2024-06-08 14:10:59 -04:00
template <class T>
struct StdArrayType {
using Type = T;
};
template <class T, size_t _n>
struct StdArrayType<T[_n]> {
using Type =
std::array<typename StdArrayType<std::remove_cvref_t<T>>::Type, _n>;
using ValueType = std::remove_cvref_t<T>;
constexpr static size_t size = _n;
};
template <class T>
using to_std_array_t = StdArrayType<T>::Type;
template <class T>
auto to_std_array(T&& _t) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_array_v<Type>) {
constexpr size_t n = StdArrayType<Type>::size;
const auto fct = [&]<std::size_t... _i>(std::index_sequence<_i...>) {
return to_std_array_t<Type>({to_std_array(
std::forward<typename StdArrayType<Type>::ValueType>(_t[_i]))...});
};
return fct(std::make_index_sequence<n>());
} else {
return std::forward<T>(_t);
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
template <class T>
auto to_std_array(const T& _t) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_array_v<Type>) {
constexpr size_t n = StdArrayType<Type>::size;
const auto fct = [&]<std::size_t... _i>(std::index_sequence<_i...>) {
return to_std_array_t<Type>({to_std_array(_t[_i])...});
};
return fct(std::make_index_sequence<n>());
} else {
return _t;
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
} // namespace rfl::internal
2024-05-31 22:59:00 -04:00
#endif