Cogs.Core
|
The generic resource manager provides a base implementation for specialized resource managers to build on. More...
#include <ResourceManager.h>
Classes | |
struct | SwapOperation |
Defines a swap operation replacing the contents of a resource. More... | |
Public Types | |
using | ResourceHandle = ResourceHandle_t< ResourceType > |
Type of handle used to hold resources. | |
using | ResourceProxy = ResourceProxy< ResourceType, ResourceManager > |
Type of resource proxy objects, specialized on the type of resource. | |
Public Member Functions | |
ResourceManager (Context *context, Collections::ElementOffset capacity=1024) | |
Constructs a resource manager with the given context. | |
~ResourceManager () override | |
Destructs a resource manager, provided for destruction via base pointer. | |
void | clear () override |
Clear the resource manager, cleaning up resources held by member handles. | |
void | clearSwapping () override |
Clear the swap queue, ensuring indirect resource references are released. | |
ResourceHandle | create () |
Create a new resource. | |
ResourceHandle | release (ResourceId resourceId) |
ResourceHandle | loadResource (LoadInfoType *loadInfo) |
ResourceProxy | createLocked () |
Creates a new resource, but returns a ResourceProxy for thread safe editing instead of a handle to the actual resource. | |
ResourceProxy | lock (const ResourceHandle &handle) |
Takes the handle given and returns a resource proxy to edit the contents of the held resource safely on other threads than the main thread. | |
void | unlockProxy (const ResourceHandle &handle, const ResourceHandle &proxy) |
Unlock the given resource proxy. | |
ResourceHandle | generateHandle (ResourceBase *resource) |
Generate a handle to the given resource. | |
virtual void | handleLoad (LoadInfoType *) |
Handler for resource loading. | |
virtual void | handleFailedLoad (const LoadInfoType *) |
Handler for failed resource loads using the given loadInfo. | |
virtual ActivationResult | handleActivation (ResourceHandle, ResourceType *) |
Handler for activation of resources. | |
virtual void | handleDeletion (ResourceType *) |
Handler for deletion of resources. | |
ResourceType * | get (const ResourceHandleBase &handle) |
Resolve the given handle to a resource. | |
void | processSwapping () override |
Process queued swap operations, exchanging resource contents of the destination resources by those indicated in the operation data. | |
ResourceHandle | getHandle (const ResourceId id) const |
Get a resource handle to the resource with the given id. | |
void | registerLoader (IResourceLoader< ResourceType, LoadInfoType > *loader) |
Register a resource loader capable of handling resources of the template type. | |
void | destroyLoadInfoInternal (ResourceLoadInfoBase *loadInfo) override |
LoadInfoType * | createLoadInfo () |
![]() | |
ResourceManagerBase (Context *context) | |
void | initialize () override |
void | clear () override |
void | reportLeaks (std::string_view typeName) |
std::string | getReport () |
Return a string with current resource usage. | |
bool | checkPreloaded (ResourceLoadInfoBase *loadInfo) |
ResourceHandleBase | releaseInternal (ResourceId resourceId) |
Releases a resourceId -> ResourceHandle mapping. | |
ResourceHandleBase | loadResourceInternal (ResourceLoadInfoBase *loadInfo) |
Load a resource using the given loadInfo. | |
void | processLoading () final |
Process loading resources. | |
void | setProcessed (ResourceLoadInfoBase *loadInfo, bool swapping=false) |
Signal the resource being loaded as done loading and ready for activation etc. | |
void | activateResources () final |
virtual void | processDeletion () override |
Process resources pending deallocation. | |
void | fillDeletionQueue (std::vector< ResourceBase * > &deletion) |
void | destroyLocked (ResourceBase *resource) |
void | safeDestroy (ResourceBase *resource) |
ResourceId | getNextResourceId () override |
Get the next unique resource id. | |
void | resourceChanged (ResourceBase *resource) override |
void | resourceDeleted (ResourceBase *resource) override |
void | setResourceId (ResourceBase *resource, ResourceId id) override |
Assign the given id to a previously created resource. | |
void | queueResource (ResourceLoadInfoBase *loadInfo) |
Re-queue the given loadInfo to defer loading to the next time the processLoading() phase is performed. | |
virtual void | handleLoadInternal (ResourceLoadInfoBase *loadInfo)=0 |
virtual ActivationResult | handleActivationInternal (ResourceHandleBase r, ResourceBase *resource)=0 |
virtual void | handleDeletionInternal (ResourceBase *resource)=0 |
virtual void | handleFailedLoadInternal (ResourceLoadInfoBase *loadInfo)=0 |
virtual void | destroyLoadInfoInternal (ResourceLoadInfoBase *loadInfo)=0 |
virtual bool | shouldAutoReload () const |
virtual bool | shouldMergeBySource () const |
virtual void | handleReload (ResourceHandleBase) |
std::vector< ResourceBase * > | getAllocatedResources () const |
ResourceHandleBase | getOrCreate (ResourceId id) |
ResourceHandleBase | getByName (std::string_view name) const |
ResourceHandleBase | getBySource (std::string_view source) const |
ResourceHandleBase | createResource () |
virtual ResourceBase * | createInternal ()=0 |
virtual void | destroyInternal (ResourceBase *resource)=0 |
virtual void | destroyInternalLocked (ResourceBase *resource)=0 |
ResourceHandleBase | generateHandle (ResourceBase *resource) |
void | clearUpdated () |
virtual int | getUpdateQuota () const |
size_t | updatedResourceCount () |
size_t | orphanedResourceCount () |
size_t | allocatedResourceCount () |
virtual void | initialize ()=0 |
virtual void | clear ()=0 |
virtual void | clearSwapping ()=0 |
virtual void | processLoading ()=0 |
virtual void | processSwapping ()=0 |
virtual void | activateResources ()=0 |
virtual void | processDeletion ()=0 |
virtual void | resourceChanged (ResourceBase *resource)=0 |
virtual void | resourceDeleted (ResourceBase *resource)=0 |
virtual ResourceId | getNextResourceId ()=0 |
virtual void | setResourceId (ResourceBase *resource, ResourceId id)=0 |
Protected Member Functions | |
ResourceType * | createInternal () override |
void | destroyInternal (ResourceBase *resource) override |
void | handleLoadInternal (ResourceLoadInfoBase *loadInfo) override |
ActivationResult | handleActivationInternal (ResourceHandleBase r, ResourceBase *resource) override |
void | handleDeletionInternal (ResourceBase *resource) override |
void | handleFailedLoadInternal (ResourceLoadInfoBase *loadInfo) override |
IResourceLoader< ResourceType, LoadInfoType > * | findLoader (const LoadInfoType *loadInfo) |
Find a loader capable of loading the resource given by loadInfo. | |
![]() | |
ResourceHandleBase | getResourceHandle (ResourceId id) const |
Get existing resource handle. | |
bool | isValidResource (ResourceId id) const |
Check if resource handle points to. | |
virtual void | postProcessLoading () |
Hook for resource managers to run code at the tail of processLoading. | |
Protected Attributes | |
Mutex | swapMutex |
Collections::Pool< ResourceType > | resources |
std::vector< IResourceLoader< ResourceType, LoadInfoType > * > | loaders |
std::deque< SwapOperation > | swapQueue |
ResourceHandle | defaultResource |
![]() | |
std::unique_ptr< ResourceManagerBaseStorage > | storage |
Context * | context = nullptr |
ResourceTypes | resourceType = ResourceTypes::Unknown |
Private Member Functions | |
void | destroyInternalLocked (ResourceBase *resource) override |
Private Attributes | |
Mutex | loadInfoMutex |
Collections::Pool< LoadInfoType > | loadInfos |
The generic resource manager provides a base implementation for specialized resource managers to build on.
The resource manager takes care of all housekeeping tasks for resources, except the actual storage which is handled by ResourcePool instances.
Most processing of resources is asynchronous and/or delayed, and is queued up and handled during one of the resource processing phases;
The resource manager is designed to keep its public API thread-safe, but keep in mind that modifying shared resources is not necessarily covered by this guarantee. Editing of proxy resources is provided to facilitate updating shared resources from background threads.
ResourceType | Type of resource to be managed by this instance. |
LoadInfoType | Specialized load info structure to use when loading resources of ResourceType. |
Definition at line 75 of file ResourceManager.h.
using Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::ResourceHandle = ResourceHandle_t<ResourceType> |
Type of handle used to hold resources.
Definition at line 79 of file ResourceManager.h.
using Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::ResourceProxy = ResourceProxy<ResourceType, ResourceManager> |
Type of resource proxy objects, specialized on the type of resource.
Definition at line 82 of file ResourceManager.h.
|
inline |
Constructs a resource manager with the given context.
context | Pointer to a valid context. |
Definition at line 104 of file ResourceManager.h.
|
inlineoverride |
Destructs a resource manager, provided for destruction via base pointer.
Definition at line 113 of file ResourceManager.h.
|
inlineoverridevirtual |
Clear the resource manager, cleaning up resources held by member handles.
Implements Cogs::Core::IResourceManager.
Definition at line 126 of file ResourceManager.h.
Referenced by Cogs::Core::FontManager::clear(), Cogs::Core::MaterialManager::releaseAll(), and Cogs::Core::MaterialManager::~MaterialManager().
|
inlineoverridevirtual |
Clear the swap queue, ensuring indirect resource references are released.
Implements Cogs::Core::IResourceManager.
Definition at line 136 of file ResourceManager.h.
|
inline |
Create a new resource.
Creates a new resource of type ResourceType and returns a handle to the resource. The resource itself is allocated from a resource pool containing preallocated storage for resources of the current type. See ResourcePool for more details on the allocation and deallocation behavior for resources, and their performance.
The resource will have it's ownership managed by this resource manager instance, and reference count modified by the returned handle and/or copies of it.
If the resource count of the resource drops to zero the resource manager will be informed (See ResourceBase::decrement()). If no external resources are kept, the resource will be considered orphaned and will be deleted on the next call to processDeletion().
Definition at line 161 of file ResourceManager.h.
Referenced by Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::createLocked(), and Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::lock().
|
inlineoverrideprotectedvirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 183 of file ResourceManager.h.
|
inline |
Definition at line 409 of file ResourceManager.h.
|
inline |
Creates a new resource, but returns a ResourceProxy for thread safe editing instead of a handle to the actual resource.
Derived resource handlers can defer loading by calling queueResource() with the given load info, which will re-queue the resource for loading at a later stage. This can for example be useful when missing dependencies needed before loading.
Definition at line 235 of file ResourceManager.h.
References Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::create(), and Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::lock().
|
inlineoverrideprotectedvirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 188 of file ResourceManager.h.
|
inlineoverrideprivatevirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 194 of file ResourceManager.h.
|
inlineoverridevirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 402 of file ResourceManager.h.
|
inlineprotected |
Find a loader capable of loading the resource given by loadInfo.
loadInfo | A load info structure containing resource load data. |
Definition at line 425 of file ResourceManager.h.
|
inline |
Generate a handle to the given resource.
The returned handle will automatically modify the reference count of the held resource when moved, copied or destructed.
resourceBase | Pointer to a resource allocated from this resource managers pool. |
Definition at line 280 of file ResourceManager.h.
|
inline |
Resolve the given handle to a resource.
handle | Resource handle holding a resource. If the handle is not valid, the default resource will be used. |
Definition at line 342 of file ResourceManager.h.
References Cogs::Core::HandleIsValid().
|
inline |
Get a resource handle to the resource with the given id.
id | ResourceId for a previously loaded resource with the given id. |
Definition at line 382 of file ResourceManager.h.
References Cogs::Core::ResourceManagerBase::getResourceHandle().
Referenced by Cogs::Core::Text3DSystem::update().
|
inlinevirtual |
Handler for activation of resources.
Should be overridden by derived resource managers to handle resource activation, for example Textures needing to be handed over to the renderer when the resource has completed loading.
This handler is normally invoked for all resources that have successfully completed loading, during the activateResources() processing phase.
handle | Handle to the resource being activated. |
resource | Pointer to the resource being activated. |
Reimplemented in Cogs::Core::EffectManager, Cogs::Core::FontManager, Cogs::Core::MaterialManager, Cogs::Core::MaterialInstanceManager, and Cogs::Core::TextureManager.
Definition at line 317 of file ResourceManager.h.
References Cogs::Core::Success.
|
inlineoverrideprotectedvirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 205 of file ResourceManager.h.
|
inlinevirtual |
Handler for deletion of resources.
Should be overridden by derived resource managers to handle resource deletion, for example Textures needing to be removed from the renderer when the texture is no longer used by any materials or fonts.
This handler is invoked for all orphaned resources during the processDeletion() processing phase.
processDeletion() will invoke the destroy() function, which in turn invokes the deletion handler.
resource | Pointer to the resource being deleted. |
Reimplemented in Cogs::Core::EffectManager, Cogs::Core::FontManager, Cogs::Core::MaterialManager, Cogs::Core::MaterialInstanceManager, Cogs::Core::MeshManager, Cogs::Core::ModelManager, and Cogs::Core::TextureManager.
Definition at line 331 of file ResourceManager.h.
Referenced by Cogs::Core::ModelManager::handleDeletion().
|
inlineoverrideprotectedvirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 210 of file ResourceManager.h.
|
inlinevirtual |
Handler for failed resource loads using the given loadInfo.
Should be overridden by derived resource managers to handle custom failure logic.
loadInfo | The load info struct containing resource load data. |
Reimplemented in Cogs::Core::FontManager.
Definition at line 301 of file ResourceManager.h.
|
inlineoverrideprotectedvirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 215 of file ResourceManager.h.
|
inlinevirtual |
Handler for resource loading.
Should be overridden by derived resource managers to handle custom loading logic.
loadInfo | Load information struct containing resource load data. |
Reimplemented in Cogs::Core::AnimationManager, Cogs::Core::AssetManager, Cogs::Core::EffectManager, Cogs::Core::FontManager, Cogs::Core::GuiManager, Cogs::Core::MaterialManager, Cogs::Core::ModelManager, Cogs::Core::MeshManager, Cogs::Core::SoundManager, and Cogs::Core::TextureManager.
Definition at line 292 of file ResourceManager.h.
|
inlineoverrideprotectedvirtual |
Implements Cogs::Core::ResourceManagerBase.
Definition at line 200 of file ResourceManager.h.
|
inline |
Definition at line 177 of file ResourceManager.h.
|
inline |
Takes the handle given and returns a resource proxy to edit the contents of the held resource safely on other threads than the main thread.
handle | A handle to the resource being modified. |
Definition at line 252 of file ResourceManager.h.
References Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::create().
Referenced by Cogs::Core::ResourceManager< ResourceType, LoadInfoType >::createLocked().
|
inlineoverridevirtual |
Process queued swap operations, exchanging resource contents of the destination resources by those indicated in the operation data.
Implements Cogs::Core::IResourceManager.
Definition at line 355 of file ResourceManager.h.
References Cogs::Core::Context::engine.
Referenced by Cogs::Core::MeshManager::processSwapping().
|
inline |
Register a resource loader capable of handling resources of the template type.
The given loader will be queried for queued resources if it can load the resource contents from the information given in the load info structure.
The resource manager will take ownership of the loader and destroy it when the resource manager is destroyed.
loader | Pointer to a loader. |
Definition at line 397 of file ResourceManager.h.
|
inline |
Definition at line 172 of file ResourceManager.h.
|
inline |
Unlock the given resource proxy.
The handle given holds the resource being modified. The unmanaged (not allocated from resource pools) proxy resource is swapped with the old resource contents at an appropriate time.
Definition at line 265 of file ResourceManager.h.
|
protected |
Definition at line 444 of file ResourceManager.h.
|
protected |
Definition at line 440 of file ResourceManager.h.
|
private |
Definition at line 447 of file ResourceManager.h.
|
private |
Definition at line 448 of file ResourceManager.h.
|
protected |
Definition at line 438 of file ResourceManager.h.
|
protected |
Definition at line 436 of file ResourceManager.h.
|
protected |
Definition at line 442 of file ResourceManager.h.