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

#include <cache.h>

Public Types

using Callback1 = std::function< Value()>
 
using Callback2 = std::function< Value(Value &v)>
 

Public Member Functions

bool has (const Key &key) const
 
std::optional< Value > get (const Key &key, std::optional< Value > default_={}) const
 
const Value & getOrCreate (const Key &key, const Callback1 &cb)
 
const Value & getOrCreate (const Key &key, const Callback1 &cb1, const 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>
const Value& hilti::util::Cache< Key, Value >::getOrCreate ( const Key &  key,
const Callback1 &  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>
const Value& hilti::util::Cache< Key, Value >::getOrCreate ( const Key &  key,
const Callback1 &  cb1,
const 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 prelimary 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: