Class resource_loader

Synopsis

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

template<typename Loader, typename Resource>
class resource_loader

Description

Base class for resource loaders.

Resource loaders must inherit from this class and stay true to the CRTP idiom. Moreover, a resource loader must expose a public, const member function named load that accepts a variable number of arguments and returns a shared pointer to the resource just created.
As an example:

struct my_resource {};

struct my_loader: entt::resource_loader<my_loader, my_resource> {
    std::shared_ptr<my_resource> load(int) const {
        // use the integer value somehow
        return std::make_shared<my_resource>();
    }
};

In general, resource loaders should not have a state or retain data of any type. They should let the cache manage their resources instead.

Note
Base class and CRTP idiom aren't strictly required with the current implementation. One could argue that a cache can easily work with loaders of any type. However, future changes won't be breaking ones by forcing the use of a base class today and that's why the model is already in its place.
Template Parameters

Loader - Type of the derived class.

Resource - Type of resource for which to use the loader.

Mentioned in

Source

Lines 44-59 in src/entt/resource/loader.hpp.

template<typename Loader, typename Resource>
class resource_loader {
    /*! @brief Resource loaders are friends of their caches. */
    friend struct resource_cache<Resource>;

    /**
     * @brief Loads the resource and returns it.
     * @tparam Args Types of arguments for the loader.
     * @param args Arguments for the loader.
     * @return The resource just loaded or an empty pointer in case of errors.
     */
    template<typename... Args>
    [[nodiscard]] std::shared_ptr<Resource> get(Args &&... args) const {
        return static_cast<const Loader *>(this)->load(std::forward<Args>(args)...);
    }
};





Add Discussion

Log in to comment