#include <tb_resource_cache.hpp>
Public Member Functions | |
HandleType | CreateResource (const ResourceType &resource, const tbCore::tbString &resourceName="") |
void | DestroyResource (const HandleType &handle) |
void | ClearCache (void) |
void | IncrementReferenceOf (const HandleType &handle) |
bool | DecrementReferenceOf (const HandleType &handle) |
ResourceType | GetResource (const HandleType &handle) const |
void | SetResource (const HandleType &handle, const ResourceType &resource) |
const ResourceType & | GetResourceReference (const HandleType &handle) const |
ResourceType & | GetResourceReference (const HandleType &handle) |
HandleType | GetResourceHandle (const tbCore::tbString &resourceName) const |
bool | IsValidHandle (const HandleType &handle) const |
size_t | ForEachValidResource (std::function< void(ResourceType &, HandleType)> callbackFunction) |
Static Public Member Functions | |
static HandleType | InvalidResource (void) |
Provides and interface for caching resources by name; sharing a single instance of a resource with multiple users/end-points. The ResourceCache does NOT manage the creation or destruction/cleanup of the resources that it contains. It simple provides tracking by name (often filepath) and reference counting.
void TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::ClearCache | ( | void | ) |
Nuclear option to remove everything from the cache immediately. This will invalidate ALL handles that belong to the cache even if the reference count is above 0.
HandleType TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::CreateResource | ( | const ResourceType & | resource, |
const tbCore::tbString & | resourceName = "" |
||
) |
Creates (or adds) a resource to the resource cache; optionally tracked by a name for ensuring only one copy of the resource will exist for that name. Once added/created the resource will have exactly 1 reference automatically.
resource | The resource data to add to the caching system, the resource should have been allocated and setup before calling this. |
resourceName | Optional name for the resource to ensure multiple copies of the same resource use a single instance. If the name is left empty it will be free to be copied, or one can use IncrementReferenceCount to manually share without name. |
bool TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::DecrementReferenceOf | ( | const HandleType & | handle | ) |
Decrements the reference count for a resource handle and returns true when the refence count for the handle reaches 0. When true is returned it is expected that the resource gets cleaned up and DestroyResource is then called for it to be removed.
handle | A handle to the resource that should remove a reference. |
void TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::DestroyResource | ( | const HandleType & | handle | ) |
Destroys (or removes) a resource from the resource cache. Note that the resource cache is NOT responsible for the destrucion/cleanup of the resource being removed, in fact it should probably happen immediately before this is called.
handle | A handle to the resource that should be destroyed/removed from the ResourceCache. If the handle is invalid or not contained in the cache an error condition will be triggered. |
ResourceType TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::GetResource | ( | const HandleType & | handle | ) | const |
Returns a copy of the resource for manipulation as needed.
handle | A handle to the resource that should be retreived. If the handle is invalid or not contained within the ResouceCache and error condition will be triggered. |
HandleType TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::GetResourceHandle | ( | const tbCore::tbString & | resourceName | ) | const |
Returns the resource handle associated with a resourceName. If the handle is invalid it means the ResourceCache does not have a resource with that name.
resourceName | The name of the resource to search for. |
const ResourceType & TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::GetResourceReference | ( | const HandleType & | handle | ) | const |
Returns the resource data by reference so it can be modified in place.
handle | A handle to the resource that should be retreived. If the handle is invalid or not contained within the ResouceCache and error condition will be triggered. |
ResourceType & TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::GetResourceReference | ( | const HandleType & | handle | ) |
Returns the resource data by reference so it can be modified in place.
handle | A handle to the resource that should be retreived. If the handle is invalid or not contained within the ResouceCache and error condition will be triggered. |
void TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::IncrementReferenceOf | ( | const HandleType & | handle | ) |
Increments the reference count for a valid resource handle so that a single resource instance can be shared with multiple parties.
handle | A handle to the resource that should have an additional reference added. |
|
static |
Returns a ResourceHandle that is marked invalid for comparing against.
bool TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::IsValidHandle | ( | const HandleType & | handle | ) | const |
Checks a handle for validity and containment within the ResourceCache and returns true if valid, or false if invalid or not contained.
handle | The handle to check if it is a valid handle with the ResouceCache. |
void TurtleBrains::Core::ResourceCache< ResourceType, HandleType >::SetResource | ( | const HandleType & | handle, |
const ResourceType & | resource | ||
) |
Changes the data of a resource for the given handle which is expected to be valid.
handle | A handle to the resource that should be retreived. If the handle is invalid or not contained within the ResouceCache and error condition will be triggered. |
resource | The resource data to set to the caching system, replacing the old data for this handle. |