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 FSTR_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 FSTR_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, FSTR_VA_NARGS(ObjectType*, __VA_ARGS__), __VA_ARGS__)
87 
96 #define DEFINE_FSTR_VECTOR_DATA_SIZED(name, ObjectType, size, ...) \
97  FSTR_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>, const ObjectType*>
110 {
111 public:
112  using DataPtrType = const ObjectType* const*;
113 
114  template <typename ValueType, typename T = ObjectType>
115  typename std::enable_if<!std::is_same<T, String>::value, int>::type indexOf(const ValueType& value) const
116  {
117  return Object<Vector<ObjectType>, const ObjectType*>::indexOf(value);
118  }
119 
120  template <typename T = ObjectType>
121  typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const char* value,
122  bool ignoreCase = true) const
123  {
124  auto dataptr = this->data();
125  auto len = this->length();
126  auto clen = strlen(value);
127  for(unsigned i = 0; i < len; ++i) {
128  if(unsafeValueAt(dataptr, i).equals(value, clen, ignoreCase)) {
129  return i;
130  }
131  }
132 
133  return -1;
134  }
135 
136  template <typename ValueType, typename T = ObjectType>
137  typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const ValueType& value,
138  bool ignoreCase = true) const
139  {
140  auto dataptr = this->data();
141  auto len = this->length();
142  for(unsigned i = 0; i < len; ++i) {
143  if(unsafeValueAt(dataptr, i).equals(value, ignoreCase)) {
144  return i;
145  }
146  }
147 
148  return -1;
149  }
150 
151  const ObjectType& valueAt(unsigned index) const
152  {
153  return (index < this->length()) ? this->unsafeValueAt(this->data(), index) : ObjectType::empty();
154  }
155 
156  const ObjectType& operator[](unsigned index) const
157  {
158  return valueAt(index);
159  }
160 
161  /* Arduino Print support */
162 
164  {
165  return ArrayPrinter<Vector>(*this);
166  }
167 
168  size_t printTo(Print& p) const
169  {
170  return printer().printTo(p);
171  }
172 
173  FSTR_INLINE static const ObjectType& unsafeValueAt(const DataPtrType dataptr, unsigned index)
174  {
175  auto ptr = dataptr[index];
176  return ptr ? *ptr : ObjectType::empty();
177  }
178 };
179 
180 } // namespace FSTR
181 
Class template to provide a simple way to print the contents of an array.
Definition: ArrayPrinter.hpp:40
Base class template for all types.
Definition: Object.hpp:125
constexpr const size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:180
DataPtrType data() const
Definition: Object.hpp:217
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:137
size_t printTo(Print &p) const
Definition: Vector.hpp:168
const ObjectType & operator[](unsigned index) const
Definition: Vector.hpp:156
std::enable_if<!std::is_same< T, String >::value, int >::type indexOf(const ValueType &value) const
Definition: Vector.hpp:115
std::enable_if< std::is_same< T, String >::value, int >::type indexOf(const char *value, bool ignoreCase=true) const
Definition: Vector.hpp:121
const ObjectType *const * DataPtrType
Definition: Vector.hpp:112
ArrayPrinter< Vector > printer() const
Definition: Vector.hpp:163
const ObjectType & valueAt(unsigned index) const
Definition: Vector.hpp:151
static const ObjectType & unsafeValueAt(const DataPtrType dataptr, unsigned index)
Definition: Vector.hpp:173
Provides formatted output to stream.
Definition: Print.h:37
#define FSTR_INLINE
Definition: config.hpp:28
Definition: Array.hpp:108
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34