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