Simox  2.3.74.0
simox::caching::CacheMap< KeyT, ValueT, MapT > Class Template Reference

Wrapper for using a std::map or std::unordered_map as a data cache. More...

Public Types

using FetchFn = std::function< ValueT(KeyT key)>
 A fetch function which creates an item when its not already in the cache. More...
 

Public Member Functions

 CacheMap ()
 Construct a CacheMap without default fetch function. More...
 
 CacheMap (FetchFn fetchFn)
 Construct a CacheMap with default fetch function. More...
 
void setFetchFn (FetchFn fetchFn, bool clear=false)
 Set the fetch function, optionally clearing the cache (disabled by default). More...
 
ValueT & get (const KeyT &key, std::function< ValueT(KeyT)> fetchFn)
 Get a value for the given key, using the given fetch function. More...
 
ValueT & get (const KeyT &key)
 Get a value for the given key, using the stored fetch function. Make sure to pass set a fetch function via the constructor or setFetchFn(). More...
 
ValueT & get (const KeyT &key) const
 Get a value for the given key (const version). More...
 
ValueT & insert (const KeyT &key)
 Explicitly insert a value using the stored fetch function. More...
 
ValueT & insert (const KeyT &key, const ValueT &value)
 Explicitly insert a value. More...
 
ValueT & insert (const KeyT &key, std::function< ValueT(KeyT)> fetchFn)
 Explicitly insert a value using the fetch function. More...
 
void clear ()
 
size_t size () const
 
bool empty () const
 
bool contains (const KeyT &key) const
 
auto begin ()
 
auto begin () const
 
auto end ()
 
auto end () const
 
auto find (const KeyT &key)
 
auto find (const KeyT &key) const
 

Detailed Description

template<class KeyT, class ValueT, template< class ... > class MapT = std::map>
class simox::caching::CacheMap< KeyT, ValueT, MapT >

Wrapper for using a std::map or std::unordered_map as a data cache.

A CacheMap uses a "fetch function" (FetchFn) to retrieve values for keys that are not in the cache. It can be specified (among others) in the constructor, via setFetchFn() or when accessing an item via get().

Member Typedef Documentation

◆ FetchFn

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
using simox::caching::CacheMap< KeyT, ValueT, MapT >::FetchFn = std::function<ValueT(KeyT key)>

A fetch function which creates an item when its not already in the cache.

Constructor & Destructor Documentation

◆ CacheMap() [1/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
simox::caching::CacheMap< KeyT, ValueT, MapT >::CacheMap ( )
inline

Construct a CacheMap without default fetch function.

Specify the fetch function when calling get() or via setFetchFn().

◆ CacheMap() [2/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
simox::caching::CacheMap< KeyT, ValueT, MapT >::CacheMap ( FetchFn  fetchFn)
inline

Construct a CacheMap with default fetch function.

Member Function Documentation

◆ begin() [1/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
auto simox::caching::CacheMap< KeyT, ValueT, MapT >::begin ( )
inline

◆ begin() [2/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
auto simox::caching::CacheMap< KeyT, ValueT, MapT >::begin ( ) const
inline

◆ clear()

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
void simox::caching::CacheMap< KeyT, ValueT, MapT >::clear ( )
inline

◆ contains()

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
bool simox::caching::CacheMap< KeyT, ValueT, MapT >::contains ( const KeyT &  key) const
inline

◆ empty()

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
bool simox::caching::CacheMap< KeyT, ValueT, MapT >::empty ( ) const
inline

◆ end() [1/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
auto simox::caching::CacheMap< KeyT, ValueT, MapT >::end ( )
inline

◆ end() [2/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
auto simox::caching::CacheMap< KeyT, ValueT, MapT >::end ( ) const
inline

◆ find() [1/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
auto simox::caching::CacheMap< KeyT, ValueT, MapT >::find ( const KeyT &  key)
inline

◆ find() [2/2]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
auto simox::caching::CacheMap< KeyT, ValueT, MapT >::find ( const KeyT &  key) const
inline

◆ get() [1/3]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
ValueT& simox::caching::CacheMap< KeyT, ValueT, MapT >::get ( const KeyT &  key,
std::function< ValueT(KeyT)>  fetchFn 
)
inline

Get a value for the given key, using the given fetch function.

Parameters
keyThe key.
fetchFnThe function to use when the key is not stored in the cache.
Returns
The stored value.

◆ get() [2/3]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
ValueT& simox::caching::CacheMap< KeyT, ValueT, MapT >::get ( const KeyT &  key)
inline

Get a value for the given key, using the stored fetch function. Make sure to pass set a fetch function via the constructor or setFetchFn().

Parameters
keyThe key.
Returns
The stored value.
Exceptions
<tt>simox::error::SimoxError</tt>If no fetch function was specified.

◆ get() [3/3]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
ValueT& simox::caching::CacheMap< KeyT, ValueT, MapT >::get ( const KeyT &  key) const
inline

Get a value for the given key (const version).

This does not change the cache, therefore accessing a non-existing key will cause an exception.

Parameters
keyThe key.
Returns
The stored value if it exists.
Exceptions
<tt>simox::error::SimoxError</tt>If the key is not known.

◆ insert() [1/3]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
ValueT& simox::caching::CacheMap< KeyT, ValueT, MapT >::insert ( const KeyT &  key)
inline

Explicitly insert a value using the stored fetch function.

◆ insert() [2/3]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
ValueT& simox::caching::CacheMap< KeyT, ValueT, MapT >::insert ( const KeyT &  key,
const ValueT &  value 
)
inline

Explicitly insert a value.

◆ insert() [3/3]

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
ValueT& simox::caching::CacheMap< KeyT, ValueT, MapT >::insert ( const KeyT &  key,
std::function< ValueT(KeyT)>  fetchFn 
)
inline

Explicitly insert a value using the fetch function.

◆ setFetchFn()

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
void simox::caching::CacheMap< KeyT, ValueT, MapT >::setFetchFn ( FetchFn  fetchFn,
bool  clear = false 
)
inline

Set the fetch function, optionally clearing the cache (disabled by default).

◆ size()

template<class KeyT , class ValueT , template< class ... > class MapT = std::map>
size_t simox::caching::CacheMap< KeyT, ValueT, MapT >::size ( ) const
inline