MaxMatrix
Drive chained 8x8 LED dot matrix modules with MAX7219.
Loading...
Searching...
No Matches
array.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_ARRAY_H_
11#define MAXMATRIX_SRC_UTILITY_ARRAY_H_
12
13#include <stddef.h>
14
15#include "MaxMatrixAttributes.h"
16
17namespace max_matrix {
18
36/*
37 * MaxMatrix's own, by CodingStyle.md M-3: avr-gcc brings no <array>, and
38 * MaxMatrix depends on neither the ETL nor Utl, whose utl::array this follows
39 * (OIL 28, 41).
40 */
41template <typename T, size_t N>
42struct array {
43 // A C array of no elements is ill-formed.
44 static_assert(N > 0, "max_matrix::array needs at least one element");
45
46 using value_type = T;
47 using size_type = size_t;
48
55 T elements[N]; // NOLINT(modernize-avoid-c-arrays): T-20, what it wraps
56
58 MAXMATRIX_NODISCARD constexpr size_type size() const { return N; }
59
65 T& operator[](size_type index) { return elements[index]; }
66
68 constexpr T const& operator[](size_type index) const {
69 return elements[index];
70 }
71
74
76 MAXMATRIX_NODISCARD constexpr T const* data() const { return elements; }
77};
78
79namespace internal {
80
87template <typename T>
89
96template <typename Value>
97constexpr bool IsNegative(Value value) {
98 return value != Value{0} && !(value > Value{0});
99}
100
105template <typename T>
107
117template <typename T, typename Value, typename Braced = T>
124 static constexpr bool Fits(Value value) {
125 return static_cast<Value>(static_cast<T>(value)) == value &&
126 IsNegative(value) == IsNegative(static_cast<T>(value));
127 }
128
135 static constexpr T Of(Value value) {
136 return Fits(value) ? static_cast<T>(value) : ValueOutOfRange<T>();
137 }
138};
139
145template <typename T, typename Value>
146struct ArrayElement<T, Value, decltype(T{DeclVal<Value>()})> {
152 static constexpr T Of(Value const& value) { return value; }
153};
154
155} // namespace internal
156
184template <typename T, typename... Values>
185constexpr array<T, sizeof...(Values)> make_array(Values... values) {
186 return {{internal::ArrayElement<T, Values>::Of(values)...}};
187}
188
189} // namespace max_matrix
190
191#endif // MAXMATRIX_SRC_UTILITY_ARRAY_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
T ValueOutOfRange()
Declared only, so that make_array() stops the build with its name.
constexpr array< T, sizeof...(Values)> make_array(Values... values)
Returns an array of T of its values, with the size taken from them.
Definition array.h:185
constexpr bool IsNegative(Value value)
Returns whether value is below 0, without comparing a type that has no sign with 0,...
Definition array.h:97
T && DeclVal()
Declared only: in a decltype, a value of type T, where nothing is evaluated, as std::declval(),...
A C array of N elements that knows its size.
Definition array.h:42
MAXMATRIX_NODISCARD constexpr size_type size() const
Returns the number of elements, N.
Definition array.h:58
T value_type
Definition array.h:46
T & operator[](size_type index)
Returns the element at index, unchecked.
Definition array.h:65
T elements[N]
The elements.
Definition array.h:55
MAXMATRIX_NODISCARD T * data()
Returns the address of the first element, as a C array gives it.
Definition array.h:73
size_t size_type
Definition array.h:47
constexpr T const & operator[](size_type index) const
Returns the element at index, unchecked.
Definition array.h:68
MAXMATRIX_NODISCARD constexpr T const * data() const
Returns the address of the first element, as a C array gives it.
Definition array.h:76
static constexpr T Of(Value const &value)
Returns value as an element.
Definition array.h:152
Converts a value of make_array() that would narrow, a number, into an element.
Definition array.h:118
static constexpr T Of(Value value)
Returns value as an element, or stops the build if it does not fit.
Definition array.h:135
static constexpr bool Fits(Value value)
Returns whether value is the same as an element as it was.
Definition array.h:124