File: to_std_array.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (57 lines) | stat: -rw-r--r-- 1,465 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_
#define RFL_INTERNAL_TO_STD_ARRAY_HPP_

#include <array>
#include <cstdint>
#include <type_traits>

namespace rfl::internal {

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 = typename 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