Inkscape
Vector Graphics Editor
|
A cached_map<Tk, Tv> is designed for use by a factory that takes as input keys of type Tk and produces objects of type std::unique_ptr<Tv> in response. More...
#include <cached_map.h>
Classes | |
struct | Item |
Public Member Functions | |
cached_map (std::size_t max_cache_size=32) | |
Construct an empty cached_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 already present. | |
auto | lookup (Tk const &key) -> std::shared_ptr< Tv > |
Look up a key in the map. | |
void | clear () |
Private Member Functions | |
auto | get_view (Item &item) |
void | remove_unused (Tv *value) |
void | push_unused (Tv *value) |
void | pop_unused () |
Private Attributes | |
std::size_t const | max_cache_size |
std::unordered_map< Tk, Item, Hash, Compare > | map |
std::deque< Tv * > | unused |
A cached_map<Tk, Tv> is designed for use by a factory that takes as input keys of type Tk and produces objects of type std::unique_ptr<Tv> in response.
It allows such a factory to remember a finite number of previously constructed objects for later re-use.
Upon constructing an object v for key k for the first time, calling
my_ptr = my_cached_map.add(k, std::move(v));
will add it to the cache, returning a std::shared_ptr<Tv> by which it can now be accessed.
To re-use an object that might be in the cache, use
my_ptr = my_cached_map.lookup(k)
When all copies of the shared_ptr my_ptr have expired, the object is marked as unused. However it is not immediately deleted. As further objects are marked as unused, the oldest unused objects are gradually deleted, with their number never exceeding the value max_cache_size.
Note that the cache must not be destroyed while any shared pointers to any of its objects are still active. This is in accord with its expected usage; if the factory loads objects from an external library, then it should be safe to destroy the cache just before the library is unloaded, as the objects should no longer be in use at that point anyway.
Definition at line 49 of file cached_map.h.
|
inline |
Construct an empty cached_map.
The optional max_cache_size argument specifies the maximum number of unused elements which will be kept in memory.
Definition at line 58 of file cached_map.h.
|
inline |
Given a key and a unique_ptr to a value, inserts them into the map, or discards them if the key is already present.
Returns a non-null shared_ptr to the new value in the map corresponding to key.
Definition at line 66 of file cached_map.h.
References Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::get_view(), key, and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::map.
|
inline |
Definition at line 86 of file cached_map.h.
References Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::map, and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::unused.
|
inlineprivate |
Definition at line 104 of file cached_map.h.
References item, and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::remove_unused().
Referenced by Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::add(), and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::lookup().
|
inline |
Look up a key in the map.
Returns a shared pointer to the corresponding value, or null if the key is not present.
Definition at line 77 of file cached_map.h.
References Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::get_view(), key, and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::map.
|
inlineprivate |
Definition at line 134 of file cached_map.h.
References Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::map, and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::unused.
Referenced by Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::push_unused().
|
inlineprivate |
|
inlineprivate |
Definition at line 118 of file cached_map.h.
References Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::unused.
Referenced by Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::get_view().
|
private |
Definition at line 101 of file cached_map.h.
Referenced by Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::add(), Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::clear(), Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::lookup(), and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::pop_unused().
|
private |
Definition at line 100 of file cached_map.h.
Referenced by Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::push_unused().
|
private |
Definition at line 102 of file cached_map.h.
Referenced by Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::clear(), Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::pop_unused(), Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::push_unused(), and Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::remove_unused().