boat
C++23 geospatial library
Loading...
Searching...
No Matches
cache.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_GUI_CACHES_CACHE_HPP
4#define BOAT_GUI_CACHES_CACHE_HPP
5
6#include <atomic>
7#include <boat/detail/any_hashable.hpp>
8
10
13 size_t requests = 0;
14 size_t misses = 0;
15};
16
17struct cache {
18 virtual ~cache() = default;
19
21 virtual std::any get(any_hashable const&) = 0;
22
23 virtual void put(any_hashable, std::any) = 0;
24
25 virtual cache_stats stats() const = 0;
26};
27
28namespace detail {
29
30auto get_or_invoke(cache* ptr, any_hashable const& key, auto&& fn)
31{
32 if (auto any = ptr ? ptr->get(key) : std::any{}; any.has_value())
33 return std::any_cast<decltype(fn())>(std::move(any));
34 auto ret = fn();
35 if (ptr)
36 ptr->put(key, ret);
37 return ret;
38}
39
40} // namespace detail
41
43inline size_t next_key()
44{
45 static constinit std::atomic_size_t seq_;
46 return ++seq_;
47}
48
49} // namespace boat::gui::caches
50
51#endif // BOAT_GUI_CACHES_CACHE_HPP
Definition cache.hpp:9
size_t next_key()
Process-wide sequence.
Definition cache.hpp:43
Counts get() calls and misses.
Definition cache.hpp:12
size_t requests
Definition cache.hpp:13
size_t misses
Definition cache.hpp:14
Definition cache.hpp:17
virtual std::any get(any_hashable const &)=0
Returns empty on a miss.
virtual ~cache()=default
virtual void put(any_hashable, std::any)=0
virtual cache_stats stats() const =0