Spicy
Public Member Functions | List of all members
hilti::util::Cache< Key, Value > Class Template Reference

#include <cache.h>

Inheritance diagram for hilti::util::Cache< Key, Value >:
hilti::util::Uniquer< std::string > hilti::util::Uniquer< hilti::ID >

Public Member Functions

bool has (const Key &key) const
 
std::optional< Value > get (const Key &key, std::optional< Value > default_={}) const
 
template<typename Callback >
const Value & getOrCreate (const Key &key, Callback &&cb)
 
template<typename Callback1 , typename Callback2 >
const Value & getOrCreate (const Key &key, Callback1 &&cb1, Callback2 &&cb2)
 
const Value & put (const Key &key, Value value)
 
void remove (const Key &key)
 

Detailed Description

template<typename Key, typename Value>
class hilti::util::Cache< Key, Value >

Simple cache to remember a computed value for a given key.

Member Function Documentation

◆ get()

template<typename Key , typename Value >
std::optional<Value> hilti::util::Cache< Key, Value >::get ( const Key &  key,
std::optional< Value >  default_ = {} 
) const
inline

Returns the value for a given key, or optionally a default if not found. Returning the default won't modify the cache.

◆ getOrCreate() [1/2]

template<typename Key , typename Value >
template<typename Callback >
const Value& hilti::util::Cache< Key, Value >::getOrCreate ( const Key &  key,
Callback &&  cb 
)
inline

Returns the value for a given key if it exists; or, if not, executes a callback to compute a value. In the latter case the computed value will be inserted into the cache before it's returned.

◆ getOrCreate() [2/2]

template<typename Key , typename Value >
template<typename Callback1 , typename Callback2 >
const Value& hilti::util::Cache< Key, Value >::getOrCreate ( const Key &  key,
Callback1 &&  cb1,
Callback2 &&  cb2 
)
inline

Returns the value for a given key if it exists; or, if not, executes a a couple callbacks to compute a value. This splits the computation into two parts to handle cases where it may recurse: the first callback computes a preliminary value v that will be inserted into the cache immediately. It will then be passed to the second callback to compute the final value. If that second callback accesses the cache with the same key during its operation, it will find v. The 2nd callbacks result will update the cache on completion, although usually it will probably just return v again to stay consistent.

◆ has()

template<typename Key , typename Value >
bool hilti::util::Cache< Key, Value >::has ( const Key &  key) const
inline

Returns true if the cache has an entry for a given key.

◆ put()

template<typename Key , typename Value >
const Value& hilti::util::Cache< Key, Value >::put ( const Key &  key,
Value  value 
)
inline

Stores a value for a key in the cache.

◆ remove()

template<typename Key , typename Value >
void hilti::util::Cache< Key, Value >::remove ( const Key &  key)
inline

Removes an item from the cache.


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