utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
utility::Cache< Key, Entry, Compare > Class Template Reference

A thread-safe cache for storing key-value pairs. More...

#include <headers/utility/cache.hpp>

Public Member Functions

 Cache (void)=default
 Default constructor for the Cache class.
 
 ~Cache (void)=default
 Default destructor for the Cache class.
 
 Cache (const Cache &other)=delete
 Deleted copy constructor to prevent copying of the cache.
 
Cacheoperator= (const Cache &other)=delete
 Deleted copy assignment operator to prevent copying of the cache.
 
 Cache (Cache &&other) noexcept=delete
 Deleted move constructor to prevent moving of the cache.
 
Cacheoperator= (Cache &&other) noexcept=delete
 Deleted move assignment operator to prevent moving of the cache.
 
bool contains (const Key &key) const
 Check if the cache contains a specific key.
 
std::optional< Entry > get (const Key &key) const
 Retrieve the value associated with a specific key.
 
void put (const Key &key, const Entry &value)
 Add or update a key-value pair in the cache.
 
void put (Key &&key, Entry &&value)
 Add or update a key-value pair in the cache using move semantics.
 
template<typename... Args>
Entry & emplace (const Key &key, Args &&...args)
 Emplace a new entry in the cache with the given key and constructor arguments.
 
template<typename... Args>
Entry & emplace (Key &&key, Args &&...args)
 Emplace a new entry in the cache with the given key and constructor arguments using move semantics.
 
bool erase (const Key &key)
 Remove an entry from the cache.
 
void erase_if (const std::function< bool(const Key &, const Entry &)> &predicate)
 Remove entries from the cache based on a predicate.
 
void apply (const std::function< void(const Key &, Entry &)> &function)
 Apply a function to each entry in the cache.
 
void clear (void)
 Clear all entries from the cache.
 
std::map< Key, Entry, Compare >::size_type size (void) const
 Get the number of entries in the cache.
 
bool empty (void) const
 Check if the cache is empty.
 

Detailed Description

template<typename Key, CacheEntry Entry, typename Compare = std::less<Key>>
requires CacheKey<Key, Compare>
class utility::Cache< Key, Entry, Compare >

A thread-safe cache for storing key-value pairs.

This class provides a thread-safe cache that allows storing and retrieving key-value pairs. It uses a std::map internally to manage the entries and provides methods for adding, retrieving, and removing entries.

Template Parameters
KeyType of the keys used in the cache.
EntryType of the values stored in the cache.
CompareComparison function type for ordering keys (default is std::less<Key>).

Definition at line 68 of file cache.hpp.

Member Function Documentation

◆ apply()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
void utility::Cache< Key, Entry, Compare >::apply ( const std::function< void(const Key &, Entry &)> &  function)
inline

Apply a function to each entry in the cache.

Parameters
functionA function that takes a key and entry and performs an operation on them.

Definition at line 232 of file cache.hpp.

◆ clear()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
void utility::Cache< Key, Entry, Compare >::clear ( void  )
inline

Clear all entries from the cache.

Definition at line 243 of file cache.hpp.

◆ contains()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
bool utility::Cache< Key, Entry, Compare >::contains ( const Key &  key) const
inline

Check if the cache contains a specific key.

Parameters
keyThe key to check for in the cache.
Returns
True if the key exists in the cache, false otherwise.

Definition at line 114 of file cache.hpp.

◆ emplace() [1/2]

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
template<typename... Args>
Entry & utility::Cache< Key, Entry, Compare >::emplace ( const Key &  key,
Args &&...  args 
)
inline

Emplace a new entry in the cache with the given key and constructor arguments.

Template Parameters
ArgsTypes of the constructor arguments for the entry.
Parameters
keyThe key to associate with the new entry.
argsConstructor arguments for the entry.
Returns
Reference to the newly emplaced entry.
Note
The returned reference is only valid while the calling thread holds no other cache operation; concurrent modification may invalidate it.

Definition at line 171 of file cache.hpp.

◆ emplace() [2/2]

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
template<typename... Args>
Entry & utility::Cache< Key, Entry, Compare >::emplace ( Key &&  key,
Args &&...  args 
)
inline

Emplace a new entry in the cache with the given key and constructor arguments using move semantics.

Template Parameters
ArgsTypes of the constructor arguments for the entry.
Parameters
keyThe key to associate with the new entry (moved).
argsConstructor arguments for the entry.
Returns
Reference to the newly emplaced entry.
Note
The returned reference is only valid while the calling thread holds no other cache operation; concurrent modification may invalidate it.

Definition at line 190 of file cache.hpp.

◆ empty()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
bool utility::Cache< Key, Entry, Compare >::empty ( void  ) const
inline

Check if the cache is empty.

Returns
True if the cache is empty, false otherwise.

Definition at line 263 of file cache.hpp.

◆ erase()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
bool utility::Cache< Key, Entry, Compare >::erase ( const Key &  key)
inline

Remove an entry from the cache.

Parameters
keyThe key of the entry to remove.
Returns
True if the entry was removed, false if it was not found.

Definition at line 203 of file cache.hpp.

◆ erase_if()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
void utility::Cache< Key, Entry, Compare >::erase_if ( const std::function< bool(const Key &, const Entry &)> &  predicate)
inline

Remove entries from the cache based on a predicate.

Parameters
predicateA function that takes a key and entry and returns true if the entry should be removed.

Definition at line 214 of file cache.hpp.

◆ get()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
std::optional< Entry > utility::Cache< Key, Entry, Compare >::get ( const Key &  key) const
inline

Retrieve the value associated with a specific key.

Parameters
keyThe key to look up in the cache.
Returns
An optional containing the value if the key exists, or std::nullopt if the key is not found.

Definition at line 126 of file cache.hpp.

◆ put() [1/2]

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
void utility::Cache< Key, Entry, Compare >::put ( const Key &  key,
const Entry &  value 
)
inline

Add or update a key-value pair in the cache.

Parameters
keyThe key to add or update.
valueThe value associated with the key.

Definition at line 141 of file cache.hpp.

◆ put() [2/2]

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
void utility::Cache< Key, Entry, Compare >::put ( Key &&  key,
Entry &&  value 
)
inline

Add or update a key-value pair in the cache using move semantics.

Parameters
keyThe key to add or update (moved).
valueThe value associated with the key (moved).

Definition at line 153 of file cache.hpp.

◆ size()

template<typename Key , CacheEntry Entry, typename Compare = std::less<Key>>
std::map< Key, Entry, Compare >::size_type utility::Cache< Key, Entry, Compare >::size ( void  ) const
inline

Get the number of entries in the cache.

Returns
The number of entries in the cache.

Definition at line 253 of file cache.hpp.


The documentation for this class was generated from the following file: