mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Made sure that to_named_tuple (and, by extension, rfl::replace) works properly
This commit is contained in:
@@ -1,24 +1,57 @@
|
||||
#ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_
|
||||
#define RFL_INTERNAL_TO_STD_ARRAY_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl::internal {
|
||||
|
||||
template <class T>
|
||||
struct to_std_array {
|
||||
using type = T;
|
||||
struct StdArrayType {
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
template <class T, std::size_t _n>
|
||||
struct to_std_array<T[_n]> {
|
||||
using type = std::array<typename to_std_array<std::remove_cvref_t<T>>::type, _n>;
|
||||
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 = to_std_array<T>::type;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user