Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
constant_sequence_accessor.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_CONSTANT_SEQUENCE_ACCESSOR_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_CONSTANT_SEQUENCE_ACCESSOR_HPP_
7
11#include "arene/base/stdlib_choice/cstddef.hpp"
12#include "arene/base/stdlib_choice/enable_if.hpp"
13#include "arene/base/stdlib_choice/is_abstract.hpp"
14#include "arene/base/stdlib_choice/is_convertible.hpp"
15#include "arene/base/stdlib_choice/is_object.hpp"
16
17namespace arene {
18namespace base {
19namespace constant_sequence_accessor_detail {
20
21/// @brief Helper trait to check if @c Sequence has a static @c values() member. The value is @c true if it does, @c
22/// false otherwise
23/// @tparam Sequence The type to check
24template <typename Sequence, typename = constraints<>>
25extern constexpr bool has_values_member_v = false;
26
27/// @brief Helper trait to check if @c Sequence has a static @c values() member. The value is @c true if it does, @c
28/// false otherwise
29/// @tparam Sequence The type to check
30template <typename Sequence>
31extern constexpr bool has_values_member_v<Sequence, constraints<decltype(Sequence::values())>> = true;
32
33/// @brief Array of elements for the sequence type @c Sequence
34/// @tparam Sequence The type of the sequence
35/// @pre Requires that @c Sequence has a type alias @c value_type for the element type, a static member function @c
36/// size() for the size of the sequence that is callable in a constant expression, and a static member function @c
37/// values() that returns something convertible to an array of @c Sequence::size() elements of type @c
38/// Sequence::value_type that is callable in a constant expression
39template <typename Sequence, typename = constraints<>>
40extern constexpr array<typename Sequence::value_type, Sequence::size()> sequence_values_v{Sequence::values()};
41
42/// @brief Array of elements for a sequence type that is defined as a template with the same parameters as @c
43/// std::integer_sequence but not constrained to be integers
44/// @tparam SequenceElementType The type of the sequence element
45/// @tparam Values The values of the sequence
46template <
47 template <typename SequenceElementType, SequenceElementType... Values>
48 class SequenceTemplate,
49 typename SequenceElementType,
50 SequenceElementType... Values>
51extern constexpr array<SequenceElementType, sizeof...(Values)> sequence_values_v<
52 SequenceTemplate<SequenceElementType, Values...>,
54 std::enable_if_t<(sizeof...(Values) > 0)>,
55 std::enable_if_t<!has_values_member_v<SequenceTemplate<SequenceElementType, Values...>>>>>{Values...};
56
57} // namespace constant_sequence_accessor_detail
58
59/// @brief Accessor policy for @c mdspan which returns values from a compile time sequence
60/// @tparam Sequence A type holding the sequence of values. This must either be:
61///
62/// - an instance of a class template that has
63/// the element type as the first parameter and the elements themselves as subsequence parameters, and a member type
64/// alias @c value_type that is the type of the elements, as per @c std::integer_sequence , or
65///
66/// - it must be a class type that has a member type alias @c value_type that is the type of
67/// the elements, a static member function @c size() for the size of the sequence that is callable in a constant
68/// expression, and a static member function @c values() that returns something convertible to an array of @c
69/// Sequence::size() elements of type @c Sequence::value_type that is callable in a constant expression
70/// @tparam ElementType the type of the element. The element type of @c Sequence must be convertible to the @c
71/// ElementType , which must be an object type that is not an abstract type.
72template <typename Sequence, typename ElementType = typename Sequence::value_type>
74 public:
75 static_assert(!std::is_abstract<ElementType>::value, "ElementType must not be abstract");
76 static_assert(std::is_object<ElementType>::value, "ElementType must be an object type");
77 static_assert(
79 "Raw sequence value_type must be convertible to ElementType"
80 );
81
82 /// @brief The type of the element
83 using element_type = ElementType;
84 /// @brief The type of the data handle --- this is the index into the sequence from which to start the accesses
86 /// @brief The type of a "reference" to the element --- this is the same as the element type, so no backing data is
87 /// required, the value is returned by value
88 using reference = ElementType;
89 /// @brief The type of the accessor policy for accessing data with an offset --- this is @c constant_sequence_accessor
90 /// , since the value is constant
92
93 /// @brief Return the value for the given data handle and index. The index is ignored as the value is always constant
94 /// @param data_handle The base position into the sequence to use
95 /// @param index The index value to the chosen item from the base position
96 /// @return The fixed value from the sequence at the position <c>(data_handle+index)</c>
97 /// @pre The value of <c>(data_handle+index)</c> must be less than @c Sequence::size()
98 static constexpr auto access(data_handle_type data_handle, std::size_t index) noexcept -> reference {
99 ARENE_PRECONDITION((data_handle + index) < Sequence::size());
100 return static_cast<ElementType>(constant_sequence_accessor_detail::sequence_values_v<Sequence>[data_handle + index]
101 );
102 }
103 /// @brief Return the data handle value to use with the offset policy for the given data handle and index.
104 /// @param data_handle The base position into the sequence to use
105 /// @param index The index value to the chosen item from the base position
106 /// @return <c>(data_handle+index)</c>
107 static constexpr auto offset(data_handle_type data_handle, std::size_t index) noexcept -> data_handle_type {
108 return data_handle + index;
109 }
110};
111} // namespace base
112} // namespace arene
113
114#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_CONSTANT_SEQUENCE_ACCESSOR_HPP_
Accessor policy for mdspan which returns values from a compile time sequence.
Definition constant_sequence_accessor.hpp:73
ElementType reference
The type of a "reference" to the element — this is the same as the element type, so no backing data i...
Definition constant_sequence_accessor.hpp:88
static constexpr auto offset(data_handle_type data_handle, std::size_t index) noexcept -> data_handle_type
Return the data handle value to use with the offset policy for the given data handle and index.
Definition constant_sequence_accessor.hpp:107
std::size_t data_handle_type
The type of the data handle — this is the index into the sequence from which to start the accesses.
Definition constant_sequence_accessor.hpp:85
constant_sequence_accessor offset_policy
The type of the accessor policy for accessing data with an offset — this is constant_sequence_accesso...
Definition constant_sequence_accessor.hpp:91
ElementType element_type
The type of the element.
Definition constant_sequence_accessor.hpp:83
static constexpr auto access(data_handle_type data_handle, std::size_t index) noexcept -> reference
Return the value for the given data handle and index. The index is ignored as the value is always con...
Definition constant_sequence_accessor.hpp:98
static constexpr value_type value
Definition integral_constant.hpp:34
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41