Engine
Raylib based game framework
Loading...
Searching...
No Matches
ResourceCache< Resource > Class Template Reference

Thread-safe cache for a single resource type. More...

#include <ResourceManager.hpp>

Inheritance diagram for ResourceCache< Resource >:
Collaboration diagram for ResourceCache< Resource >:

Public Member Functions

 ResourceCache (const std::function< std::optional< Resource >(const std::string &)> &loadFunction, const std::function< void(Resource)> &unloadFunction)
 Constructs a cache with the given load and unload functions.
 ResourceCache (const ResourceCache &)=delete
ResourceCacheoperator= (const ResourceCache &)=delete
std::shared_ptr< Resource > Get (const std::string &name)
 Looks up a cached resource by name without loading it.
std::shared_ptr< Resource > Load (const std::string &path)
 Loads a resource from disk and caches it under its path.
std::shared_ptr< Resource > Add (Resource &&object, const std::string &name)
 Stores an already-constructed resource under an arbitrary name.
void Remove (const std::string &path)
 Removes a resource from the cache.

Detailed Description

template<class Resource>
class ResourceCache< Resource >

Thread-safe cache for a single resource type.

Resources are loaded on demand, stored by path, and automatically unloaded when the last shared_ptr to them is released.

All public methods are safe to call from multiple threads concurrently.

Template Parameters
ResourceThe resource type to cache (e.g. Texture2D, Sound)

Constructor & Destructor Documentation

◆ ResourceCache()

template<class Resource>
ResourceCache< Resource >::ResourceCache ( const std::function< std::optional< Resource >(const std::string &)> & loadFunction,
const std::function< void(Resource)> & unloadFunction )
inline

Constructs a cache with the given load and unload functions.

Parameters
loadFunctionCalled with a file path; returns the resource or nullopt on failure
unloadFunctionCalled when the last reference to a resource is dropped

Member Function Documentation

◆ Add()

template<class Resource>
std::shared_ptr< Resource > ResourceCache< Resource >::Add ( Resource && object,
const std::string & name )
inline

Stores an already-constructed resource under an arbitrary name.

Useful for runtime-generated resources that have no associated file path.

Parameters
objectResource to store (moved into the cache)
nameKey to store the resource under
Returns
Shared pointer to the stored resource

◆ Get()

template<class Resource>
std::shared_ptr< Resource > ResourceCache< Resource >::Get ( const std::string & name)
inline

Looks up a cached resource by name without loading it.

Parameters
nameKey the resource was stored under (typically its file path)
Returns
Shared pointer to the resource, or empty if not cached

◆ Load()

template<class Resource>
std::shared_ptr< Resource > ResourceCache< Resource >::Load ( const std::string & path)
inline

Loads a resource from disk and caches it under its path.

If the resource is already cached it is returned immediately without reloading.

Parameters
pathFile path passed to the load function
Returns
Shared pointer to the resource, or empty if loading failed

◆ Remove()

template<class Resource>
void ResourceCache< Resource >::Remove ( const std::string & path)
inline

Removes a resource from the cache.

The resource is unloaded when the last shared_ptr to it is released. Existing shared_ptrs remain valid after removal.

Parameters
pathKey the resource was stored under

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