Function load

Synopsis

#include <src/entt/resource/cache.hpp>

template <typename Loader, typename... Args>
resource_handle< Resource > load(const id_type id, Args &&... args)

Description

Loads the resource that corresponds to a given identifier.

In case an identifier isn't already present in the cache, it loads its resource and stores it aside for future uses. Arguments are forwarded directly to the loader in order to construct properly the requested resource.

Note
If the identifier is already present in the cache, this function does nothing and the arguments are simply discarded.
Warning
If the resource cannot be loaded correctly, the returned handle will be invalid and any use of it will result in undefined behavior.
Template Parameters

Loader - Type of loader to use to load the resource if required.

Args - Types of arguments to use to load the resource if required.

Parameters

id - Unique resource identifier.

args - Arguments to use to load the resource if required.

Return
A handle for the given resource.

Mentioned in

Source

Lines 93-108 in src/entt/resource/cache.hpp.

template<typename Loader, typename... Args>
resource_handle<Resource> load(const id_type id, Args &&... args) {
    static_assert(std::is_base_of_v<resource_loader<Loader, Resource>, Loader>, "Invalid loader type");
    resource_handle<Resource> resource{};
    if(auto it = resources.find(id); it == resources.cend()) {
        if(auto instance = Loader{}.get(std::forward<Args>(args)...); instance) {
            resources[id] = instance;
            resource = std::move(instance);
        }
    } else {
        resource = it->second;
    }
    return resource;
}





Add Discussion

Log in to comment