Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::resource::ResourcePool< T > Class Template Reference

Typed resource pool with reference counting. More...

#include <ResourcePool.hpp>

Inheritance diagram for esengine::resource::ResourcePool< T >:

Classes

struct  Entry
 Entry storing a resource with metadata. More...

Public Member Functions

 ResourcePool ()
 ~ResourcePool () override=default
 ResourcePool (const ResourcePool &)=delete
ResourcePool & operator= (const ResourcePool &)=delete
 ResourcePool (ResourcePool &&)=default
ResourcePool & operator= (ResourcePool &&)=default
Handle< T > add (Unique< T > resource, const std::string &path="", usize bytes=0)
 Adds a resource to the pool.
template<typename Fn>
void forEachAlive (Fn &&fn)
 Visits every live resource, with the handle naming it.
template<typename Fn>
void forEachAlive (Fn &&fn) const
T * get (Handle< T > handle)
 Gets a resource by handle.
const T * get (Handle< T > handle) const
 Gets a resource by handle (const).
Handle< T > findByPath (const std::string &path) const
 Finds a resource by its cached path.
void setPath (Handle< T > handle, const std::string &path)
 Associates a path with an existing resource.
void addRef (Handle< T > handle)
 Increments the reference count for a resource.
void release (u32 id) override
 Decrements the reference count and frees if zero.
usize size () const override
 Gets the number of active resources.
void clear () override
 Releases all resources.
void setBudget (usize bytes)
 Sets the resident-byte budget. 0 (default) disables caching — a resource is freed the instant its refCount hits 0 (legacy behavior). When > 0, refCount==0 resources are retained as evictable cache entries and dropped (oldest-first) only when over budget.
usize budget () const
 The current resident-byte budget (0 = caching disabled).
usize trimEvictables ()
 Frees every evictable (refCount==0) cache entry immediately.
bool invalidatePath (const std::string &path)
 Severs a path's cache identity so the entry can't be revived.
usize residentBytes () const
 Bytes currently resident (held + evictable entries).
usize evictableCount () const
 Number of evictable (refCount==0, cached) entries.
bool isEvictable (Handle< T > handle) const
 True if the handle's entry is alive but evictable (cached).
u32 getRefCount (Handle< T > handle) const
 Gets the current reference count for a resource.
const std::string & getPath (Handle< T > handle) const
 Gets the cached path for a resource.
Public Member Functions inherited from esengine::resource::ResourcePoolBase
virtual ~ResourcePoolBase ()=default

Detailed Description

template<typename T>
class esengine::resource::ResourcePool< T >

Typed resource pool with reference counting.

Stores resources in a dense array with a free list for recycling slots. Supports optional path-based caching for deduplication.

Template Parameters
TThe resource type to manage (must be movable)
auto handle = shaders.add(Shader::create(...), "shaders/color.glsl");
Shader* ptr = shaders.get(handle);
shaders.release(handle.id());
GPU shader program for rendering.
Definition Shader.hpp:86
static Unique< Shader > create(GfxDevice &device, const std::string &vertexSrc, const std::string &fragmentSrc, GfxShaderLanguage language=GfxShaderLanguage::GLSL_ES300)
Creates a shader from source code strings.
Definition Shader.cpp:90
Handle< T > add(Unique< T > resource, const std::string &path="", usize bytes=0)
Adds a resource to the pool.
T * get(Handle< T > handle)
Gets a resource by handle.
void release(u32 id) override
Decrements the reference count and frees if zero.

Definition at line 85 of file ResourcePool.hpp.

Constructor & Destructor Documentation

◆ ResourcePool() [1/3]

template<typename T>
esengine::resource::ResourcePool< T >::ResourcePool ( )
inline

Definition at line 101 of file ResourcePool.hpp.

Referenced by ResourcePool(), ResourcePool(), operator=(), and operator=().

◆ ~ResourcePool()

template<typename T>
esengine::resource::ResourcePool< T >::~ResourcePool ( )
overridedefault

◆ ResourcePool() [2/3]

template<typename T>
esengine::resource::ResourcePool< T >::ResourcePool ( const ResourcePool< T > & )
delete

References ResourcePool().

◆ ResourcePool() [3/3]

template<typename T>
esengine::resource::ResourcePool< T >::ResourcePool ( ResourcePool< T > && )
default

References ResourcePool().

Member Function Documentation

◆ add()

template<typename T>
Handle< T > esengine::resource::ResourcePool< T >::add ( Unique< T > resource,
const std::string & path = "",
usize bytes = 0 )
inline

Adds a resource to the pool.

Parameters
resourceThe resource to add (takes ownership)
pathOptional path for cache lookup
bytesResource size for the eviction budget (0 = untracked)
Returns
Handle to the added resource

Definition at line 119 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::fromParts().

◆ addRef()

template<typename T>
void esengine::resource::ResourcePool< T >::addRef ( Handle< T > handle)
inline

Increments the reference count for a resource.

Parameters
handleThe resource handle

Definition at line 236 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::generation(), esengine::resource::Handle< T >::index(), and esengine::resource::Handle< T >::isValid().

◆ budget()

template<typename T>
usize esengine::resource::ResourcePool< T >::budget ( ) const
inline

The current resident-byte budget (0 = caching disabled).

Definition at line 317 of file ResourcePool.hpp.

◆ clear()

template<typename T>
void esengine::resource::ResourcePool< T >::clear ( )
inlineoverridevirtual

Releases all resources.

Implements esengine::resource::ResourcePoolBase.

Definition at line 291 of file ResourcePool.hpp.

◆ evictableCount()

template<typename T>
usize esengine::resource::ResourcePool< T >::evictableCount ( ) const
inline

Number of evictable (refCount==0, cached) entries.

Definition at line 366 of file ResourcePool.hpp.

◆ findByPath()

template<typename T>
Handle< T > esengine::resource::ResourcePool< T >::findByPath ( const std::string & path) const
inline

Finds a resource by its cached path.

Parameters
pathThe path to look up
Returns
Handle to the resource, or invalid handle if not found

Definition at line 202 of file ResourcePool.hpp.

◆ forEachAlive() [1/2]

template<typename T>
template<typename Fn>
void esengine::resource::ResourcePool< T >::forEachAlive ( Fn && fn)
inline

Visits every live resource, with the handle naming it.

For sweeps that must reach resources by identity rather than by path, including the ones created from pixels and never given one.

Definition at line 147 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::fromParts(), esengine::resource::ResourcePool< T >::Entry::generation, and esengine::resource::ResourcePool< T >::Entry::resource.

◆ forEachAlive() [2/2]

template<typename T>
template<typename Fn>
void esengine::resource::ResourcePool< T >::forEachAlive ( Fn && fn) const
inline

◆ get() [1/2]

template<typename T>
T * esengine::resource::ResourcePool< T >::get ( Handle< T > handle)
inline

Gets a resource by handle.

Parameters
handleThe resource handle
Returns
Pointer to the resource, or nullptr if invalid

Definition at line 170 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::generation(), esengine::resource::Handle< T >::index(), and esengine::resource::Handle< T >::isValid().

◆ get() [2/2]

template<typename T>
const T * esengine::resource::ResourcePool< T >::get ( Handle< T > handle) const
inline

Gets a resource by handle (const).

Parameters
handleThe resource handle
Returns
Const pointer to the resource, or nullptr if invalid

Definition at line 186 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::generation(), esengine::resource::Handle< T >::index(), and esengine::resource::Handle< T >::isValid().

◆ getPath()

template<typename T>
const std::string & esengine::resource::ResourcePool< T >::getPath ( Handle< T > handle) const
inline

Gets the cached path for a resource.

Parameters
handleThe resource handle
Returns
The path, or empty string if not found

Definition at line 400 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::generation(), esengine::resource::Handle< T >::index(), and esengine::resource::Handle< T >::isValid().

◆ getRefCount()

template<typename T>
u32 esengine::resource::ResourcePool< T >::getRefCount ( Handle< T > handle) const
inline

Gets the current reference count for a resource.

Parameters
handleThe resource handle
Returns
Reference count, or 0 if invalid

Definition at line 386 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::generation(), esengine::resource::Handle< T >::index(), and esengine::resource::Handle< T >::isValid().

◆ invalidatePath()

template<typename T>
bool esengine::resource::ResourcePool< T >::invalidatePath ( const std::string & path)
inline

Severs a path's cache identity so the entry can't be revived.

For hot reload: the bytes behind path changed, so the cached resource must never be handed out for that path again. Removes the path mapping; an evictable entry is freed immediately, a held entry stays alive for its current holders but loses its path, so its final release frees it instead of caching it.

Parameters
pathThe path to invalidate
Returns
True if a mapping for path existed

Definition at line 346 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::extractGeneration(), and esengine::resource::Handle< T >::extractIndex().

◆ isEvictable()

template<typename T>
bool esengine::resource::ResourcePool< T >::isEvictable ( Handle< T > handle) const
inline

True if the handle's entry is alive but evictable (cached).

Definition at line 373 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::generation(), esengine::resource::Handle< T >::index(), and esengine::resource::Handle< T >::isValid().

◆ operator=() [1/2]

template<typename T>
ResourcePool & esengine::resource::ResourcePool< T >::operator= ( const ResourcePool< T > & )
delete

References ResourcePool().

◆ operator=() [2/2]

template<typename T>
ResourcePool & esengine::resource::ResourcePool< T >::operator= ( ResourcePool< T > && )
default

References ResourcePool().

◆ release()

template<typename T>
void esengine::resource::ResourcePool< T >::release ( u32 id)
inlineoverridevirtual

Decrements the reference count and frees if zero.

Parameters
idThe resource identifier

Implements esengine::resource::ResourcePoolBase.

Definition at line 257 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::extractGeneration(), and esengine::resource::Handle< T >::extractIndex().

◆ residentBytes()

template<typename T>
usize esengine::resource::ResourcePool< T >::residentBytes ( ) const
inline

Bytes currently resident (held + evictable entries).

Definition at line 363 of file ResourcePool.hpp.

◆ setBudget()

template<typename T>
void esengine::resource::ResourcePool< T >::setBudget ( usize bytes)
inline

Sets the resident-byte budget. 0 (default) disables caching — a resource is freed the instant its refCount hits 0 (legacy behavior). When > 0, refCount==0 resources are retained as evictable cache entries and dropped (oldest-first) only when over budget.

Definition at line 311 of file ResourcePool.hpp.

◆ setPath()

template<typename T>
void esengine::resource::ResourcePool< T >::setPath ( Handle< T > handle,
const std::string & path )
inline

Associates a path with an existing resource.

Parameters
handleThe resource handle
pathThe path to associate

Definition at line 218 of file ResourcePool.hpp.

References esengine::resource::Handle< T >::generation(), esengine::resource::Handle< T >::id(), esengine::resource::Handle< T >::index(), and esengine::resource::Handle< T >::isValid().

◆ size()

template<typename T>
usize esengine::resource::ResourcePool< T >::size ( ) const
inlineoverridevirtual

Gets the number of active resources.

Returns
Count of non-freed resources

Implements esengine::resource::ResourcePoolBase.

Definition at line 284 of file ResourcePool.hpp.

◆ trimEvictables()

template<typename T>
usize esengine::resource::ResourcePool< T >::trimEvictables ( )
inline

Frees every evictable (refCount==0) cache entry immediately.

The memory-pressure response: held resources are untouched, but the whole warm cache is dropped regardless of the budget. The budget itself is unchanged — the cache refills as resources are released afterwards.

Returns
Number of entries freed

Definition at line 327 of file ResourcePool.hpp.


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