Bromeon
Public Member Functions
thor::ResourceManager< Resource, ResourceKey > Class Template Reference

Class that is responsible for the management of resources like images, fonts or music. More...

Inheritance diagram for thor::ResourceManager< Resource, ResourceKey >:
Inheritance graph

List of all members.

Public Member Functions

 ResourceManager ()
 Default constructor.
 ~ResourceManager ()
 Destructor.
ResourcePtr< Resource > Search (const ResourceKey &key)
 Searches for an occurrence of key and returns the mapped resource, if possible.
ResourcePtr< const Resource > Search (const ResourceKey &key) const
 Searches for an occurrence of key and returns the mapped resource, if possible (const overload).
ResourcePtr< Resource > Acquire (const ResourceKey &key)
 Returns the resource mapped to key, loading the resource if required.
bool Release (const ResourceKey &key)
 Releases the resource as soon as possible.
void SetLoadingFailureStrategy (Resources::LoadingFailureStrategy strategy)
 Determines how to react to resources that cannot be loaded.
void SetReleaseStrategy (Resources::ReleaseStrategy strategy)
 Determines whether resources are automatically released or not.

Detailed Description

template<class Resource, class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
class thor::ResourceManager< Resource, ResourceKey >

Class that is responsible for the management of resources like images, fonts or music.

You can acquire and release resources. Access is granted through the key (an ID class).

Template Parameters:
ResourceThe resource type to manage (for example sf::Image). Const-correctness is properly forwarded: If Resource is const-qualified, you cannot modify the resources after initialization (via ResourcePtr).
Resource isn't required to be default-constructible or copy-constructible.
ResourceKeyThe key class that is used to distinguish between resources (for example thor::Resources::ImageKey). By default, a library implementation supporting the SFML resource classes is used. If you want to specify your own key classes, they have to fulfill the following requirements:
  • Value semantics. Your key class must be copyable and assignable.
  • thor::MovedPtr<Resource> ResourceKey::Load() const loads the resource and returns a smart pointer to it (pointing to memory allocated with new). In case of loading failure, a null pointer shall be returned.
  • bool operator< (const ResourceKey&, const ResourceKey&) which supports strict weak ordering.

Constructor & Destructor Documentation

template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
thor::ResourceManager< Resource, ResourceKey >::ResourceManager ( )

Default constructor.

Initially, the object owns no allocated resources. By default, the loading failure strategy Resources::ThrowException and the release strategy Resources::ExplicitRelease are used.

template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
thor::ResourceManager< Resource, ResourceKey >::~ResourceManager ( )

Destructor.

Destroys all allocated resources. All ResourcePtr instances pointing to any of the resources stored inside this ResourceManager become NULL.


Member Function Documentation

template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
ResourcePtr<Resource> thor::ResourceManager< Resource, ResourceKey >::Acquire ( const ResourceKey &  key)

Returns the resource mapped to key, loading the resource if required.

If the key is already stored, the corresponding resource is returned. Otherwise, the resource is loaded. The release time of the resource is determined by the release strategy.

Parameters:
keyThe resource identifier to search for, which also contains loading information.
Returns:
ResourcePtr that allows access to the resource. If the resource allocation fails and the ReturnNullPointer strategy is active, a null pointer is returned.
Exceptions:
ResourceLoadingExceptionif the loading of the resource fails (and the ThrowException strategy is active).
See also:
SetLoadingFailureStrategy(), SetReleaseStrategy()
template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
bool thor::ResourceManager< Resource, ResourceKey >::Release ( const ResourceKey &  key)

Releases the resource as soon as possible.

If the resource is not in use (i.e. no ResourcePtr references it), it is immediately released. Otherwise, it is released as soon as the last ResourcePtr loses ownership. Calling this function has no effect when the AutoRelease strategy is active, since the resource is released ASAP anyway.

Precondition:
key must be a valid resource key (the corresponding resource must be stored inside this manager). To check for available resources, use Search().
Parameters:
keyThe resource identifier.
Returns:
true, if the resource was immediately released; false, if it is in use.
template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
ResourcePtr<Resource> thor::ResourceManager< Resource, ResourceKey >::Search ( const ResourceKey &  key)

Searches for an occurrence of key and returns the mapped resource, if possible.

This function does never load a new resource.

Parameters:
keyThe resource identifier to search for.
Returns:
The corresponding ResourcePtr, if found, or NULL otherwise.
template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
ResourcePtr<const Resource> thor::ResourceManager< Resource, ResourceKey >::Search ( const ResourceKey &  key) const

Searches for an occurrence of key and returns the mapped resource, if possible (const overload).

This function does never load a new resource.

Parameters:
keyThe resource identifier to search for.
Returns:
The corresponding read-only ResourcePtr, if found, or NULL otherwise.
template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
void thor::ResourceManager< Resource, ResourceKey >::SetLoadingFailureStrategy ( Resources::LoadingFailureStrategy  strategy)

Determines how to react to resources that cannot be loaded.

The strategy concerns all failing Acquire() calls.

template<class Resource , class ResourceKey = typename Resources::KeyTraits<Resource>::Type>
void thor::ResourceManager< Resource, ResourceKey >::SetReleaseStrategy ( Resources::ReleaseStrategy  strategy)

Determines whether resources are automatically released or not.

This function sets the strategy for each resource that will be acquired in the future. Resources that have already been allocated are not concerned! This allows you to set the release behaviour specificially for each resource before you call Acquire().


The documentation for this class was generated from the following file: