MaxMatrix
Drive chained 8x8 LED dot matrix modules with MAX7219.
Loading...
Searching...
No Matches
span.h
Go to the documentation of this file.
1// Copyright 2026 Andreas Burnickl
2// SPDX-License-Identifier: GPL-3.0-or-later
3
9
10#ifndef MAXMATRIX_SRC_UTILITY_SPAN_H_
11#define MAXMATRIX_SRC_UTILITY_SPAN_H_
12
13#include <stddef.h>
14
15#include "MaxMatrixAttributes.h"
16#include "Utility/array.h"
17
18namespace max_matrix {
19
20namespace internal {
21
28template <typename To>
29constexpr To* ConvertPointer(To* pointer) {
30 return pointer;
31}
32
34template <typename T, typename Element>
36 decltype(ConvertPointer<T>(static_cast<Element*>(nullptr)));
37
38} // namespace internal
39
59/*
60 * MaxMatrix's own, by CodingStyle.md M-3: avr-gcc brings no <span>, which is
61 * C++20, and MaxMatrix depends on neither the ETL nor Utl (OIL 28, 41).
62 */
63template <typename T>
64class span {
65 public:
66 using element_type = T;
67 using size_type = size_t;
68
70 constexpr span() = default;
71
78 constexpr span(T* data, size_type size) : data_{data}, size_{size} {}
79
86 template <size_t N>
87 // NOLINTNEXTLINE(modernize-avoid-c-arrays): T-20, a text in quotes is one
88 constexpr span(T (&elements)[N]) // NOLINT(runtime/explicit): as std
89 : data_{elements}, size_{N} {}
90
98 template <typename Element, size_t N,
100 constexpr span(array<Element, N>& elements) // NOLINT(runtime/explicit): std
101 : data_{elements.data()}, size_{N} {}
102
104 template <typename Element, size_t N,
106 constexpr span( // NOLINT(runtime/explicit): as std
107 array<Element, N> const& elements)
108 : data_{elements.data()}, size_{N} {}
109
117 template <typename Element,
119 constexpr span(span<Element> const& other) // NOLINT(runtime/explicit): std
120 : data_{other.data()}, size_{other.size()} {}
121
123 MAXMATRIX_NODISCARD constexpr T* data() const { return data_; }
124
126 MAXMATRIX_NODISCARD constexpr size_type size() const { return size_; }
127
133 constexpr T& operator[](size_type index) const {
134 // T-20: the one place that reaches an element through its address.
135 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
136 return data_[index];
137 }
138
144 MAXMATRIX_NODISCARD constexpr span first(size_type count) const {
145 return span{data_, count};
146 }
147
153 MAXMATRIX_NODISCARD constexpr span subspan(size_type offset) const {
154 // T-20: the one place that moves an address.
155 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
156 return span{data_ + offset, size_ - offset};
157 }
158
159 private:
160 T* data_{nullptr};
161 size_type size_{0};
162};
163
164} // namespace max_matrix
165
166#endif // MAXMATRIX_SRC_UTILITY_SPAN_H_
The attributes of a later standard, as macros, where the compiler has them.
#define MAXMATRIX_NODISCARD
Marks a function whose result must not be dropped: the compiler warns where it is.
Definition MaxMatrixAttributes.h:37
A C array that knows its size, as std::array, and make_array(), which takes the size from the values.
constexpr span()=default
Makes a span of no elements.
constexpr span(array< Element, N > const &elements)
Makes a span of an array.
Definition span.h:106
size_t size_type
Definition span.h:67
constexpr T & operator[](size_type index) const
Returns the element at index, unchecked.
Definition span.h:133
constexpr span(T *data, size_type size)
Makes a span of size elements from data on.
Definition span.h:78
constexpr span(span< Element > const &other)
Makes a span of what other views, as one of uint8_t into one of uint8_t const.
Definition span.h:119
MAXMATRIX_NODISCARD constexpr span subspan(size_type offset) const
Returns a span of the elements from offset on.
Definition span.h:153
constexpr span(array< Element, N > &elements)
Makes a span of an array.
Definition span.h:100
T element_type
Definition span.h:66
MAXMATRIX_NODISCARD constexpr size_type size() const
Returns the number of elements.
Definition span.h:126
constexpr span(T(&elements)[N])
Makes a span of a C array, as a text in quotes is.
Definition span.h:88
MAXMATRIX_NODISCARD constexpr span first(size_type count) const
Returns a span of the first count elements.
Definition span.h:144
MAXMATRIX_NODISCARD constexpr T * data() const
Returns the address of the first element.
Definition span.h:123
constexpr To * ConvertPointer(To *pointer)
Returns pointer; in a decltype, whether a pointer converts to a To* without a cast,...
Definition span.h:29
decltype(ConvertPointer< T >(static_cast< Element * >(nullptr))) IfPointerConverts
Names a type only if an Element* converts to a T*.
Definition span.h:35
A C array of N elements that knows its size.
Definition array.h:42