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>, const ObjectType*>
110 {
111 public:
112  using DataPtrType = const ObjectType* const*;
113 
115 
116  template <typename T = ObjectType>
117  typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const char* value,
118  bool ignoreCase = true) const
119  {
120  auto dataptr = this->data();
121  auto len = this->length();
122  auto clen = strlen(value);
123  for(unsigned i = 0; i < len; ++i) {
124  if(unsafeValueAt(dataptr, i).equals(value, clen, ignoreCase)) {
125  return i;
126  }
127  }
128 
129  return -1;
130  }
131 
132  template <typename ValueType, typename T = ObjectType>
133  typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const ValueType& value,
134  bool ignoreCase = true) const
135  {
136  auto dataptr = this->data();
137  auto len = this->length();
138  for(unsigned i = 0; i < len; ++i) {
139  if(unsafeValueAt(dataptr, i).equals(value, ignoreCase)) {
140  return i;
141  }
142  }
143 
144  return -1;
145  }
146 
147  const ObjectType& valueAt(unsigned index) const
148  {
149  return (index < this->length()) ? this->unsafeValueAt(this->data(), index) : ObjectType::empty();
150  }
151 
152  const ObjectType& operator[](unsigned index) const
153  {
154  return valueAt(index);
155  }
156 
157  /* Arduino Print support */
158 
160  {
161  return ArrayPrinter<Vector>(*this);
162  }
163 
164  size_t printTo(Print& p) const
165  {
166  return printer().printTo(p);
167  }
168 
169  FSTR_INLINE static const ObjectType& unsafeValueAt(const DataPtrType dataptr, unsigned index)
170  {
171  auto ptr = dataptr[index];
172  return ptr ? *ptr : ObjectType::empty();
173  }
174 };
175 
176 } // namespace FSTR
177 
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:121
constexpr const size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:168
DataPtrType data() const
Definition: Object.hpp:205
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:133
size_t printTo(Print &p) const
Definition: Vector.hpp:164
const ObjectType & operator[](unsigned index) const
Definition: Vector.hpp:152
std::enable_if< std::is_same< T, String >::value, int >::type indexOf(const char *value, bool ignoreCase=true) const
Definition: Vector.hpp:117
const ObjectType *const * DataPtrType
Definition: Vector.hpp:112
ArrayPrinter< Vector > printer() const
Definition: Vector.hpp:159
const ObjectType & valueAt(unsigned index) const
Definition: Vector.hpp:147
static const ObjectType & unsafeValueAt(const DataPtrType dataptr, unsigned index)
Definition: Vector.hpp:169
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