Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
resource_cache.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <cstddef>
6#include <cstdint>
7#include <exception>
8#include <functional>
9#include <future>
10#include <memory>
11#include <mutex>
12#include <stdexcept>
13#include <string>
14#include <string_view>
15#include <unordered_map>
16#include <utility>
17
18namespace aegis2d::core {
19
37template <typename Resource>
39public:
41 using Handle = std::shared_ptr<Resource>;
42
44 using Loader = std::function<Resource(std::string_view name)>;
45
49 explicit ResourceCache(Loader loader) : loader_{std::move(loader)} {
50 if (!loader_) {
51 throw std::invalid_argument{"ResourceCache: no loader"};
52 }
53 }
54
60 [[nodiscard]] Handle load(std::string_view name) {
61 const std::string key{name};
62 std::promise<Handle> promise;
63 std::shared_future<Handle> pending;
64 std::uint64_t mine = 0;
65 {
66 const std::scoped_lock lock{mutex_};
67 Entry& entry = entries_[key];
68 if (Handle alive = entry.resource.lock()) {
69 return alive;
70 }
71 if (entry.loading.valid()) {
72 pending = entry.loading;
73 } else {
74 mine = ++loads_;
75 entry.load = mine;
76 entry.loading = promise.get_future().share();
77 }
78 }
79 if (pending.valid()) {
80 return pending.get();
81 }
82 try {
83 Handle loaded = std::make_shared<Resource>(loader_(name));
84 {
85 // After a clear(), the entry may be gone or belong to a newer load: leave it.
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;
92 }
93 }
94 promise.set_value(loaded);
95 return loaded;
96 } catch (...) {
97 {
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);
102 }
103 }
104 promise.set_exception(std::current_exception());
105 throw;
106 }
107 }
108
113 [[nodiscard]] Handle find(std::string_view name) const {
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();
117 }
118
122 [[nodiscard]] bool loaded(std::string_view name) const { return find(name) != nullptr; }
123
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()
131 ? 0
132 : static_cast<std::size_t>(found->second.resource.use_count());
133 }
134
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()) {
142 ++alive;
143 }
144 }
145 return alive;
146 }
147
151 void clear() {
152 const std::scoped_lock lock{mutex_};
153 entries_.clear();
154 }
155
156private:
157 struct Entry {
158 // The resource while a handle keeps it alive.
159 std::weak_ptr<Resource> resource{};
160 // Valid while a thread loads it: the others wait for this load.
161 std::shared_future<Handle> loading{};
162 // Which load owns the entry, 0 for none: a load ends only its own entry.
163 std::uint64_t load = 0;
164 };
165
166 Loader loader_;
167 mutable std::mutex mutex_;
168 std::unordered_map<std::string, Entry> entries_;
169 std::uint64_t loads_ = 0;
170};
171
172} // namespace aegis2d::core
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