WeakValueHashMap is a thread-safe hash map that holds weak pointers to its values. More...
#include <WeakValueHashMap.hpp>
Classes | |
| class | ValueHandle |
| ValueHandle is a handle to a value in the WeakValueHashMap. More... | |
WeakValueHashMap is a thread-safe hash map that holds weak pointers to its values.
| KeyType | - Type of the keys in the map. Must be hashable and comparable. |
| ValueType | - Type of the values in the map. |
| Hasher | - Hash function for the keys. Default is std::hash<KeyType>. |
| Keyeq | - Equality comparison function for the keys. Default is std::equal_to<KeyType>. |
When a value is requested via GetOrInsert(), a strong pointer (shared_ptr) to the value is returned wrapped in a ValueHandle object. The ValueHandle object is responsible for removing the entry from the map when it is destroyed. If there are no more strong pointers to the value, the entry is removed from the map.
If a value is requested via Get(), a strong pointer to the value is returned wrapped in a ValueHandle object if the entry exists and the value has not expired. Otherwise, an empty ValueHandle is returned.
The map is thread-safe and can be accessed from multiple threads simultaneously.
Example usage:
WeakValueHashMap<int, std::string> Map; auto Handle = Map.GetOrInsert(1, "Value"); std::cout << *Handle << std::endl; // Outputs "Value"