ObjectMap< K, V > Class Template Reference
Implementation of a HashMap for owned objects, i.e. anything created with new(). More...
#include <ObjectMap.h>
Collaboration diagram for ObjectMap< K, V >:
Classes | |
| struct | Entry |
| An entry in the ObjectMap. More... | |
| class | Value |
| Class to provide safe access to mapped value. More... | |
Public Member Functions | |
| ObjectMap ()=default | |
| ~ObjectMap () | |
| 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< Entry > | entries |
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();
}
MyType* object2 = new MyType();
// Demonstrate use of value reference
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 destroyed
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
Implementation of a HashMap for owned objects, i.e. anything created with new().
Definition: ObjectMap.h:49
struct test_s test
Constructor & Destructor Documentation
◆ ObjectMap()
◆ ~ObjectMap()
Member Function Documentation
◆ clear()
template<typename K , typename V >
|
inline |
Clear the map of all entries.
◆ contains()
template<typename K , typename V >
|
inline |
Check if a key is contained within this map.
- Parameters
-
key the key to check
- Return values
-
bool true if key exists
◆ count()
Get the number of entries in this map.
- Return values
-
int Entry count
◆ extract()
template<typename K , typename V >
|
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
◆ extractAt()
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
◆ find()
template<typename K , typename V >
|
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[]orvalueAt()
◆ get()
◆ indexOf()
template<typename K , typename V >
|
inline |
Get the index of a key.
- Parameters
-
key
- Return values
-
int The index of the key, or -1 if key does not exist
◆ keyAt() [1/2]
◆ keyAt() [2/2]
◆ operator[]() [1/2]
◆ operator[]() [2/2]
template<typename K , typename V >
|
inline |
Get value for given key, if it exists.
- Parameters
-
key
- Return values
-
const V* Will be null if not found in the map
- Note
- The caller must not use
deleteon the returned value
◆ remove()
template<typename K , typename V >
|
inline |
Remove a key from this map.
- Parameters
-
key The key identifying the entry to remove
- Return values
-
bool true if the value was found and removed
◆ removeAt()
Remove entry at given index.
- Parameters
-
index location to remove from this map
◆ set()
template<typename K , typename V >
|
inline |
Set a key value.
- Parameters
-
key value
◆ valueAt() [1/2]
◆ valueAt() [2/2]
Member Data Documentation
◆ entries
The documentation for this class was generated from the following file:
1.9.1