template<typename Key, typename Value>
class hilti::util::Cache< Key, Value >
Simple cache to remember a computed value for a given key.
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.