FastLED 3.9.15
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1// allow-include-after-namespace
2
3#pragma once
4
5#include <string.h>
6
7#include "fl/inplacenew.h"
8#include "fl/memfill.h"
9#include "fl/type_traits.h"
10#include "fl/bit_cast.h"
11
12#include "fl/initializer_list.h"
13
14
15
16namespace fl {
17
27template <typename T, fl::size N> class array {
28 public:
29 // Standard container type definitions
30 using value_type = T;
31 using size_type = fl::size;
34 using const_reference = const value_type &;
36 using const_pointer = const value_type *;
39
40 // Default constructor - elements are default-initialized
41 array() = default;
42
43 // Fill constructor
44 explicit array(const T &value) {
45 // std::fill_n(begin(), N, value);
46 fill_n(data_, N, value);
47 }
48
49 // Initializer list constructor
50 array(fl::initializer_list<T> list) {
51 fl::size i = 0;
52 for (auto it = list.begin(); it != list.end() && i < N; ++it, ++i) {
53 data_[i] = *it;
54 }
55 }
56
57 // Copy constructor
58 array(const array &) = default;
59
60 // Move constructor
61 array(array &&) = default;
62
63 // Copy assignment
64 array &operator=(const array &) = default;
65
66 // Move assignment
67 array &operator=(array &&) = default;
68
69 // Element access
70 T &at(fl::size pos) {
71 if (pos >= N) {
72 return error_value();
73 }
74 return data_[pos];
75 }
76
77 const T &at(fl::size pos) const {
78 if (pos >= N) {
79 return error_value();
80 }
81 return data_[pos];
82 }
83
84 T &operator[](fl::size pos) { return data_[pos]; }
85
86 const_reference operator[](fl::size pos) const { return data_[pos]; }
87
88 T &front() { return data_[0]; }
89
90 const T &front() const { return data_[0]; }
91
92 T &back() { return data_[N - 1]; }
93
94 const T &back() const { return data_[N - 1]; }
95
96 pointer data() noexcept { return data_; }
97
98 const_pointer data() const noexcept { return data_; }
99
100 // Iterators
101 iterator begin() noexcept { return data_; }
102
103 const_iterator begin() const noexcept { return data_; }
104
105 const_iterator cbegin() const noexcept { return data_; }
106
107 iterator end() noexcept { return data_ + N; }
108
109 const_iterator end() const noexcept { return data_ + N; }
110
111 // Capacity
112 bool empty() const noexcept { return N == 0; }
113
114 fl::size size() const noexcept { return N; }
115
116 fl::size max_size() const noexcept { return N; }
117
118 // Operations
119 void fill(const T &value) {
120 for (fl::size i = 0; i < N; ++i) {
121 data_[i] = value;
122 }
123 }
124
125 void swap(array &other) {
126 for (fl::size i = 0; i < N; ++i) {
127 fl::swap(data_[i], other.data_[i]);
128 }
129 }
130
131 private:
132 static T &error_value() {
133 static T empty_value;
134 return empty_value;
135 }
136 T data_[N];
137};
138
139// Non-member functions
140template <typename T, fl::size N>
141bool operator==(const array<T, N> &lhs, const array<T, N> &rhs) {
142 // return std::equal(lhs.begin(), lhs.end(), rhs.begin());
143 for (fl::size i = 0; i < N; ++i) {
144 if (lhs[i] != rhs[i]) {
145 return false;
146 }
147 }
148}
149
150template <typename T, fl::size N>
151bool operator!=(const array<T, N> &lhs, const array<T, N> &rhs) {
152 return !(lhs == rhs);
153}
154
155template <typename T, fl::size N>
157 array<T, N> &rhs) noexcept(noexcept(lhs.swap(rhs))) {
158 lhs.swap(rhs);
159}
160
161} // namespace fl
162
163// FASTLED_STACK_ARRAY
164// An array of variable length that is allocated on the stack using
165// either alloca or a variable length array (VLA) support built into the
166// the compiler.
167// Example:
168// Instead of: int array[buff_size];
169// You'd use: FASTLED_STACK_ARRAY(int, array, buff_size);
170
171#ifndef FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION
172#if defined(__clang__) || defined(ARDUINO_GIGA_M7) || defined(ARDUINO_GIGA)
173// Clang doesn't have variable length arrays. Therefore we need to emulate them
174// using alloca. It's been found that Arduino Giga M7 also doesn't support
175// variable length arrays for some reason so we force it to emulate them as well
176// in this case.
177#define FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION 1
178#else
179// Else, assume the compiler is gcc, which has variable length arrays
180#define FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION 0
181#endif
182#endif // FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION
183
184#if !FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION
185#define FASTLED_STACK_ARRAY(TYPE, NAME, SIZE) \
186 TYPE NAME[SIZE]; \
187 fl::memfill(NAME, 0, sizeof(TYPE) * (SIZE))
188#elif __has_include(<alloca.h>)
189#include <alloca.h>
190#define FASTLED_STACK_ARRAY(TYPE, NAME, SIZE) \
191 TYPE *NAME = fl::bit_cast_ptr<TYPE>(alloca(sizeof(TYPE) * (SIZE))); \
192 fl::memfill(NAME, 0, sizeof(TYPE) * (SIZE))
193#elif __has_include(<cstdlib>)
194#include <cstdlib> // ok include
195#define FASTLED_STACK_ARRAY(TYPE, NAME, SIZE) \
196 TYPE *NAME = fl::bit_cast_ptr<TYPE>(alloca(sizeof(TYPE) * (SIZE))); \
197 fl::memfill(NAME, 0, sizeof(TYPE) * (SIZE))
198#else
199#error "Compiler does not allow variable type arrays."
200#endif
uint8_t pos
Definition Blur.ino:11
T & front()
Definition array.h:88
T & at(fl::size pos)
Definition array.h:70
array(array &&)=default
value_type & reference
Definition array.h:33
fl::size max_size() const noexcept
Definition array.h:116
pointer data() noexcept
Definition array.h:96
ptrdiff_t difference_type
Definition array.h:32
const_pointer const_iterator
Definition array.h:38
const_reference operator[](fl::size pos) const
Definition array.h:86
array()=default
bool empty() const noexcept
Definition array.h:112
T & back()
Definition array.h:92
array & operator=(array &&)=default
array(const array &)=default
T & operator[](fl::size pos)
Definition array.h:84
const value_type * const_pointer
Definition array.h:36
const_iterator end() const noexcept
Definition array.h:109
array(const T &value)
Definition array.h:44
array(fl::initializer_list< T > list)
Definition array.h:50
T data_[N]
Definition array.h:136
const T & back() const
Definition array.h:94
void swap(array &other)
Definition array.h:125
T value_type
Definition array.h:30
const value_type & const_reference
Definition array.h:34
void fill(const T &value)
Definition array.h:119
const T & front() const
Definition array.h:90
array & operator=(const array &)=default
const T & at(fl::size pos) const
Definition array.h:77
iterator begin() noexcept
Definition array.h:101
fl::size size_type
Definition array.h:31
value_type * pointer
Definition array.h:35
fl::size size() const noexcept
Definition array.h:114
iterator end() noexcept
Definition array.h:107
const_iterator begin() const noexcept
Definition array.h:103
static T & error_value()
Definition array.h:132
pointer iterator
Definition array.h:37
const_pointer data() const noexcept
Definition array.h:98
const_iterator cbegin() const noexcept
Definition array.h:105
A fixed-size array implementation similar to std::array.
Definition array.h:27
void swap(array< T, N > &lhs, array< T, N > &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition array.h:156
bool operator!=(const array< T, N > &lhs, const array< T, N > &rhs)
Definition array.h:151
__PTRDIFF_TYPE__ ptrdiff_t
Definition cstddef.h:22
bool operator==(const array< T, N > &lhs, const array< T, N > &rhs)
Definition array.h:141
IMPORTANT!
Definition crgb.h:20