vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::ResourceManager Class Reference

Open type-erased resource registry with typed handles and generational lifetimes. More...

#include <resource_manager.h>

Public Member Functions

template<typename ResourceType>
auto add (ResourceType &&resource)
 Put resource in the graph under its name, stamping its identity.
template<typename ResourceType>
auto add (ResourceType &&resource, std::string name)
 add() under name.
template<typename ResourceType>
auto addPrivate (ResourceType &&resource, std::string name)
 add() under name, setting Resource::isHidden: kept from the user and scene saves.
template<typename HandleType>
void remove (const HandleType &handle)
 Remove a resource: its name mapping, storage and handle slot.
template<typename HandleType>
bool isAlive (const HandleType &handle) const
 Whether handle still names a live resource of its type.
template<typename HandleType>
const auto & get (const HandleType &handle) const
 The resource handle names; asserts it is live (see isAlive(), tryGet()).
template<typename HandleType>
const auto * tryGet (const HandleType &handle) const
 The asset handle names, or null when it names none.
template<typename HandleType>
auto * tryEdit (const HandleType &handle)
 The asset handle names for editing, or null when it names none.
template<typename HandleType>
auto & edit (const HandleType &handle)
 Mutable access to the resource handle names.
template<typename HandleType>
void rename (const HandleType &handle, std::string newName)
 Rename a resource, keeping findByName consistent.
template<typename HandleType>
void swapValue (const HandleType &handle, typename HandleType::resource_t &value)
 Exchange the contents of the asset at handle with value, each keeping its identity, and commit.
template<typename HandleType>
void commit (const HandleType &handle)
 Bump the asset's version, which GLView::sync keys its GPU re-upload on.
template<typename T>
Handle< T > findByName (const std::string &name) const
 Find a resource by the name it was added or renamed under, in O(1).
template<typename T>
Handle< T > find (const AssetRef< T > &ref) const
 Resolve an authored reference to the asset it names.
template<typename T, typename Fn>
void forEachOfType (Fn &&fn) const
 Visit every live resource of type T; a linear scan for tooling, not hot paths.
template<typename T>
size_t countOfType () const
 How many resources of type T the graph holds.
void clear ()
 Drop every resource but the fonts, by swap() with an empty manager.
void swap (ResourceManager &other) noexcept
 Swap the asset graph with another ResourceManager, fonts excepted.
uint64_t epoch () const
 Identity of the current asset graph, bumped by every swap.

Detailed Description

Open type-erased resource registry with typed handles and generational lifetimes.

Per type, as in Scene: a SlotAllocator, a SparseSet<T> and a name index.

Only the engine's asset types (IS_ENGINE_ASSET) are stored, each slot made in vkm_core at construction. A SparseSet<T> carries the vtable of the binary that made it, so a slot a gameplay module made would outlive the module past dlclose; any other type is a compile error. See scripting.md, "Nothing may hold module code across the swap".

Member Function Documentation

◆ add() [1/2]

template<typename ResourceType>
auto Vkm::Engine::ResourceManager::add ( ResourceType && resource)
inline

Put resource in the graph under its name, stamping its identity.

The name is the identity (what a scene file stores), so naming one that exists replaces its contents in place and returns the existing handle; this keeps adding repeatable across a script reload, where onStart runs again. An unnamed asset gets a free name ("asset", "asset (2)", ...) instead. Either way it gets a fresh uid, so an async completion minted against a previous occupant can tell.

Template Parameters
ResourceTypeThe asset type, deriving from Resource.
Parameters
resourceThe asset to add (moved from).
Returns
The handle naming the asset - the existing one on a replace.

◆ add() [2/2]

template<typename ResourceType>
auto Vkm::Engine::ResourceManager::add ( ResourceType && resource,
std::string name )
inline

add() under name.

Template Parameters
ResourceTypeThe asset type, deriving from Resource.
Parameters
resourceThe asset to add (moved from).
nameName to stamp before insertion.
Returns
Handle for the asset now standing under name.

◆ addPrivate()

template<typename ResourceType>
auto Vkm::Engine::ResourceManager::addPrivate ( ResourceType && resource,
std::string name )
inline

add() under name, setting Resource::isHidden: kept from the user and scene saves.

Template Parameters
ResourceTypeThe asset type, deriving from Resource.
Parameters
resourceThe asset to add (moved from).
nameName to stamp before insertion.
Returns
Handle for the private asset.

◆ remove()

template<typename HandleType>
void Vkm::Engine::ResourceManager::remove ( const HandleType & handle)
inline

Remove a resource: its name mapping, storage and handle slot.

GPU memory is not freed: GLView reclaims a slot when its index is recycled, or all at once when the epoch moves. A stale handle asserts in debug and is a no-op in release: past the generation check everything addresses the slot index alone.

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleThe resource to remove; must still be live.

◆ isAlive()

template<typename HandleType>
bool Vkm::Engine::ResourceManager::isAlive ( const HandleType & handle) const
inline

Whether handle still names a live resource of its type.

For get/edit on handles that may have been freed since, chiefly in async completions.

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleAny value.
Returns
Whether handle names a live resource.

◆ get()

template<typename HandleType>
const auto & Vkm::Engine::ResourceManager::get ( const HandleType & handle) const
inline

The resource handle names; asserts it is live (see isAlive(), tryGet()).

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleThe resource to fetch.
Returns
The stored resource.

◆ tryGet()

template<typename HandleType>
const auto * Vkm::Engine::ResourceManager::tryGet ( const HandleType & handle) const
inline

The asset handle names, or null when it names none.

The tolerant get(), as Scene::tryGet: for a handle out of a component or cached state, which may be null or stale.

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleAny value; null or stale answers nullptr.
Returns
The asset, or nullptr.

◆ tryEdit()

template<typename HandleType>
auto * Vkm::Engine::ResourceManager::tryEdit ( const HandleType & handle)
inline

The asset handle names for editing, or null when it names none.

The tolerant edit(): commit() afterwards if the backend needs to see the change.

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleAny value.
Returns
The asset, or nullptr.

◆ edit()

template<typename HandleType>
auto & Vkm::Engine::ResourceManager::edit ( const HandleType & handle)
inline

Mutable access to the resource handle names.

Identity stays private to Resource: rename() changes a name, commit() tells the backend the contents changed.

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleThe resource; must still be live.
Returns
The stored resource.

◆ rename()

template<typename HandleType>
void Vkm::Engine::ResourceManager::rename ( const HandleType & handle,
std::string newName )
inline

Rename a resource, keeping findByName consistent.

newName may come straight from a text field: empty falls back to the generic base and a taken one gets a " (N)" suffix. Unlike add(), a taken name never replaces its holder - renaming A onto B's name is a slip, not a request to destroy B.

Parameters
handleThe asset to rename.
newNameDesired name; made unique.

◆ swapValue()

template<typename HandleType>
void Vkm::Engine::ResourceManager::swapValue ( const HandleType & handle,
typename HandleType::resource_t & value )
inline

Exchange the contents of the asset at handle with value, each keeping its identity, and commit.

Rebuilds an asset without reissuing it: handles and findByName keep naming it. The value's name, uid, hidden flag and version are traded back rather than riding in, which would break the name index and replay a version the backend has seen; value can be removed afterwards like any other asset.

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleThe asset to rebuild; must still be live.
valueThe new contents; receives the old ones.

◆ commit()

template<typename HandleType>
void Vkm::Engine::ResourceManager::commit ( const HandleType & handle)
inline

Bump the asset's version, which GLView::sync keys its GPU re-upload on.

Template Parameters
HandleTypeHandle type identifying the resource type.
Parameters
handleThe changed resource; must still be live.

◆ findByName()

template<typename T>
Handle< T > Vkm::Engine::ResourceManager::findByName ( const std::string & name) const
inline

Find a resource by the name it was added or renamed under, in O(1).

Template Parameters
TThe resource type.
Parameters
nameThe name to look up.
Returns
The handle, or an invalid one when the type is unregistered or nothing matches.

◆ find()

template<typename T>
Handle< T > Vkm::Engine::ResourceManager::find ( const AssetRef< T > & ref) const
inline

Resolve an authored reference to the asset it names.

m_clip = resources().find(chime); // AssetRef<AudioClipAsset> chime;
Template Parameters
TThe referenced asset type.
Parameters
refThe reference; an empty name answers an invalid handle.
Returns
The handle, or an invalid one when nothing has that name.

◆ forEachOfType()

template<typename T, typename Fn>
void Vkm::Engine::ResourceManager::forEachOfType ( Fn && fn) const
inline

Visit every live resource of type T; a linear scan for tooling, not hot paths.

A no-op when the type is unregistered.

Template Parameters
TThe resource type.
FnCallable taking (Handle<T>, const T&).
Parameters
fnCalled per resource in storage order; must not add or remove a T.

◆ countOfType()

template<typename T>
size_t Vkm::Engine::ResourceManager::countOfType ( ) const
inline

How many resources of type T the graph holds.

Returns
The count; 0 when the type is unregistered.

◆ clear()

void Vkm::Engine::ResourceManager::clear ( )

Drop every resource but the fonts, by swap() with an empty manager.

Nothing tracks handle holders: clear the scene first.

◆ swap()

void Vkm::Engine::ResourceManager::swap ( ResourceManager & other)
noexcept

Swap the asset graph with another ResourceManager, fonts excepted.

Commits a staging manager together with Scene::swap; on failure the staging one is dropped and the live state is untouched. Fonts stay: baked at startup, never in a scene file, and a UIText's font name must keep resolving. Handles from before the swap point into the OTHER manager; a cached hidden asset is re-acquired by findByName, then addPrivate.

Parameters
otherThe manager to trade graphs with.

◆ epoch()

uint64_t Vkm::Engine::ResourceManager::epoch ( ) const
inline

Identity of the current asset graph, bumped by every swap.

A new graph reuses indices, generations and version 1, so a backend cache must drop everything when this moves.

Returns
The current graph's epoch; never 0.

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