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_NAMED(name, FSTR::Array<ElementType>);
51 
55 #define DEFINE_FSTR_ARRAY_LOCAL(name, ElementType, ...) \
56  static DEFINE_FSTR_ARRAY_DATA(FSTR_DATA_NAME(name), ElementType, __VA_ARGS__); \
57  static constexpr DEFINE_FSTR_REF_NAMED(name, FSTR::Array<ElementType>);
58 
65 #define DEFINE_FSTR_ARRAY_DATA(name, ElementType, ...) \
66  constexpr const struct { \
67  FSTR::ObjectBase object; \
68  ElementType data[sizeof((const ElementType[]){__VA_ARGS__}) / sizeof(ElementType)]; \
69  } FSTR_PACKED FSTR_ALIGNED name PROGMEM = {{sizeof(name.data)}, {__VA_ARGS__}}; \
70  FSTR_CHECK_STRUCT(name);
71 
81 #define LOAD_FSTR_ARRAY(name, array) \
82  decltype((array)[0]) name[(array).size()] FSTR_ALIGNED; \
83  memcpy_aligned(name, (array).data(), (array).size());
84 
89 #define FSTR_ARRAY_ARRAY(name, ElementType, ...) \
90  static DEFINE_FSTR_ARRAY_DATA(FSTR_DATA_NAME(name), ElementType, __VA_ARGS__); \
91  LOAD_FSTR_ARRAY(name, FSTR_DATA_NAME(name).object)
92 
100 #define IMPORT_FSTR_ARRAY(name, ElementType, file) IMPORT_FSTR_OBJECT(name, FSTR::Array<ElementType>, file)
101 
105 #define IMPORT_FSTR_ARRAY_LOCAL(name, ElementType, file) IMPORT_FSTR_OBJECT_LOCAL(name, FSTR::Array<ElementType>, file)
106 
107 namespace FSTR
108 {
113 template <typename ElementType> class Array : public Object<Array<ElementType>, ElementType>
114 {
115 public:
116  static_assert(!std::is_pointer<ElementType>::value, "Pointer types not supported by Array - use Vector");
117 
118  /* Arduino Print support */
119 
125  {
126  return ArrayPrinter<Array>(*this);
127  }
128 
129  size_t printTo(Print& p) const
130  {
131  return printer().printTo(p);
132  }
133 };
134 
135 } // namespace FSTR
136 
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:114
ArrayPrinter< Array > printer() const
Returns a printer object for this array.
Definition: Array.hpp:124
size_t printTo(Print &p) const
Definition: Array.hpp:129
Base class template for all types.
Definition: Object.hpp:121
Provides formatted output to stream.
Definition: Print.h:37
Definition: Array.hpp:108