![]() |
vkmEngine 1.0.15
A C++ game engine · vkmengine.com
|
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 T, typename Make> | |
| Handle< T > | findOrAdd (const std::string &name, Make &&make) |
The asset under name, or the one make makes, added under it. | |
| 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. | |
| uint64_t | revision () const |
| Moved by every commit, swap, replacement and removal of an asset, and every swap of the graph. | |
| template<typename T> | |
| uint64_t | revisionOf () const |
revision() narrowed to T: moved by every commit, swap, replacement and removal of one of its assets. | |
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, "Unloading a module".
|
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.
| ResourceType | The asset type, deriving from Resource. |
| resource | The asset to add (moved from). |
|
inline |
|
inline |
The asset under name, or the one make makes, added under it.
For an asset made in code that may already be there - the library's, or made the first time round. What stands under the name is kept as it is: to change it in place, swapValue it.
| T | The asset type, deriving from Resource. |
| name | Name to look up, and to stamp on what make returns. |
| make | Callable returning a T, called only when nothing stands under name. |
name.
|
inline |
add() under name, setting Resource::isHidden: kept from the user and scene saves.
| ResourceType | The asset type, deriving from Resource. |
| resource | The asset to add (moved from). |
| name | Name to stamp before insertion. |
|
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.
| HandleType | Handle type identifying the resource type. |
| handle | The resource to remove; must still be live. |
|
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.
| HandleType | Handle type identifying the resource type. |
| handle | Any value. |
handle names a live resource.
|
inline |
|
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.
| HandleType | Handle type identifying the resource type. |
| handle | Any value; null or stale answers nullptr. |
|
inline |
|
inline |
Mutable access to the resource handle names.
Identity stays private to Resource: rename() changes a name, commit() tells the backend the contents changed.
| HandleType | Handle type identifying the resource type. |
| handle | The resource; must still be live. |
|
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.
| handle | The asset to rename. |
| newName | Desired name; made unique. |
|
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.
| HandleType | Handle type identifying the resource type. |
| handle | The asset to rebuild; must still be live. |
| value | The new contents; receives the old ones. |
|
inline |
Bump the asset's version, which GLView::sync keys its GPU re-upload on.
| HandleType | Handle type identifying the resource type. |
| handle | The changed resource; must still be live. |
|
inline |
Find a resource by the name it was added or renamed under, in O(1).
| T | The resource type. |
| name | The name to look up. |
|
inline |
Resolve an authored reference to the asset it names.
| T | The referenced asset type. |
| ref | The reference; an empty name answers an invalid handle. |
|
inline |
Visit every live resource of type T; a linear scan for tooling, not hot paths.
| T | The resource type. |
| Fn | Callable taking (Handle<T>, const T&). |
| fn | Called per resource in storage order; must not add or remove a T. |
|
inline |
How many resources of type T the graph holds.
| 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.
|
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.
| other | The manager to trade graphs with. |
|
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.
|
inline |
Moved by every commit, swap, replacement and removal of an asset, and every swap of the graph.
What a cache of values derived from assets - a mesh's box in world space - compares: one number for every asset, where each asset's version is its own.
|
inline |
revision() narrowed to T: moved by every commit, swap, replacement and removal of one of its assets.
For a cache that reads one kind of asset, which a change to another kind leaves standing. A swap of the graph brings the other graph's count, so compare epoch() too.
| T | The asset type. |