OpenLexocad  27.1
entt::cache< Resource > Struct Template Reference

Simple cache for resources of a given type. More...

#include <entt.hpp>

Public Types

using size_type = std::size_t
 Unsigned integer type. More...
 
using resource_type = Resource
 Type of resources managed by a cache. More...
 
using id_type = ENTT_ID_TYPE
 Unique identifier type for resources. More...
 

Public Member Functions

 cache ()=default
 Default constructor. More...
 
 cache (cache &&)=default
 Default move constructor. More...
 
cacheoperator= (cache &&)=default
 Default move assignment operator. More...
 
size_type size () const ENTT_NOEXCEPT
 Number of resources managed by a cache. More...
 
bool empty () const ENTT_NOEXCEPT
 Returns true if a cache contains no resources, false otherwise. More...
 
void clear () ENTT_NOEXCEPT
 Clears a cache and discards all its resources. More...
 
template<typename Loader , typename... Args>
entt::handle< Resource > load (const id_type id, Args &&... args)
 Loads the resource that corresponds to a given identifier. More...
 
template<typename Loader , typename... Args>
entt::handle< Resource > reload (const id_type id, Args &&... args)
 Reloads a resource or loads it for the first time if not present. More...
 
template<typename Loader , typename... Args>
entt::handle< Resource > temp (Args &&... args) const
 Creates a temporary handle for a resource. More...
 
entt::handle< Resource > handle (const id_type id) const
 Creates a handle for a given resource identifier. More...
 
bool contains (const id_type id) const ENTT_NOEXCEPT
 Checks if a cache contains a given identifier. More...
 
void discard (const id_type id) ENTT_NOEXCEPT
 Discards the resource that corresponds to a given identifier. More...
 
template<typename Func >
void each (Func func) const
 Iterates all resources. More...
 

Detailed Description

template<typename Resource>
struct entt::cache< Resource >

Simple cache for resources of a given type.

Minimal implementation of a cache for resources of a given type. It doesn't offer much functionalities but it's suitable for small or medium sized applications and can be freely inherited to add targeted functionalities for large sized applications.

Template Parameters
ResourceType of resources managed by a cache.

Member Typedef Documentation

◆ id_type

template<typename Resource >
using entt::cache< Resource >::id_type = ENTT_ID_TYPE

Unique identifier type for resources.

◆ resource_type

template<typename Resource >
using entt::cache< Resource >::resource_type = Resource

Type of resources managed by a cache.

◆ size_type

template<typename Resource >
using entt::cache< Resource >::size_type = std::size_t

Unsigned integer type.

Constructor & Destructor Documentation

◆ cache() [1/2]

template<typename Resource >
entt::cache< Resource >::cache ( )
default

Default constructor.

◆ cache() [2/2]

template<typename Resource >
entt::cache< Resource >::cache ( cache< Resource > &&  )
default

Default move constructor.

Member Function Documentation

◆ clear()

template<typename Resource >
void entt::cache< Resource >::clear ( void  )
inline

Clears a cache and discards all its resources.

Handles are not invalidated and the memory used by a resource isn't freed as long as at least a handle keeps the resource itself alive.

◆ contains()

template<typename Resource >
bool entt::cache< Resource >::contains ( const id_type  id) const
inline

Checks if a cache contains a given identifier.

Parameters
idUnique resource identifier.
Returns
True if the cache contains the resource, false otherwise.

◆ discard()

template<typename Resource >
void entt::cache< Resource >::discard ( const id_type  id)
inline

Discards the resource that corresponds to a given identifier.

Handles are not invalidated and the memory used by the resource isn't freed as long as at least a handle keeps the resource itself alive.

Parameters
idUnique resource identifier.

◆ each()

template<typename Resource >
template<typename Func >
void entt::cache< Resource >::each ( Func  func) const
inline

Iterates all resources.

The function object is invoked for each element. It is provided with either the resource identifier, the resource handle or both of them.
The signature of the function must be equivalent to one of the following forms:

void(const id_type);
void(handle<Resource>);
void(const id_type, handle<Resource>);
Template Parameters
FuncType of the function object to invoke.
Parameters
funcA valid function object.

◆ empty()

template<typename Resource >
bool entt::cache< Resource >::empty ( ) const
inline

Returns true if a cache contains no resources, false otherwise.

Returns
True if the cache contains no resources, false otherwise.

◆ handle()

template<typename Resource >
entt::handle<Resource> entt::cache< Resource >::handle ( const id_type  id) const
inline

Creates a handle for a given resource identifier.

A resource handle can be in a either valid or invalid state. In other terms, a resource handle is properly initialized with a resource if the cache contains the resource itself. Otherwise the returned handle is uninitialized and accessing it results in undefined behavior.

See also
handle
Parameters
idUnique resource identifier.
Returns
A handle for the given resource.

◆ load()

template<typename Resource >
template<typename Loader , typename... Args>
entt::handle<Resource> entt::cache< Resource >::load ( const id_type  id,
Args &&...  args 
)
inline

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
LoaderType of loader to use to load the resource if required.
ArgsTypes of arguments to use to load the resource if required.
Parameters
idUnique resource identifier.
argsArguments to use to load the resource if required.
Returns
A handle for the given resource.

◆ operator=()

template<typename Resource >
cache& entt::cache< Resource >::operator= ( cache< Resource > &&  )
default

Default move assignment operator.

Returns
This cache.

◆ reload()

template<typename Resource >
template<typename Loader , typename... Args>
entt::handle<Resource> entt::cache< Resource >::reload ( const id_type  id,
Args &&...  args 
)
inline

Reloads a resource or loads it for the first time if not present.

Equivalent to the following snippet (pseudocode):

cache.discard(id);
cache.load(id, args...);

Arguments are forwarded directly to the loader in order to construct properly the requested resource.

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
LoaderType of loader to use to load the resource.
ArgsTypes of arguments to use to load the resource.
Parameters
idUnique resource identifier.
argsArguments to use to load the resource.
Returns
A handle for the given resource.

◆ size()

template<typename Resource >
size_type entt::cache< Resource >::size ( ) const
inline

Number of resources managed by a cache.

Returns
Number of resources currently stored.

◆ temp()

template<typename Resource >
template<typename Loader , typename... Args>
entt::handle<Resource> entt::cache< Resource >::temp ( Args &&...  args) const
inline

Creates a temporary handle for a resource.

Arguments are forwarded directly to the loader in order to construct properly the requested resource. The handle isn't stored aside and the cache isn't in charge of the lifetime of the resource itself.

Template Parameters
LoaderType of loader to use to load the resource.
ArgsTypes of arguments to use to load the resource.
Parameters
argsArguments to use to load the resource.
Returns
A handle for the given resource.

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