3#include "IResourceLoader.h"
4#include "IResourceManager.h"
5#include "ResourceManagerBase.h"
6#include "ResourceProxy.h"
9#include "../EntityDefinition.h"
10#include "../MemoryContext.h"
12#include "Foundation/Collections/Pool.h"
13#include "Foundation/Platform/Threads.h"
23 constexpr ResourceTypes getResourceType(
const ResourceBase *) {
return ResourceTypes::Unknown; }
24 constexpr ResourceTypes getResourceType(
const Asset *) {
return ResourceTypes::Asset; }
25 constexpr ResourceTypes getResourceType(
const Model *) {
return ResourceTypes::Model; }
26 constexpr ResourceTypes getResourceType(
const Texture *) {
return ResourceTypes::Texture; }
27 constexpr ResourceTypes getResourceType(
const Effect *) {
return ResourceTypes::Effect; }
28 constexpr ResourceTypes getResourceType(
const Material *) {
return ResourceTypes::Material; }
29 constexpr ResourceTypes getResourceType(
const MaterialInstance *) {
return ResourceTypes::MaterialInstance; }
30 constexpr ResourceTypes getResourceType(
const BufferResource *) {
return ResourceTypes::Buffer; }
31 constexpr ResourceTypes getResourceType(
const Mesh *) {
return ResourceTypes::Mesh; }
32 constexpr ResourceTypes getResourceType(
const Font *) {
return ResourceTypes::Font; }
48 virtual void enable() = 0;
74 template<
typename ResourceType,
typename LoadInfoType>
106 resources(capacity, capacity, context->memory->resourceAllocator, MemBlockType::
ResourceManager),
107 loadInfos(128, 128, context->memory->resourceAllocator, MemBlockType::
ResourceManager)
109 resourceType = getResourceType(
static_cast<const ResourceType*
>(
nullptr));
115 for (
auto & loader : loaders) {
130 ResourceManagerBase::clear();
138 LockGuard swapLock(swapMutex);
183 ResourceType * createInternal()
override
185 return resources.
create();
188 void destroyInternal(ResourceBase * resource)
override
190 resources.
destroy(
static_cast<ResourceType *
>(resource));
194 void destroyInternalLocked(ResourceBase * resource)
override
196 resources.
destroy(
static_cast<ResourceType *
>(resource));
200 void handleLoadInternal(ResourceLoadInfoBase * loadInfo)
override
202 handleLoad(
static_cast<LoadInfoType *
>(loadInfo));
205 ActivationResult handleActivationInternal(ResourceHandleBase r, ResourceBase * resource)
override
210 void handleDeletionInternal(ResourceBase * resource)
override
215 void handleFailedLoadInternal(ResourceLoadInfoBase * loadInfo)
override
267 LockGuard swapLock(swapMutex);
269 swapQueue.emplace_back(handle, proxy);
345 return static_cast<ResourceType*
>(defaultResource.get());
348 return const_cast<ResourceType *
>(
static_cast<const ResourceType*
>(handle.get()));
357 LockGuard swapLock(swapMutex);
359 for (
auto & swapResource : swapQueue) {
360 auto source = swapResource.source.resolve();
361 auto destination = swapResource.destinationHandle.resolve();
363 assert(source &&
"Source resource for swap cannot be null.");
364 assert(destination &&
"Destination resource for swap cannot be null.");
366 *destination = std::move(*swapResource.source.resolve());
367 destination->setLoaded();
368 destination->setChanged();
369 context->
engine->setDirty();
399 loaders.push_back(loader);
404 LockGuard
lock(loadInfoMutex);
406 loadInfos.
destroy(
static_cast<LoadInfoType *
>(loadInfo));
409 LoadInfoType * createLoadInfo()
411 LockGuard
lock(loadInfoMutex);
413 return loadInfos.
create();
427 for (
auto loader : loaders) {
428 if (loader->canLoad(context, *loadInfo)) {
440 std::vector<IResourceLoader<ResourceType, LoadInfoType> *> loaders;
442 std::deque<SwapOperation> swapQueue;
A Context instance contains all the services, systems and runtime components needed to use Cogs.
std::unique_ptr< class Engine > engine
Engine instance.
ResourceHandleBase releaseInternal(ResourceId resourceId)
Releases a resourceId -> ResourceHandle mapping.
ResourceHandleBase loadResourceInternal(ResourceLoadInfoBase *loadInfo)
Load a resource using the given loadInfo.
ResourceHandleBase getResourceHandle(ResourceId id) const
Get existing resource handle.
The generic resource manager provides a base implementation for specialized resource managers to buil...
ResourceHandle getHandle(const ResourceId id) const
Get a resource handle to the resource with the given id.
virtual void handleFailedLoad(const LoadInfoType *)
Handler for failed resource loads using the given loadInfo.
~ResourceManager() override
Destructs a resource manager, provided for destruction via base pointer.
ResourceManager(Context *context, Collections::ElementOffset capacity=1024)
Constructs a resource manager with the given context.
void clear() override
Clear the resource manager, cleaning up resources held by member handles.
ResourceHandle_t< ResourceType > ResourceHandle
Type of handle used to hold resources.
ResourceHandle create()
Create a new resource.
ResourceProxy lock(const ResourceHandle &handle)
Takes the handle given and returns a resource proxy to edit the contents of the held resource safely ...
virtual ActivationResult handleActivation(ResourceHandle, ResourceType *)
Handler for activation of resources.
ResourceType * get(const ResourceHandleBase &handle)
Resolve the given handle to a resource.
virtual void handleLoad(LoadInfoType *)
Handler for resource loading.
void clearSwapping() override
Clear the swap queue, ensuring indirect resource references are released.
ResourceHandle generateHandle(ResourceBase *resource)
Generate a handle to the given resource.
virtual void handleDeletion(ResourceType *)
Handler for deletion of resources.
IResourceLoader< ResourceType, LoadInfoType > * findLoader(const LoadInfoType *loadInfo)
Find a loader capable of loading the resource given by loadInfo.
void unlockProxy(const ResourceHandle &handle, const ResourceHandle &proxy)
Unlock the given resource proxy.
void processSwapping() override
Process queued swap operations, exchanging resource contents of the destination resources by those in...
ResourceProxy createLocked()
Creates a new resource, but returns a ResourceProxy for thread safe editing instead of a handle to th...
void registerLoader(IResourceLoader< ResourceType, LoadInfoType > *loader)
Register a resource loader capable of handling resources of the template type.
ResourceProxy< ResourceType, ResourceManager > ResourceProxy
Type of resource proxy objects, specialized on the type of resource.
Field definition describing a single data member of a data structure.
uint16_t ElementOffset
Offset type used to index elements in resource pools.
ActivationResult
Defines results for resource activation.
@ Success
Resource activated successfully.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
ResourceTypes
Resource types.
DefaultValueType
Defines value types for default values.
Contains all Cogs related functionality.
Pool used to store elements of ElementType.
ElementType * create(ARGS &&... args)
Allocate and initialize a new element from the pool passing any arguments to the constructor of the n...
void destroy(ElementType *element)
Free and destroy the given element in the pool.
Defines a value to apply to a field.
Base class for engine resources.
Resource handle base class handling reference counting of resources derived from ResourceBase.
Defines a swap operation replacing the contents of a resource.
ResourceHandle destinationHandle
Handle to the destination resource to replace contents of.
ResourceHandle source
Source resource to move data from.