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
#include <functional>
9
#include <type_traits>
10
11
namespace
boat::gui::caches
{
12
13
struct
cache
{
14
virtual
~cache
() =
default
;
15
17
virtual
std::any
get
(any_hashable
const
&) = 0;
18
19
virtual
void
put
(any_hashable, std::any) = 0;
20
};
21
22
template
<
class
F,
class
... Args>
23
auto
get_or_invoke
(
cache
* ptr, any_hashable
const
& key, F&& f, Args&&... args)
24
{
25
if
(
auto
any = ptr ? ptr->
get
(key) : std::any{}; any.has_value())
26
return
std::any_cast<std::decay_t<std::invoke_result_t<F, Args...>>>(
27
std::move(any));
28
auto
ret = std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
29
if
(ptr)
30
ptr->
put
(key, ret);
31
return
ret;
32
}
33
34
inline
size_t
next_key
()
35
{
36
static
constinit
std::atomic_size_t seq_;
37
return
++seq_;
38
}
39
40
}
// namespace boat::gui::caches
41
42
#endif
// BOAT_GUI_CACHES_CACHE_HPP
boat::gui::caches
Definition
cache.hpp:11
boat::gui::caches::get_or_invoke
auto get_or_invoke(cache *ptr, any_hashable const &key, F &&f, Args &&... args)
Definition
cache.hpp:23
boat::gui::caches::next_key
size_t next_key()
Definition
cache.hpp:34
boat::gui::caches::cache
Definition
cache.hpp:13
boat::gui::caches::cache::get
virtual std::any get(any_hashable const &)=0
Returns an empty std::any on a miss.
boat::gui::caches::cache::~cache
virtual ~cache()=default
boat::gui::caches::cache::put
virtual void put(any_hashable, std::any)=0
include
boat
gui
caches
cache.hpp
Generated by
1.15.0