ObjectMap.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * ObjectMap.h
8  *
9  * @author: 31 Jul 2018 - Mikee47 <mike@sillyhouse.net>
10  *
11  */
12 
13 #pragma once
14 
15 #include "WVector.h"
16 #include <memory>
17 
48 template <typename K, typename V> class ObjectMap
49 {
50 public:
51  ObjectMap() = default;
52 
54  {
55  clear();
56  }
57 
62  class Value
63  {
64  public:
65  Value(ObjectMap<K, V>& map, const K& key) : map(map), key(key)
66  {
67  }
68 
69  const K& getKey() const
70  {
71  return key;
72  }
73 
74  V* getValue() const
75  {
76  return map.find(key);
77  }
78 
79  Value& operator=(V* newValue)
80  {
81  map.set(key, newValue);
82  return *this;
83  }
84 
85  operator V*() const
86  {
87  return getValue();
88  }
89 
90  V* operator->() const
91  {
92  return getValue();
93  }
94 
99  bool remove()
100  {
101  return map.remove(key);
102  }
103 
109  V* extract()
110  {
111  return map.extract(key);
112  }
113 
114  private:
115  ObjectMap<K, V>& map;
116  K key;
117  };
118 
123  unsigned count() const
124  {
125  return entries.count();
126  }
127 
128  /*
129  * @brief Get a key at a specified index, non-modifiable
130  * @param idx the index to get the key at
131  * @return The key at index idx
132  */
133  const K& keyAt(unsigned idx) const
134  {
135  return entries[idx].key;
136  }
137 
138  /*
139  * @brief Get a key at a specified index
140  * @param idx the index to get the key at
141  * @return Reference to the key at index idx
142  */
143  K& keyAt(unsigned idx)
144  {
145  return entries[idx].key;
146  }
147 
148  /*
149  * @brief Get a value at a specified index, non-modifiable
150  * @param idx the index to get the value at
151  * @retval The value at index idx
152  * @note The caller must not use `delete` on the returned value
153  */
154  const V* valueAt(unsigned idx) const
155  {
156  return entries[idx].value;
157  }
158 
159  /*
160  * @brief Get a value at a specified index
161  * @param idx the index to get the value at
162  * @retval Value Reference to value at index idx
163  * @see `operator[]`
164  */
165  Value valueAt(unsigned idx)
166  {
167  return Value(*this, entries[idx].key);
168  }
169 
176  const V* operator[](const K& key) const
177  {
178  return find(key);
179  }
180 
188  Value operator[](const K& key)
189  {
190  return get(key);
191  }
192 
198  Value get(const K& key)
199  {
200  return Value(*this, key);
201  }
202 
207  void set(const K& key, V* value)
208  {
209  int i = entries.indexOf(key);
210  if(i >= 0) {
211  entries[i].value.reset(value);
212  } else {
213  entries.addElement(new Entry(key, value));
214  }
215  }
216 
223  V* find(const K& key) const
224  {
225  int index = entries.indexOf(key);
226  return (index < 0) ? nullptr : entries[index].value.get();
227  }
228 
234  int indexOf(const K& key) const
235  {
236  return entries.indexOf(key);
237  }
238 
244  bool contains(const K& key) const
245  {
246  return entries.contains(key);
247  }
248 
253  void removeAt(unsigned index)
254  {
255  entries.remove(index);
256  }
257 
263  bool remove(const K& key)
264  {
265  int index = indexOf(key);
266  if(index < 0) {
267  return false;
268  }
269  removeAt(index);
270  return true;
271  }
272 
279  V* extract(const K& key)
280  {
281  int i = indexOf(key);
282  return (i < 0) ? nullptr : extractAt(i);
283  }
284 
291  V* extractAt(unsigned index)
292  {
293  std::unique_ptr<V> value;
294  if(index < entries.count()) {
295  entries[index].value.swap(value);
296  entries.remove(index);
297  }
298  return value.release();
299  }
300 
304  void clear()
305  {
306  entries.clear();
307  }
308 
309 protected:
313  struct Entry {
314  K key;
315  std::unique_ptr<V> value;
316 
317  bool operator==(const K& keyToFind) const
318  {
319  return key == keyToFind;
320  }
321 
322  Entry(const K& key, V* value) : key(key)
323  {
324  this->value.reset(value);
325  }
326  };
327 
329 
330 private:
331  // Copy constructor unsafe, so prevent access
332  ObjectMap(ObjectMap<K, V>& that);
333 };
long map(long x, long in_min, long in_max, long out_min, long out_max)
void size_t const void * key
Definition: blake2s.h:33
Class to provide safe access to mapped value.
Definition: ObjectMap.h:63
V * operator->() const
Definition: ObjectMap.h:90
const K & getKey() const
Definition: ObjectMap.h:69
Value(ObjectMap< K, V > &map, const K &key)
Definition: ObjectMap.h:65
V * getValue() const
Definition: ObjectMap.h:74
V * extract()
Get the value for a given key and remove it from the map, without destroying it.
Definition: ObjectMap.h:109
bool remove()
Remove this value from the map.
Definition: ObjectMap.h:99
Value & operator=(V *newValue)
Definition: ObjectMap.h:79
Implementation of a HashMap for owned objects, i.e. anything created with new().
Definition: ObjectMap.h:49
Value valueAt(unsigned idx)
Definition: ObjectMap.h:165
unsigned count() const
Get the number of entries in this map.
Definition: ObjectMap.h:123
ObjectMap()=default
void removeAt(unsigned index)
Remove entry at given index.
Definition: ObjectMap.h:253
V * find(const K &key) const
Find the value for a given key, if it exists.
Definition: ObjectMap.h:223
~ObjectMap()
Definition: ObjectMap.h:53
V * extract(const K &key)
Get the value for a given key and remove it from the map, without destroying it.
Definition: ObjectMap.h:279
V * extractAt(unsigned index)
Get the value at a given index and remove it from the map, without destroying it.
Definition: ObjectMap.h:291
void clear()
Clear the map of all entries.
Definition: ObjectMap.h:304
Value get(const K &key)
Get map entry value.
Definition: ObjectMap.h:198
const V * operator[](const K &key) const
Get value for given key, if it exists.
Definition: ObjectMap.h:176
int indexOf(const K &key) const
Get the index of a key.
Definition: ObjectMap.h:234
Vector< Entry > entries
Definition: ObjectMap.h:328
bool remove(const K &key)
Remove a key from this map.
Definition: ObjectMap.h:263
bool contains(const K &key) const
Check if a key is contained within this map.
Definition: ObjectMap.h:244
const K & keyAt(unsigned idx) const
Definition: ObjectMap.h:133
void set(const K &key, V *value)
Set a key value.
Definition: ObjectMap.h:207
const V * valueAt(unsigned idx) const
Definition: ObjectMap.h:154
Value operator[](const K &key)
Access map entry by reference.
Definition: ObjectMap.h:188
K & keyAt(unsigned idx)
Definition: ObjectMap.h:143
Vector class template.
Definition: WVector.h:32
Value
Definition: Components/IFS/src/include/IFS/Error.h:82
An entry in the ObjectMap.
Definition: ObjectMap.h:313
K key
Definition: ObjectMap.h:314
Entry(const K &key, V *value)
Definition: ObjectMap.h:322
std::unique_ptr< V > value
Definition: ObjectMap.h:315
bool operator==(const K &keyToFind) const
Definition: ObjectMap.h:317