Map.hpp
Go to the documentation of this file.
1 /****
2  * Map.hpp - Defines the Map 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: Nov 2019 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Object.hpp"
25 #include "MapPair.hpp"
26 #include "MapPrinter.hpp"
27 #include "ObjectIterator.hpp"
28 
42 #define DECLARE_FSTR_MAP(name, KeyType, ContentType) DECLARE_FSTR_OBJECT(name, DECL((FSTR::Map<KeyType, ContentType>)))
43 
52 #define DEFINE_FSTR_MAP(name, KeyType, ContentType, ...) \
53  static DEFINE_FSTR_MAP_DATA(FSTR_DATA_NAME(name), KeyType, ContentType, __VA_ARGS__); \
54  DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));
55 
59 #define DEFINE_FSTR_MAP_LOCAL(name, KeyType, ContentType, ...) \
60  static DEFINE_FSTR_MAP_DATA(FSTR_DATA_NAME(name), KeyType, ContentType, __VA_ARGS__); \
61  static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));
62 
71 #define DEFINE_FSTR_MAP_SIZED(name, KeyType, ContentType, size, ...) \
72  static DEFINE_FSTR_MAP_DATA_SIZED(FSTR_DATA_NAME(name), KeyType, ContentType, size, __VA_ARGS__); \
73  DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));
74 
78 #define DEFINE_FSTR_MAP_SIZED_LOCAL(name, KeyType, ContentType, size, ...) \
79  static DEFINE_FSTR_MAP_DATA_SIZED(FSTR_DATA_NAME(name), KeyType, ContentType, size, __VA_ARGS__); \
80  static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));
81 
90 #define DEFINE_FSTR_MAP_DATA(name, KeyType, ContentType, ...) \
91  DEFINE_FSTR_MAP_DATA_SIZED(name, KeyType, ContentType, \
92  FSTR_VA_NARGS(DECL((FSTR::MapPair<KeyType, ContentType>)), __VA_ARGS__), __VA_ARGS__)
93 
102 #define DEFINE_FSTR_MAP_DATA_SIZED(name, KeyType, ContentType, size, ...) \
103  FSTR_CONSTEXPR const struct { \
104  FSTR::ObjectBase object; \
105  FSTR::MapPair<KeyType, ContentType> data[size]; \
106  } FSTR_PACKED FSTR_ALIGNED name PROGMEM = {{sizeof(name.data)}, {__VA_ARGS__}}; \
107  FSTR_CHECK_STRUCT(name);
108 
109 namespace FSTR
110 {
116 template <typename KeyType, class ContentType, class Pair = MapPair<KeyType, ContentType>>
117 class Map : public Object<Map<KeyType, ContentType>, Pair>
118 {
119 public:
124  const Pair valueAt(unsigned index) const
125  {
126  if(index >= this->length()) {
127  return Pair::empty();
128  }
129 
130  static_assert(offsetof(Pair, content_) == sizeof(uint32_t), "Misaligned MapPair");
131 
132  auto ptr = this->data() + index;
133  return Pair{readValue(&ptr->key_), readValue(&ptr->content_)};
134  }
135 
141  template <typename TRefKey, typename T = KeyType>
142  typename std::enable_if<!std::is_class<T>::value, int>::type indexOf(const TRefKey& key) const
143  {
144  auto p = this->data();
145  auto len = this->length();
146  for(unsigned i = 0; i < len; ++i, ++p) {
147  if(p->key() == key) {
148  return i;
149  }
150  }
151 
152  return -1;
153  }
154 
161  template <typename TRefKey, typename T = KeyType>
162  typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const TRefKey& key,
163  bool ignoreCase = true) const
164  {
165  auto p = this->data();
166  auto len = this->length();
167  for(unsigned i = 0; i < len; ++i, ++p) {
168  if(ignoreCase) {
169  if(p->key().equalsIgnoreCase(key)) {
170  return i;
171  }
172  } else if(p->key() == key) {
173  return i;
174  }
175  }
176 
177  return -1;
178  }
179 
185  template <typename TRefKey> const Pair operator[](const TRefKey& key) const
186  {
187  return valueAt(indexOf(key));
188  }
189 
190  /* Arduino Print support */
191 
197  {
198  return MapPrinter<Map>(*this);
199  }
200 
201  size_t printTo(Print& p) const
202  {
203  return printer().printTo(p);
204  }
205 };
206 
207 } // namespace FSTR
208 
void size_t const void * key
Definition: blake2s.h:33
Class template to provide a simple way to print the contents of a Map.
Definition: MapPrinter.hpp:31
Class template to access an associative map.
Definition: Map.hpp:118
std::enable_if< std::is_same< T, String >::value, int >::type indexOf(const TRefKey &key, bool ignoreCase=true) const
Lookup a String key and return the index.
Definition: Map.hpp:162
const Pair operator[](const TRefKey &key) const
Lookup a key and return the entry, if found.
Definition: Map.hpp:185
const Pair valueAt(unsigned index) const
Get a map entry by index, if it exists.
Definition: Map.hpp:124
MapPrinter< Map > printer() const
Returns a printer object for this array.
Definition: Map.hpp:196
size_t printTo(Print &p) const
Definition: Map.hpp:201
std::enable_if<!std::is_class< T >::value, int >::type indexOf(const TRefKey &key) const
Lookup an integral key and return the index.
Definition: Map.hpp:142
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
Provides formatted output to stream.
Definition: Print.h:37
Definition: Array.hpp:108
std::enable_if< sizeof(T)==1, T >::type readValue(const T *ptr)
Read a typed value from flash memory ensuring correct alignment of access.
Definition: Utility.hpp:126