This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
Array.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
5 #include <Helpers/Error.hpp>
6 #include <stddef.h>
7 
10 
19 template <class T, size_t N>
20 struct Array {
21  T data[N];
22  using type = T;
23  constexpr static size_t length = N;
24 
34  T &operator[](size_t index) {
35  if (index >= N) { // TODO
36  ERROR(F("Index out of bounds: ") << index << F(" ≥ ") << N, 0xEDED);
37  index = N - 1;
38  }
39  return data[index];
40  }
41 
51  const T &operator[](size_t index) const {
52  if (index >= N) { // TODO
53  ERROR(F("Index out of bounds: ") << index << F(" ≥ ") << N, 0xEDED);
54  index = N - 1;
55  }
56  return data[index];
57  }
58 
62  T *begin() { return &data[0]; }
63 
67  const T *begin() const { return &data[0]; }
68 
72  T *end() { return &data[N]; }
73 
77  const T *end() const { return &data[N]; }
78 
85  bool operator==(const Array<T, N> &rhs) const {
86  if (this == &rhs)
87  return true;
88  for (size_t i = 0; i < N; i++)
89  if ((*this)[i] != rhs[i])
90  return false;
91  return true;
92  }
93 
100  bool operator!=(const Array<T, N> &rhs) const { return !(*this == rhs); }
101 };
102 
103 /// @}
Array::operator==
bool operator==(const Array< T, N > &rhs) const
Check the equality of all elements in two arrays.
Definition: Array.hpp:85
Array::operator!=
bool operator!=(const Array< T, N > &rhs) const
Check the inequality of all elements in two arrays.
Definition: Array.hpp:100
ERROR
#define ERROR(x, e)
Definition: Error.hpp:27
Error.hpp
Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:15
Array
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:20
Array::end
const T * end() const
Get a pointer to the memory beyond the array.
Definition: Array.hpp:77
Array::data
T data[N]
Definition: Array.hpp:21
Array::end
T * end()
Get a pointer to the memory beyond the array.
Definition: Array.hpp:72
Array::operator[]
T & operator[](size_t index)
Get the element at the given index.
Definition: Array.hpp:34
Array::length
constexpr static size_t length
Definition: Array.hpp:23
Array::begin
const T * begin() const
Get a pointer to the first element.
Definition: Array.hpp:67
Array::begin
T * begin()
Get a pointer to the first element.
Definition: Array.hpp:62
Array::operator[]
const T & operator[](size_t index) const
Get the element at the given index.
Definition: Array.hpp:51
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23