Shares resources by name: each is loaded once, counted, and freed when unused.
More...
|
| | ResourceCache (Loader loader) |
| | Creates an empty cache.
|
| |
| Handle | load (std::string_view name) |
| | Loads a resource, or shares the one already loaded or being loaded.
|
| |
| Handle | find (std::string_view name) const |
| | The resource of a name, without loading it nor waiting for its load.
|
| |
| bool | loaded (std::string_view name) const |
| | Tells whether a resource is loaded now.
|
| |
| std::size_t | uses (std::string_view name) const |
| | Handles keeping a resource alive.
|
| |
| std::size_t | size () const |
| | Resources loaded now.
|
| |
| void | clear () |
| | Forgets every entry; the handles given out keep their resources alive.
|
| |
template<typename Resource>
class aegis2d::core::ResourceCache< Resource >
Shares resources by name: each is loaded once, counted, and freed when unused.
load() gives a handle (a std::shared_ptr) to the named resource: the cached one while handles to it live, a fresh call of the loader otherwise. The handles are the reference counts; when the last one goes, the resource is freed, and the next load() loads it again. The cache can be shared between threads, and the loader runs outside its lock: while one thread loads a name, the others load other names, and find(), loaded() and uses() answer at once, so a preloading thread can fill the cache while the game reads it. Threads asking for a name being loaded wait for that load and share its resource, or its exception, so each name is loaded once. A loader may load other names of its own cache, never the name it is loading.
return Image::load(assets_dir() /
name);
}};
const auto hero = images.load("hero.png");
const auto same = images.load("hero.png");
Shares resources by name: each is loaded once, counted, and freed when unused.
Definition resource_cache.hpp:38
std::string_view name() noexcept
Name of the module, used to check that it is linked.