tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
integral_constant_like.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MDSPAN_INTEGRAL_CONSTANT_LIKE_HPP
4#define TETL_MDSPAN_INTEGRAL_CONSTANT_LIKE_HPP
5
14
15namespace etl::detail {
16
17template <typename T>
18concept pair_like = true;
19
20template <typename T, typename IndexType>
21concept index_pair_like = //
22 pair_like<T> //
23 and etl::convertible_to<etl::tuple_element_t<0, T>, IndexType> //
24 and etl::convertible_to<etl::tuple_element_t<1, T>, IndexType> //
25 ;
26
27template <typename T>
28concept integral_constant_like = //
29 etl::is_integral_v<decltype(T::value)> //
30 and not etl::is_same_v<bool, etl::remove_const_t<decltype(T::value)>> //
31 and etl::convertible_to<T, decltype(T::value)> //
32 /* and etl::equality_comparable_with<T, decltype(T::value)> */ //
33 and etl::bool_constant<T() == T::value>::value //
34 and etl::bool_constant<static_cast<decltype(T::value)>(T()) == T::value>::value //
35 ;
36
37template <typename T>
38[[nodiscard]] constexpr auto de_ice(T val) -> T
39{
40 return val;
41}
42
43template <integral_constant_like T>
44[[nodiscard]] constexpr auto de_ice(T /*unused*/)
45{
46 return T::value;
47}
48
49template <etl::size_t K>
50[[nodiscard]] constexpr auto nth_slice_specifier(auto first, auto... rest)
51{
52 if constexpr (K == 0) {
53 return first;
54 } else {
55 nth_slice_specifier<K - 1>(rest...);
56 }
57}
58
59template <typename IndexType, etl::size_t K, typename... SliceSpecifiers>
60[[nodiscard]] constexpr auto submdspan_first(SliceSpecifiers... slices) -> IndexType
61{
62 auto sk = nth_slice_specifier<K>(slices...);
63 using Sk = decltype(sk);
64
65 if constexpr (etl::convertible_to<Sk, IndexType>) {
66 return static_cast<IndexType>(sk);
67 } else if constexpr (index_pair_like<Sk, IndexType>) {
68 return static_cast<IndexType>(get<0>(sk));
69 } else if constexpr (is_strided_slice<Sk>) {
70 return static_cast<IndexType>(de_ice(sk.offset));
71 } else {
72 return static_cast<IndexType>(0);
73 }
74}
75
76template <etl::size_t K, typename Extents, typename... SliceSpecifiers>
77[[nodiscard]] constexpr auto submdspan_last(Extents const& src, SliceSpecifiers... slices)
78{
79 auto sk = nth_slice_specifier<K>(slices...);
80 using Sk = decltype(sk);
81 using IndexType = typename Extents::index_type;
82
83 if constexpr (etl::convertible_to<Sk, IndexType>) {
84 return static_cast<IndexType>(de_ice(sk)) + IndexType(1);
85 } else if constexpr (index_pair_like<Sk, IndexType>) {
86 return static_cast<IndexType>(get<1>(sk));
87 } else if constexpr (is_strided_slice<Sk>) {
88 return static_cast<IndexType>(de_ice(sk.offset) + de_ice(sk.extent));
89 } else {
90 return static_cast<IndexType>(src.extent(K));
91 }
92}
93
94// template <typename IndexType, size_t N, typename... SliceSpecifiers>
95// [[nodiscard]] constexpr auto src_indices(array<IndexType, N> const& indices, SliceSpecifiers...
96// slices)
97// -> array<IndexType, sizeof...(SliceSpecifiers)>
98// {
99// }
100
101} // namespace etl::detail
102
103#endif // TETL_MDSPAN_INTEGRAL_CONSTANT_LIKE_HPP
auto val(pin_number pin) -> etl::uint16_t
Definition gpio.hpp:37
constexpr bool is_integral_v
Definition is_integral.hpp:28
typename remove_const< T >::type remove_const_t
Definition remove_const.hpp:23
constexpr bool is_same_v
Definition is_same.hpp:11
integral_constant< bool, B > bool_constant
Definition bool_constant.hpp:11
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14