template<class T>
AbstractResourceLoader class
Base for resource loaders.
Contents
Provides (a)synchronous resource loading for ResourceManager.
Usage and subclassing
Usage is done by subclassing. Subclass instances can be added to ResourceManager using ResourceManager::
Subclassing is done by implementing at least doLoad() function. The loading can be done synchronously or asynchronously (i.e., in another thread). The base implementation provides interface to ResourceManager and manages loading progress (which is then available through functions requestedCount(), loadedCount() and notFoundCount()). You shouldn't access the ResourceManager directly when loading the data.
In your doLoad() implementation, after your resources are loaded, call set() to pass them to ResourceManager or call setNotFound() to indicate that the resource was not found.
You can also implement doName() to provide meaningful names for resource keys.
Example implementation for synchronous mesh loader:
class MeshResourceLoader: public AbstractResourceLoader<Mesh> { void doLoad(ResourceKey key) override { // Load the mesh... // Not found if(!found) { setNotFound(key); return; } // Found, pass it to resource manager set(key, mesh, state, policy); } };
You can then add it to resource manager instance like this. Note that the manager automatically deletes the all loaders on destruction before unloading all resources. It allows you to use resources in the loader itself without having to delete the loader explicitly to ensure proper resource unloading. In the following code, however, the loader destroys itself (and removes itself from the manager) before the manager is destroyed.
MyResourceManager manager; MeshResourceLoader loader; manager->setLoader(&loader); // This will now automatically request the mesh from loader by calling load() Resource<Mesh> myMesh = manager->get<Mesh>("my-mesh");
Public functions
-
auto requestedCount() const -> std::
size_t - Count of requested resources.
-
auto notFoundCount() const -> std::
size_t - Count of not found resources.
-
auto loadedCount() const -> std::
size_t - Count of loaded resources.
-
auto name(ResourceKey key) const -> std::
string - Resource name corresponding to given key.
- void load(ResourceKey key)
- Request resource to be loaded.
Protected functions
- void set(ResourceKey key, T* data, ResourceDataState state, ResourcePolicy policy)
- Set loaded resource to resource manager.
-
template<class U>void set(ResourceKey key, U&& data, ResourceDataState state, ResourcePolicy policy)
- void set(ResourceKey key, T* data)
- Set loaded resource to resource manager.
-
template<class U>void set(ResourceKey key, U&& data)
- void setNotFound(ResourceKey key)
- Mark resource as not found.
-
auto doName(ResourceKey key) const -> std::
string virtual - Implementation for name()
- void doLoad(ResourceKey key) pure virtual
- Implementation for load()
Function documentation
template<class T>
std:: size_t Magnum:: AbstractResourceLoader<T>:: requestedCount() const
Count of requested resources.
Count of resources requested by calling load().
template<class T>
std:: size_t Magnum:: AbstractResourceLoader<T>:: notFoundCount() const
Count of not found resources.
Count of resources requested by calling load(), but not found by the loader.
template<class T>
std:: size_t Magnum:: AbstractResourceLoader<T>:: loadedCount() const
Count of loaded resources.
Count of resources requested by calling load(), but not found by the loader.
template<class T>
std:: string Magnum:: AbstractResourceLoader<T>:: name(ResourceKey key) const
Resource name corresponding to given key.
If no such resource exists or the resource name is not available, returns empty string.
template<class T>
void Magnum:: AbstractResourceLoader<T>:: load(ResourceKey key)
Request resource to be loaded.
If the resource isn't yet loaded or loading, state of the resource is set to ResourceState::
template<class T>
void Magnum:: AbstractResourceLoader<T>:: set(ResourceKey key,
T* data,
ResourceDataState state,
ResourcePolicy policy) protected
Set loaded resource to resource manager.
Also increments count of loaded resources. Parameter state
must be either ResourceDataState::
template<class T>
template<class U>
void Magnum:: AbstractResourceLoader<T>:: set(ResourceKey key,
U&& data,
ResourceDataState state,
ResourcePolicy policy) protected
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
void Magnum:: AbstractResourceLoader<T>:: set(ResourceKey key,
T* data) protected
Set loaded resource to resource manager.
Same as above function with state set to ResourceDataState::
template<class T>
template<class U>
void Magnum:: AbstractResourceLoader<T>:: set(ResourceKey key,
U&& data) protected
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
void Magnum:: AbstractResourceLoader<T>:: setNotFound(ResourceKey key) protected
Mark resource as not found.
Also increments count of not found resources. See also ResourceManager::
template<class T>
std:: string Magnum:: AbstractResourceLoader<T>:: doName(ResourceKey key) const virtual protected
Implementation for name()
Default implementation returns empty string.
template<class T>
void Magnum:: AbstractResourceLoader<T>:: doLoad(ResourceKey key) pure virtual protected
Implementation for load()
See class documentation for reimplementation guide.