template<class T>
Magnum::AbstractResourceLoader class

Base for resource loaders.

Provides (a)synchronous resource loading for ResourceManager.

Usage and subclassing

Usage is done by subclassing. Subclass instances can be added to ResourceManager using ResourceManager::setLoader(). After adding the loader, each call to ResourceManager::get() will call load() implementation unless the resource is already loaded (or loading is in progress). Note that resources requested before the loader was added are not affected by the loader.

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<GL::Mesh> {
    void doLoad(ResourceKey key) override {
        // Load the mesh...

        // Not found
        if(!found) {
            setNotFound(key);
            return;
        }

        // Found, pass it to resource manager
        set(key, std::move(mesh));
    }
};

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;
Containers::Pointer<MeshResourceLoader> loader;

manager.setLoader<GL::Mesh>(std::move(loader));

// This will now automatically request the mesh from loader by calling load()
Resource<GL::Mesh> myMesh = manager.get<GL::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.
void set(ResourceKey key, Containers::Pointer<T> data, ResourceDataState state, ResourcePolicy policy)
template<class U, class = typename std::enable_if<!std::is_same<typename std::decay<U>::type, std::nullptr_t>::value>::type>
void set(ResourceKey key, U&& data, ResourceDataState state, ResourcePolicy policy)
void set(ResourceKey key, T* data)
Set loaded resource to resource manager.
void set(ResourceKey key, Containers::Pointer<T> data)
template<class U, class = typename std::enable_if<!std::is_same<typename std::decay<U>::type, std::nullptr_t>::value>::type>
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::Loading and count of requested features is incremented. Depending on implementation the resource might be loaded synchronously or asynchronously.

template<class T>
void Magnum::AbstractResourceLoader<T>::set(ResourceKey key, T* data, ResourceDataState state, ResourcePolicy policy) protected

Set loaded resource to resource manager.

If data is nullptr and state is ResourceDataState::NotFound, increments count of not found resources. Otherwise, if data is not nullptr, increments count of loaded resources. See ResourceManager::set() for more information.

Note that resource's state is automatically set to ResourceDataState::Loading when it is requested from ResourceManager and it's not loaded yet, so it's not needed to call this function. For marking a resource as not found you can also use the convenience setNotFound() variant.

template<class T>
void Magnum::AbstractResourceLoader<T>::set(ResourceKey key, Containers::Pointer<T> 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> template<class U, class = typename std::enable_if<!std::is_same<typename std::decay<U>::type, std::nullptr_t>::value>::type>
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::Final and policy to ResourcePolicy::Resident.

template<class T>
void Magnum::AbstractResourceLoader<T>::set(ResourceKey key, Containers::Pointer<T> 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> template<class U, class = typename std::enable_if<!std::is_same<typename std::decay<U>::type, std::nullptr_t>::value>::type>
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.

A convenience function calling set() with nullptr and ResourceDataState::NotFound.

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.