Vector.hpp
Go to the documentation of this file.
1 /****
2  * Vector.hpp - Defines the Vector class template 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_VECTOR(name, ObjectType) DECLARE_FSTR_OBJECT(name, FSTR::Vector<ObjectType>)
40 
48 #define DEFINE_FSTR_VECTOR(name, ObjectType, ...) \
49  static DEFINE_FSTR_VECTOR_DATA(FSTR_DATA_NAME(name), ObjectType, __VA_ARGS__); \
50  DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);
51 
55 #define DEFINE_FSTR_VECTOR_LOCAL(name, ObjectType, ...) \
56  static DEFINE_FSTR_VECTOR_DATA(FSTR_DATA_NAME(name), ObjectType, __VA_ARGS__); \
57  static constexpr DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);
58 
67 #define DEFINE_FSTR_VECTOR_SIZED(name, ObjectType, size, ...) \
68  static DEFINE_FSTR_VECTOR_DATA_SIZED(FSTR_DATA_NAME(name), ObjectType, size, __VA_ARGS__); \
69  DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);
70 
74 #define DEFINE_FSTR_VECTOR_SIZED_LOCAL(name, ObjectType, size, ...) \
75  static DEFINE_FSTR_VECTOR_DATA_SIZED(FSTR_DATA_NAME(name), ObjectType, size, __VA_ARGS__); \
76  static constexpr DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);
77 
85 #define DEFINE_FSTR_VECTOR_DATA(name, ObjectType, ...) \
86  DEFINE_FSTR_VECTOR_DATA_SIZED(name, ObjectType, sizeof((const void*[]){__VA_ARGS__}) / sizeof(void*), __VA_ARGS__)
87 
96 #define DEFINE_FSTR_VECTOR_DATA_SIZED(name, ObjectType, size, ...) \
97  constexpr const struct { \
98  FSTR::ObjectBase object; \
99  const ObjectType* data[size]; \
100  } name PROGMEM = {{sizeof(name.data)}, __VA_ARGS__}; \
101  FSTR_CHECK_STRUCT(name);
102 
103 namespace FSTR
104 {
109 template <class ObjectType> class Vector : public Object<Vector<ObjectType>, ObjectType*>
110 {
111 public:
112  template <typename ValueType, typename T = ObjectType>
113  typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const ValueType& value,
114  bool ignoreCase = true) const
115  {
116  if(!ignoreCase) {
117  return Object<Vector<String>, String*>::indexOf(value);
118  }
119 
120  auto len = this->length();
121  for(unsigned i = 0; i < len; ++i) {
122  if(valueAt(i).equalsIgnoreCase(value)) {
123  return i;
124  }
125  }
126 
127  return -1;
128  }
129 
130  const ObjectType& valueAt(unsigned index) const
131  {
132  if(index < this->length()) {
133  auto ptr = this->data()[index];
134  if(ptr != nullptr) {
135  return *ptr;
136  }
137  }
138 
139  return ObjectType::empty();
140  }
141 
142  const ObjectType& operator[](unsigned index) const
143  {
144  return valueAt(index);
145  }
146 
147  /* Arduino Print support */
148 
150  {
151  return ArrayPrinter<Vector>(*this);
152  }
153 
154  size_t printTo(Print& p) const
155  {
156  return printer().printTo(p);
157  }
158 };
159 
160 } // namespace FSTR
161 
Class template to provide a simple way to print the contents of an array.
Definition: ArrayPrinter.hpp:89
Base class template for all types.
Definition: Object.hpp:121
const ObjectType * * data() const
Definition: Object.hpp:203
size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:164
describes a counted string stored in flash memory
Definition: String.hpp:174
Class to access a Vector of objects stored in flash.
Definition: Vector.hpp:110
std::enable_if< std::is_same< T, String >::value, int >::type indexOf(const ValueType &value, bool ignoreCase=true) const
Definition: Vector.hpp:113
size_t printTo(Print &p) const
Definition: Vector.hpp:154
const ObjectType & operator[](unsigned index) const
Definition: Vector.hpp:142
ArrayPrinter< Vector > printer() const
Definition: Vector.hpp:149
const ObjectType & valueAt(unsigned index) const
Definition: Vector.hpp:130
Provides formatted output to stream.
Definition: Print.h:37
Definition: Array.hpp:108
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34