14 template<
typename Key,
typename Value>
17 using Callback1 = std::function<Value()>;
18 using Callback2 = std::function<Value(Value& v)>;
23 bool has(
const Key& key)
const {
return _cache.find(key) != _cache.end(); }
29 std::optional<Value>
get(
const Key& key, std::optional<Value> default_ = {})
const {
30 if (
auto i = _cache.find(key); i != _cache.end() )
33 return std::move(default_);
41 const Value&
getOrCreate(
const Key& key,
const Callback1& cb) {
42 if (
auto i = _cache.find(key); i != _cache.end() )
45 return put(key, cb());
60 const Value&
getOrCreate(
const Key& key,
const Callback1& cb1,
const Callback2& cb2) {
61 if (
auto i = _cache.find(key); i != _cache.end() )
65 return _cache[key] = cb2(_cache[key]);
69 const Value&
put(
const Key& key, Value value) {
return _cache[key] = std::move(value); }
72 void remove(
const Key& key) { _cache.erase(key); }
75 std::map<Key, Value> _cache;
bool has(const Key &key) const
Definition: cache.h:23
const Value & getOrCreate(const Key &key, const Callback1 &cb)
Definition: cache.h:41
const Value & getOrCreate(const Key &key, const Callback1 &cb1, const Callback2 &cb2)
Definition: cache.h:60
const Value & put(const Key &key, Value value)
Definition: cache.h:69