Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
ObjectMap< K, V > Class Template Reference

Implementation of a HashMap for owned objects, i.e. anything created with new(). More...

#include <ObjectMap.h>

Classes

struct  Entry
 An entry in the ObjectMap. More...
 
class  Value
 Class to provide safe access to mapped value. More...
 

Public Member Functions

unsigned count () const
 Get the number of entries in this map. More...
 
const K & keyAt (unsigned idx) const
 
K & keyAt (unsigned idx)
 
const V * valueAt (unsigned idx) const
 
Value valueAt (unsigned idx)
 
const V * operator[] (const K &key) const
 Get value for given key, if it exists. More...
 
Value operator[] (const K &key)
 Access map entry by reference. More...
 
Value get (const K &key)
 Get map entry value. More...
 
void set (const K &key, V *value)
 Set a key value. More...
 
V * find (const K &key) const
 Find the value for a given key, if it exists. More...
 
int indexOf (const K &key) const
 Get the index of a key. More...
 
bool contains (const K &key) const
 Check if a key is contained within this map. More...
 
void removeAt (unsigned index)
 Remove entry at given index. More...
 
bool remove (const K &key)
 Remove a key from this map. More...
 
V * extract (const K &key)
 Get the value for a given key and remove it from the map, without destroying it. More...
 
V * extractAt (unsigned index)
 Get the value at a given index and remove it from the map, without destroying it. More...
 
void clear ()
 Clear the map of all entries. More...
 

Protected Attributes

Vector< Entryentries
 

Detailed Description

template<typename K, typename V>
class ObjectMap< K, V >

Implementation of a HashMap for owned objects, i.e. anything created with new().

Note
Once added to the map the object is destroyed when no longer required.

Example:

void test()
{
MyType* object1 = new MyType();
if (map["key1"] == nullptr) { // Does NOT create entry in map
map["key1"] = object1; // Entry now created, "key1" -> object1
}
MyType* object2 = new MyType();
map["key1"] = object2; // object1 is destroyed, "key1" -> object2
// Demonstrate use of value reference
auto value = map["key1"]; // Returns ObjectMap<String, MyType>::Value object
value = new MyType(); // "key1" -> new object
value = nullptr; // Free object, "key1" -> nullptr (but still in map)
value.remove(); // Free object1 and remove from map
// As soon as `map` goes out of scope, all contained objects are detroyed
map["key1"] = new MyType();
map["key2"] = new MyType();
}

Member Function Documentation

template<typename K, typename V>
void ObjectMap< K, V >::clear ( )
inline

Clear the map of all entries.

template<typename K, typename V>
bool ObjectMap< K, V >::contains ( const K &  key) const
inline

Check if a key is contained within this map.

Parameters
keythe key to check
Return values
booltrue if key exists
template<typename K, typename V>
unsigned ObjectMap< K, V >::count ( ) const
inline

Get the number of entries in this map.

Return values
intEntry count
template<typename K, typename V>
V* ObjectMap< K, V >::extract ( const K &  key)
inline

Get the value for a given key and remove it from the map, without destroying it.

Parameters
key
Return values
V*
Note
The returned object must be freed by the caller when no longer required
template<typename K, typename V>
V* ObjectMap< K, V >::extractAt ( unsigned  index)
inline

Get the value at a given index and remove it from the map, without destroying it.

Parameters
index
Return values
V*
Note
The returned object must be freed by the caller when no longer required
template<typename K, typename V>
V* ObjectMap< K, V >::find ( const K &  key) const
inline

Find the value for a given key, if it exists.

Parameters
key
Return values
V*Points to the object if it exists, otherwise nullptr
Note
If you need to modify the existing map entry, use operator[] or valueAt()
template<typename K, typename V>
Value ObjectMap< K, V >::get ( const K &  key)
inline

Get map entry value.

Parameters
key
Return values
Value
See also
operator[]
template<typename K, typename V>
int ObjectMap< K, V >::indexOf ( const K &  key) const
inline

Get the index of a key.

Parameters
key
Return values
intThe index of the key, or -1 if key does not exist
template<typename K, typename V>
const V* ObjectMap< K, V >::operator[] ( const K &  key) const
inline

Get value for given key, if it exists.

Parameters
key
Return values
constV* Will be null if not found in the map
Note
The caller must not use delete on the returned value
template<typename K, typename V>
Value ObjectMap< K, V >::operator[] ( const K &  key)
inline

Access map entry by reference.

Parameters
key
Return values
ValueGuarded access to mapped value corresponding to given key
Note
If the given key does not exist in the map it will NOT be created
See also
valueAt()
template<typename K, typename V>
bool ObjectMap< K, V >::remove ( const K &  key)
inline

Remove a key from this map.

Parameters
keyThe key identifying the entry to remove
Return values
booltrue if the value was found and removed
template<typename K, typename V>
void ObjectMap< K, V >::removeAt ( unsigned  index)
inline

Remove entry at given index.

Parameters
indexlocation to remove from this map
template<typename K, typename V>
void ObjectMap< K, V >::set ( const K &  key,
V *  value 
)
inline

Set a key value.

Parameters
key
value