Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
Inkscape::Util::cached_map< Tk, Tv, Hash, Compare > Class Template Reference

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
 

Detailed Description

template<typename Tk, typename Tv, typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
class Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >

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.

Constructor & Destructor Documentation

◆ cached_map()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::cached_map ( std::size_t  max_cache_size = 32)
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.

Member Function Documentation

◆ add()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
auto Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::add ( Tk  key,
std::unique_ptr< Tv >  value 
)
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.

◆ clear()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
void Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::clear ( )
inline

◆ get_view()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
auto Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::get_view ( Item item)
inlineprivate

◆ lookup()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
auto Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::lookup ( Tk const &  key) -> std::shared_ptr<Tv>
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.

◆ pop_unused()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
void Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::pop_unused ( )
inlineprivate

◆ push_unused()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
void Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::push_unused ( Tv *  value)
inlineprivate

◆ remove_unused()

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
void Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::remove_unused ( Tv *  value)
inlineprivate

Member Data Documentation

◆ map

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
std::unordered_map<Tk, Item, Hash, Compare> Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::map
private

◆ max_cache_size

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
std::size_t const Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::max_cache_size
private

◆ unused

template<typename Tk , typename Tv , typename Hash = std::hash<Tk>, typename Compare = std::equal_to<Tk>>
std::deque<Tv*> Inkscape::Util::cached_map< Tk, Tv, Hash, Compare >::unused
private

The documentation for this class was generated from the following file: