Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::ResourceCache< Resource > Class Template Reference

Shares resources by name: each is loaded once, counted, and freed when unused. More...

#include <aegis2d/core/resource_cache.hpp>

Public Types

using Handle = std::shared_ptr< Resource >
 Handle of a loaded resource; the resource lives while handles do.
 
using Loader = std::function< Resource(std::string_view name)>
 Builds the resource of a name; its exceptions reach load()'s caller.
 

Public Member Functions

 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.
 

Detailed Description

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.

core::ResourceCache<Image> images{[](std::string_view name) {
return Image::load(assets_dir() / name);
}};
const auto hero = images.load("hero.png"); // loaded now
const auto same = images.load("hero.png"); // shared, not reloaded
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.

Member Typedef Documentation

◆ Handle

template<typename Resource >
using aegis2d::core::ResourceCache< Resource >::Handle = std::shared_ptr<Resource>

Handle of a loaded resource; the resource lives while handles do.

◆ Loader

template<typename Resource >
using aegis2d::core::ResourceCache< Resource >::Loader = std::function<Resource(std::string_view name)>

Builds the resource of a name; its exceptions reach load()'s caller.

Constructor & Destructor Documentation

◆ ResourceCache()

template<typename Resource >
aegis2d::core::ResourceCache< Resource >::ResourceCache ( Loader  loader)
inlineexplicit

Creates an empty cache.

Parameters
loaderHow a name becomes a resource, such as reading a file.
Exceptions
std::invalid_argumentif loader is empty.

Member Function Documentation

◆ clear()

template<typename Resource >
void aegis2d::core::ResourceCache< Resource >::clear ( )
inline

Forgets every entry; the handles given out keep their resources alive.

A load running meanwhile still gives its resource to its callers, but does not cache it: the next load() of that name loads it again.

◆ find()

template<typename Resource >
Handle aegis2d::core::ResourceCache< Resource >::find ( std::string_view  name) const
inline

The resource of a name, without loading it nor waiting for its load.

Parameters
nameName given to load().
Returns
A handle to the cached resource, or nothing when no handle keeps it alive (being loaded included).

◆ load()

template<typename Resource >
Handle aegis2d::core::ResourceCache< Resource >::load ( std::string_view  name)
inline

Loads a resource, or shares the one already loaded or being loaded.

Parameters
nameName the loader understands, such as a file name.
Returns
A handle to the cached resource, loaded now if nothing was using it.
Exceptions
whateverthe loader throws, to every thread waiting for that load; nothing is cached then.

◆ loaded()

template<typename Resource >
bool aegis2d::core::ResourceCache< Resource >::loaded ( std::string_view  name) const
inline

Tells whether a resource is loaded now.

Parameters
nameName given to load().
Returns
true while at least one handle keeps it alive.

◆ size()

template<typename Resource >
std::size_t aegis2d::core::ResourceCache< Resource >::size ( ) const
inline

Resources loaded now.

Returns
How many names have a handle alive.

◆ uses()

template<typename Resource >
std::size_t aegis2d::core::ResourceCache< Resource >::uses ( std::string_view  name) const
inline

Handles keeping a resource alive.

Parameters
nameName given to load().
Returns
Their count; 0 for a resource freed or never loaded.

The documentation for this class was generated from the following file: