Array.hpp
Go to the documentation of this file.
1 /****
2  * Array.hpp - Defines the Array class and associated macros
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the FlashString Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: 2018 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Object.hpp"
25 #include "ArrayPrinter.hpp"
26 
39 #define DECLARE_FSTR_ARRAY(name, ElementType) DECLARE_FSTR_OBJECT(name, FSTR::Array<ElementType>)
40 
48 #define DEFINE_FSTR_ARRAY(name, ElementType, ...) \
49  static DEFINE_FSTR_ARRAY_DATA(FSTR_DATA_NAME(name), ElementType, __VA_ARGS__); \
50  DEFINE_FSTR_REF(name)
51 
55 #define DEFINE_FSTR_ARRAY_LOCAL(name, ElementType, ...) \
56  static DEFINE_FSTR_ARRAY_DATA(FSTR_DATA_NAME(name), ElementType, __VA_ARGS__); \
57  DEFINE_FSTR_REF_LOCAL(name)
58 
65 #define DEFINE_FSTR_ARRAY_DATA(name, ElementType, ...) \
66  DEFINE_FSTR_ARRAY_DATA_SIZED(name, ElementType, FSTR_VA_NARGS(ElementType, __VA_ARGS__), __VA_ARGS__)
67 
75 #define DEFINE_FSTR_ARRAY_DATA_SIZED(name, ElementType, size, ...) \
76  constexpr const struct { \
77  FSTR::Array<ElementType> object; \
78  ElementType data[size]; \
79  } FSTR_PACKED name PROGMEM = {{sizeof(ElementType) * size}, {__VA_ARGS__}}; \
80  FSTR_CHECK_STRUCT(name);
81 
91 #define LOAD_FSTR_ARRAY(name, array) \
92  decltype((array)[0]) name[(array).size()] FSTR_ALIGNED; \
93  memcpy_aligned(name, (array).data(), (array).size());
94 
99 #define FSTR_ARRAY_ARRAY(name, ElementType, ...) \
100  static DEFINE_FSTR_ARRAY_DATA(FSTR_DATA_NAME(name), ElementType, __VA_ARGS__); \
101  LOAD_FSTR_ARRAY(name, FSTR_DATA_NAME(name).object)
102 
110 #define IMPORT_FSTR_ARRAY(name, ElementType, file) IMPORT_FSTR_OBJECT(name, FSTR::Array<ElementType>, file)
111 
115 #define IMPORT_FSTR_ARRAY_LOCAL(name, ElementType, file) IMPORT_FSTR_OBJECT_LOCAL(name, FSTR::Array<ElementType>, file)
116 
117 namespace FSTR
118 {
123 template <typename ElementType> class Array : public Object<Array<ElementType>, ElementType>
124 {
125 public:
126  static_assert(!std::is_pointer<ElementType>::value, "Pointer types not supported by Array - use Vector");
127 
128  /* Arduino Print support */
129 
135  {
136  return ArrayPrinter<Array>(*this);
137  }
138 
139  size_t printTo(Print& p) const
140  {
141  return printer().printTo(p);
142  }
143 } FSTR_PACKED;
144 
145 } // namespace FSTR
146 
Class template to provide a simple way to print the contents of an array.
Definition: ArrayPrinter.hpp:40
Class to access an array of integral values stored in flash.
Definition: Array.hpp:124
ArrayPrinter< Array > printer() const
Returns a printer object for this array.
Definition: Array.hpp:134
size_t printTo(Print &p) const
Definition: Array.hpp:139
Base class template for all types.
Definition: Object.hpp:101
Provides formatted output to stream.
Definition: Print.h:37
#define FSTR_PACKED
Definition: config.hpp:31
Definition: Array.hpp:118