13#ifndef INKSCAPE_UTIL_CACHED_MAP_H
14#define INKSCAPE_UTIL_CACHED_MAP_H
16#include <unordered_map>
48template <
typename Tk,
typename Tv,
typename Hash = std::hash<Tk>,
typename Compare = std::equal_to<Tk>>
66 auto add(Tk
key, std::unique_ptr<Tv> value)
68 auto ret =
map.emplace(std::move(
key), std::move(value));
79 if (
auto it =
map.find(
key); it !=
map.end()) {
101 std::unordered_map<Tk, Item, Hash, Compare>
map;
106 if (
auto view =
item.view.lock()) {
110 auto new_view = std::shared_ptr<Tv>(
item.value.get(), [
this] (Tv *value) {
113 item.view = new_view;
120 auto it = std::find(
unused.begin(),
unused.end(), value);
128 unused.emplace_back(value);
136 auto value =
unused.front();
137 map.erase(std::find_if(
map.begin(),
map.end(), [value] (
auto const &it) {
138 return it.second.value.get() == value;
A cached_map<Tk, Tv> is designed for use by a factory that takes as input keys of type Tk and produce...
void remove_unused(Tv *value)
auto get_view(Item &item)
cached_map(std::size_t max_cache_size=32)
Construct an empty cached_map.
auto lookup(Tk const &key) -> std::shared_ptr< Tv >
Look up a key in the map.
auto add(Tk key, std::unique_ptr< Tv > value)
Given a key and a unique_ptr to a value, inserts them into the map, or discards them if the key is al...
std::deque< Tv * > unused
void push_unused(Tv *value)
std::unordered_map< Tk, Item, Hash, Compare > map
std::size_t const max_cache_size
Miscellaneous supporting code.
Helper class to stream background task notifications as a series of messages.
static cairo_user_data_key_t key
std::unique_ptr< Tv > value
Item(decltype(value) value)