9 namespace hilti::util {
12 template<
typename Key,
typename Value>
18 bool has(
const Key& key)
const {
return _cache.find(key) != _cache.end(); }
24 std::optional<Value>
get(
const Key& key, std::optional<Value> default_ = {})
const {
25 if (
auto i = _cache.find(key); i != _cache.end() )
28 return std::move(default_);
36 template<
typename Callback>
38 if (
auto i = _cache.find(key); i != _cache.end() )
41 return put(key, cb());
56 template<
typename Callback1,
typename Callback2>
57 const Value&
getOrCreate(
const Key& key, Callback1&& cb1, Callback2&& cb2) {
58 if (
auto i = _cache.find(key); i != _cache.end() )
62 return _cache[key] = cb2(_cache[key]);
66 const Value&
put(
const Key& key, Value value) {
return _cache[key] = std::move(value); }
69 void remove(
const Key& key) { _cache.erase(key); }
72 std::map<Key, Value> _cache;
bool has(const Key &key) const
Definition: cache.h:18
void remove(const Key &key)
Definition: cache.h:69
const Value & getOrCreate(const Key &key, Callback &&cb)
Definition: cache.h:37
std::optional< Value > get(const Key &key, std::optional< Value > default_={}) const
Definition: cache.h:24
const Value & getOrCreate(const Key &key, Callback1 &&cb1, Callback2 &&cb2)
Definition: cache.h:57
const Value & put(const Key &key, Value value)
Definition: cache.h:66