41template <
typename Resource>
48 : names_{
std::move(names)} {
49 worker_ = std::jthread{[
this, &cache] {
50 for (
const std::string&
name : names_) {
53 const std::scoped_lock lock{mutex_};
54 handles_.emplace_back(
name, std::move(
handle));
55 }
catch (
const std::exception& error) {
56 const std::scoped_lock lock{mutex_};
57 failures_.push_back({.name =
name, .reason = error.what()});
60 const std::scoped_lock lock{mutex_};
61 failures_.push_back({.name =
name, .reason =
"unknown error"});
63 processed_.fetch_add(1, std::memory_order_release);
77 [[nodiscard]] std::size_t
total() const noexcept {
return names_.size(); }
81 [[nodiscard]] std::size_t
processed() const noexcept {
82 return processed_.load(std::memory_order_acquire);
87 [[nodiscard]]
float progress() const noexcept {
88 return names_.empty() ? 1.0F
98 if (worker_.joinable()) {
105 [[nodiscard]] std::vector<PreloadFailure>
failures()
const {
106 const std::scoped_lock lock{mutex_};
114 const std::scoped_lock lock{mutex_};
115 for (
const auto& [loaded, kept] : handles_) {
116 if (loaded ==
name) {
127 const std::scoped_lock lock{mutex_};
132 std::vector<std::string> names_;
133 mutable std::mutex mutex_;
134 std::vector<std::pair<std::string, typename ResourceCache<Resource>::Handle>> handles_;
135 std::vector<PreloadFailure> failures_;
136 std::atomic<std::size_t> processed_{0};
138 std::jthread worker_;
Loads a list of resources in the background, telling how far it is.
Definition preloader.hpp:42
Preloader(Preloader &&)=delete
Preloader(const Preloader &)=delete
void wait()
Waits until every name is dealt with.
Definition preloader.hpp:97
Preloader & operator=(Preloader &&)=delete
void release()
Lets go of the kept handles, once the game holds its own.
Definition preloader.hpp:125
std::size_t processed() const noexcept
Names dealt with so far, loaded or failed.
Definition preloader.hpp:81
ResourceCache< Resource >::Handle handle(std::string_view name) const
The kept handle of a loaded name.
Definition preloader.hpp:113
Preloader & operator=(const Preloader &)=delete
bool done() const noexcept
Tells whether every name was dealt with.
Definition preloader.hpp:94
std::size_t total() const noexcept
Names to load in all.
Definition preloader.hpp:77
float progress() const noexcept
How far the loading is.
Definition preloader.hpp:87
Preloader(ResourceCache< Resource > &cache, std::vector< std::string > names)
Starts loading at once, in its own thread.
Definition preloader.hpp:47
std::vector< PreloadFailure > failures() const
The resources that could not be loaded.
Definition preloader.hpp:105
~Preloader()=default
Waits for the loading thread.
Shares resources by name: each is loaded once, counted, and freed when unused.
Definition resource_cache.hpp:38
Handle load(std::string_view name)
Loads a resource, or shares the one already loaded or being loaded.
Definition resource_cache.hpp:60
std::shared_ptr< Resource > Handle
Handle of a loaded resource; the resource lives while handles do.
Definition resource_cache.hpp:41
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
Resources shared by name, loaded once and freed when nothing uses them.
A resource a Preloader could not load, and why.
Definition preloader.hpp:20
std::string name
Name given to the cache.
Definition preloader.hpp:22
std::string reason
what() of the loader's exception, or "unknown error" for one that is not an std::exception.
Definition preloader.hpp:25