A resource manager, loads, unloads and updates a set of resources. More...
#include <ResourceManager.h>
Public Member Functions | |
ResourceManager (ptr< ResourceLoader > loader, unsigned int cacheSize=0) | |
Creates a new ResourceManager. | |
virtual | ~ResourceManager () |
Deletes this resource manager. | |
ptr< ResourceLoader > | getLoader () |
Returns the object used to load the ResourceDescriptor. | |
ptr< Object > | loadResource (const string &name) |
Loads the given resource. | |
ptr< Object > | loadResource (ptr< ResourceDescriptor > desc, const TiXmlElement *f) |
Loads the given resource. | |
bool | updateResources () |
Updates the already loaded resources if their descriptors have changed. | |
void | close () |
Closes this manager. | |
Protected Member Functions | |
void | releaseResource (Resource *resource) |
Releases an unused resource. | |
void | removeResource (Resource *resource) |
Removes a resource from this manager. |
A resource manager, loads, unloads and updates a set of resources.
A manager uses a ResourceLoader to load ResourceDescriptor, then uses a ResourceFactory to create actual Resource. A manager keeps track of the resources it has loaded: it can update (i.e. reload) them when their descriptors change, and it automatically deletes them when they are unused (i.e. unreferenced). Alternatively a manager can cache unused resources so that they can be loaded quickly if they are needed again.
ork::ResourceManager::ResourceManager | ( | ptr< ResourceLoader > | loader, | |
unsigned int | cacheSize = 0 | |||
) |
Creates a new ResourceManager.
loader | the object used to load the ResourceDescriptor. | |
cacheSize | the size of the cache of unused resources. |
virtual ork::ResourceManager::~ResourceManager | ( | ) | [virtual] |
Deletes this resource manager.
This deletes the cached unused resources, if any.
void ork::ResourceManager::close | ( | ) |
Closes this manager.
This method disables the cache of unused resources.
ptr<ResourceLoader> ork::ResourceManager::getLoader | ( | ) |
Returns the object used to load the ResourceDescriptor.
ptr<Object> ork::ResourceManager::loadResource | ( | ptr< ResourceDescriptor > | desc, | |
const TiXmlElement * | f | |||
) |
Loads the given resource.
This method first loads its descriptor with getLoader, then creates the resource with ResourceFactory, and finally returns the result. Unless the resource has already been loaded, in which case it is returned directly.
desc | descriptor of the resource to load. | |
f | the XML part of a ResourceDescriptor. |
Loads the given resource.
This method first loads its descriptor with getLoader, then creates the resource with ResourceFactory, and finally returns the result. Unless the resource has already been loaded, in which case it is returned directly.
name | the name of the resource to be loaded. |
void ork::ResourceManager::releaseResource | ( | Resource * | resource | ) | [protected] |
Releases an unused resource.
If there is a cache of unused resources then this resource is put in this cache (the oldest resource in the cache is evicted if the cache is full). Otherwise if there is no cache, the resource is deleted directly.
resource | an unused resource, i.e. an unreferenced resource. |
void ork::ResourceManager::removeResource | ( | Resource * | resource | ) | [protected] |
Removes a resource from this manager.
This method is called from the Resource destructor when a resource gets deleted (for example when a resource is deleted in the releaseResource method).
resource | a resource which is currently being deleted. |
bool ork::ResourceManager::updateResources | ( | ) |
Updates the already loaded resources if their descriptors have changed.
This update is atomic, i.e. either all resources are updated, or none are updated.