Template Class UsageTrackingMap#

Class Documentation#

template<class K, class V>
class UsageTrackingMap#

A map-like container that tracks usage of its elements using shared pointers.

This class ensures that elements are not removed while they are still in use and prevents duplicate keys from being added.

Template Parameters:
  • K – The type of keys in the map.

  • V – The type of values in the map. Values are stored as shared pointers.

Public Functions

inline ~UsageTrackingMap()#

Clean up the UsageTrackingMap. This tries to erase every value it is holding and will throw an error if there are issues.

inline void add(const K &key, const ks_ptr<V> &val)#

Adds a new key-value pair to the map.

Parameters:
  • key – The key to add.

  • val – The value to associate with the key, stored as a shared pointer.

Throws:

std::invalid_argument – If the key already exists in the map.

inline const std::map<K, ks_ptr<V>> &map() const#

Return the map of entries.

Returns:

map of registered entries

inline void erase(const K &key)#

Removes a key-value pair from the map.

Parameters:

key – The key to remove.

Throws:

std::invalid_argument – If the value associated with the key is still in use, i.e., its use count is greater than 1.

inline void purge(const K &key)#

Removes a key-value pair from the map without doing any checks on the reference count.

Parameters:

key – The key to remove.

inline size_t size() const#

Gets the number of items in the map.

Returns:

The number of items currently in the map.

inline const ks_ptr<V> &at(const K &key) const#

Retrieves the value associated with a given key.

Parameters:

key – The key whose value is to be retrieved.

Throws:

std::out_of_range – If the key is not found in the map.

Returns:

A const reference to the shared pointer holding the value.

inline bool contains(const K &key) const#

Checks if a key exists in the map.

Parameters:

key – The key to check.

Returns:

True if the key exists, false otherwise.

inline bool empty() const noexcept#

Checks if the map is empty.

Returns:

True if the map contains no elements, false otherwise.

inline void forEach(const std::function<void(const K&, const ks_ptr<V>&)> &callback)#

Apply the callback function to each key-value pair in the UsageTrackingMap.

Parameters:

callback – The function to apply to each key-value pair.

inline std::vector<ks_ptr<V>> items() const#

Return a list of the items in the usage map.

Returns:

The list of items

inline const std::string dumpString(std::string_view prefix = "") const#

Dumps the usage tracker map content as a string.

This method returns a string representation of the map’s item names.

Parameters:

prefix – A prefix string to prepend.

Returns:

A string representation of the map item names.