15#include <unordered_map>
37template <
typename Resource>
41 using Handle = std::shared_ptr<Resource>;
44 using Loader = std::function<Resource(std::string_view
name)>;
51 throw std::invalid_argument{
"ResourceCache: no loader"};
61 const std::string key{
name};
62 std::promise<Handle> promise;
63 std::shared_future<Handle> pending;
64 std::uint64_t mine = 0;
66 const std::scoped_lock lock{mutex_};
67 Entry& entry = entries_[key];
68 if (
Handle alive = entry.resource.lock()) {
71 if (entry.loading.valid()) {
72 pending = entry.loading;
76 entry.loading = promise.get_future().share();
79 if (pending.valid()) {
86 const std::scoped_lock lock{mutex_};
87 if (
const auto found = entries_.find(key);
88 found != entries_.end() && found->second.load == mine) {
89 found->second.resource =
loaded;
90 found->second.loading = {};
91 found->second.load = 0;
98 const std::scoped_lock lock{mutex_};
99 if (
const auto found = entries_.find(key);
100 found != entries_.end() && found->second.load == mine) {
101 entries_.erase(found);
104 promise.set_exception(std::current_exception());
114 const std::scoped_lock lock{mutex_};
115 const auto found = entries_.find(std::string{
name});
116 return found == entries_.end() ? nullptr : found->second.resource.lock();
127 [[nodiscard]] std::size_t
uses(std::string_view
name)
const {
128 const std::scoped_lock lock{mutex_};
129 const auto found = entries_.find(std::string{
name});
130 return found == entries_.end()
132 :
static_cast<std::size_t
>(found->second.resource.use_count());
137 [[nodiscard]] std::size_t
size()
const {
138 const std::scoped_lock lock{mutex_};
139 std::size_t alive = 0;
140 for (
const auto& [
name, entry] : entries_) {
141 if (!entry.resource.expired()) {
152 const std::scoped_lock lock{mutex_};
159 std::weak_ptr<Resource> resource{};
161 std::shared_future<Handle> loading{};
163 std::uint64_t load = 0;
167 mutable std::mutex mutex_;
168 std::unordered_map<std::string, Entry> entries_;
169 std::uint64_t loads_ = 0;
Shares resources by name: each is loaded once, counted, and freed when unused.
Definition resource_cache.hpp:38
void clear()
Forgets every entry; the handles given out keep their resources alive.
Definition resource_cache.hpp:151
Handle load(std::string_view name)
Loads a resource, or shares the one already loaded or being loaded.
Definition resource_cache.hpp:60
std::size_t size() const
Resources loaded now.
Definition resource_cache.hpp:137
Handle find(std::string_view name) const
The resource of a name, without loading it nor waiting for its load.
Definition resource_cache.hpp:113
ResourceCache(Loader loader)
Creates an empty cache.
Definition resource_cache.hpp:49
std::shared_ptr< Resource > Handle
Handle of a loaded resource; the resource lives while handles do.
Definition resource_cache.hpp:41
std::function< Resource(std::string_view name)> Loader
Builds the resource of a name; its exceptions reach load()'s caller.
Definition resource_cache.hpp:44
std::size_t uses(std::string_view name) const
Handles keeping a resource alive.
Definition resource_cache.hpp:127
bool loaded(std::string_view name) const
Tells whether a resource is loaded now.
Definition resource_cache.hpp:122
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
std::string_view name() noexcept
Name of the module, used to check that it is linked.
Definition endpoint.hpp:85