MAGE  v0.171.0
Matthias Advanced Game Engine
mage::PersistentResourcePool< KeyT, ResourceT > Class Template Reference

#include <resource_pool.hpp>

Public Types

using key_type = KeyT
 
using value_type = ResourceT
 

Public Member Functions

 PersistentResourcePool ()=default
 
 PersistentResourcePool (const PersistentResourcePool &pool)=delete
 
 PersistentResourcePool (PersistentResourcePool &&pool) noexcept
 
 ~PersistentResourcePool () noexcept
 
PersistentResourcePooloperator= (const PersistentResourcePool &pool)=delete
 
PersistentResourcePooloperator= (PersistentResourcePool &&pool)=delete
 
bool empty () const noexcept
 
std::size_t size () const noexcept
 
bool Contains (const KeyT &key) noexcept
 
SharedPtr< ResourceT > Get (const KeyT &key) noexcept
 
template<typename... ConstructorArgsT>
SharedPtr< ResourceT > GetOrCreate (const KeyT &key, ConstructorArgsT &&... args)
 
template<typename DerivedResourceT , typename... ConstructorArgsT>
SharedPtr< ResourceT > GetOrCreateDerived (const KeyT &key, ConstructorArgsT &&... args)
 
void Remove (const KeyT &key)
 
void RemoveAll () noexcept
 

Private Types

using ResourceMap = std::map< KeyT, SharedPtr< ResourceT > >
 

Private Attributes

ResourceMap m_resource_map
 
std::mutex m_mutex
 

Detailed Description

template<typename KeyT, typename ResourceT>
class mage::PersistentResourcePool< KeyT, ResourceT >

A class of persistent resource pools.

Template Parameters
KeyTThe key type.
ResourceTThe resource type.

Member Typedef Documentation

◆ key_type

template<typename KeyT , typename ResourceT >
using mage::PersistentResourcePool< KeyT, ResourceT >::key_type = KeyT

The key type of persistent resource pools.

◆ ResourceMap

template<typename KeyT , typename ResourceT >
using mage::PersistentResourcePool< KeyT, ResourceT >::ResourceMap = std::map< KeyT, SharedPtr< ResourceT > >
private

A resource map used by a persistent resource pool.

◆ value_type

template<typename KeyT , typename ResourceT >
using mage::PersistentResourcePool< KeyT, ResourceT >::value_type = ResourceT

The value type of persistent resource pools.

Constructor & Destructor Documentation

◆ PersistentResourcePool() [1/3]

template<typename KeyT , typename ResourceT >
mage::PersistentResourcePool< KeyT, ResourceT >::PersistentResourcePool ( )
default

Constructs a persistent resource pool.

◆ PersistentResourcePool() [2/3]

template<typename KeyT , typename ResourceT >
mage::PersistentResourcePool< KeyT, ResourceT >::PersistentResourcePool ( const PersistentResourcePool< KeyT, ResourceT > &  pool)
delete

Constructs a persistent resource pool from the given persistent resource pool.

Parameters
[in]poolA reference to the persistent resource pool to copy.

◆ PersistentResourcePool() [3/3]

template<typename KeyT , typename ResourceT >
mage::PersistentResourcePool< KeyT, ResourceT >::PersistentResourcePool ( PersistentResourcePool< KeyT, ResourceT > &&  pool)
noexcept

Constructs a persistent resource pool by moving the given persistent resource pool.

Parameters
[in]poolA reference to the persistent resource pool to move.

◆ ~PersistentResourcePool()

template<typename KeyT , typename ResourceT >
mage::PersistentResourcePool< KeyT, ResourceT >::~PersistentResourcePool ( )
noexcept

Destructs this persistent resource pool.

Member Function Documentation

◆ Contains()

template<typename KeyT , typename ResourceT >
bool mage::PersistentResourcePool< KeyT, ResourceT >::Contains ( const KeyT &  key)
noexcept

Checks whether this persistent resource pool contains a resource corresponding to the given key from this persistent resource pool.

Parameters
[in]keyA reference to the key of the resource.
Returns
true, if a resource is contained in this persistent resource pool corresponding to the given key. false, otherwise.

◆ empty()

template<typename KeyT , typename ResourceT >
bool mage::PersistentResourcePool< KeyT, ResourceT >::empty ( ) const
noexcept

Checks whether this persistent resource pool is empty.

Returns
true if this persistent resource pool is empty. false otherwise.

◆ Get()

template<typename KeyT , typename ResourceT >
SharedPtr< ResourceT > mage::PersistentResourcePool< KeyT, ResourceT >::Get ( const KeyT &  key)
noexcept

Returns the resource corresponding to the given key from this persistent resource pool.

Parameters
[in]keyA reference to the key of the resource.
Returns
nullptr, if no resource is contained in this persistent resource pool corresponding to the given key.
A pointer to the resource corresponding to the given key from this persistent resource pool.

◆ GetOrCreate()

template<typename KeyT , typename ResourceT >
template<typename... ConstructorArgsT>
SharedPtr< ResourceT > mage::PersistentResourcePool< KeyT, ResourceT >::GetOrCreate ( const KeyT &  key,
ConstructorArgsT &&...  args 
)

Returns the resource corresponding to the given key from this persistent resource pool.

If no resource is contained in this persistent resource pool corresponding to the given key, a new resource is created from the given arguments, added to this persistent resource pool and returned.

Template Parameters
ConstructorArgsTThe argument types for creating a new resource of type ResourceT.
Parameters
[in]keyA reference to the key of the resource.
[in]argsThe arguments for creating a new resource of type ResourceT.
Returns
A pointer to the resource corresponding to the given key from this persistent resource pool.

◆ GetOrCreateDerived()

template<typename KeyT , typename ResourceT >
template<typename DerivedResourceT , typename... ConstructorArgsT>
SharedPtr< ResourceT > mage::PersistentResourcePool< KeyT, ResourceT >::GetOrCreateDerived ( const KeyT &  key,
ConstructorArgsT &&...  args 
)

Returns the resource corresponding to the given key from this persistent resource pool.

If no resource is contained in this persistent resource pool corresponding to the given key, a new resource is created from the given arguments, added to this persistent resource pool and returned.

Precondition
DerivedResourceT is a derived class of ResourceT.
Template Parameters
DerivedResourceTThe derived resource type.
ConstructorArgsTThe argument types for creating a new resource of type DerivedResourceT.
Parameters
[in]keyA reference to the key of the resource.
[in]argsThe arguments for creating a new resource of type DerivedResourceT.
Returns
A pointer to the resource corresponding to the given key from this persistent resource pool.

◆ operator=() [1/2]

template<typename KeyT , typename ResourceT >
PersistentResourcePool& mage::PersistentResourcePool< KeyT, ResourceT >::operator= ( const PersistentResourcePool< KeyT, ResourceT > &  pool)
delete

Copies the given persistent resource pool to this persistent resource pool.

Parameters
[in]poolA reference to the persistent resource pool to copy.
Returns
A reference to the copy of the given persistent resource pool (i.e. this persistent resource pool).

◆ operator=() [2/2]

template<typename KeyT , typename ResourceT >
PersistentResourcePool& mage::PersistentResourcePool< KeyT, ResourceT >::operator= ( PersistentResourcePool< KeyT, ResourceT > &&  pool)
delete

Moves the given persistent resource pool to this persistent resource pool.

Parameters
[in]poolA reference to the persistent resource pool to move.
Returns
A reference to the moved persistent resource pool (i.e. this persistent resource pool).

◆ Remove()

template<typename KeyT , typename ResourceT >
void mage::PersistentResourcePool< KeyT, ResourceT >::Remove ( const KeyT &  key)

Removes the resource corresponding to the given key from this persistent resource pool.

Parameters
[in]keyA reference to the key of the resource to remove.

◆ RemoveAll()

template<typename KeyT , typename ResourceT >
void mage::PersistentResourcePool< KeyT, ResourceT >::RemoveAll ( )
noexcept

Removes all resources from this persistent resource pool.

◆ size()

template<typename KeyT , typename ResourceT >
std::size_t mage::PersistentResourcePool< KeyT, ResourceT >::size ( ) const
noexcept

Returns the number of resources contained in this persistent resource pool.

Member Data Documentation

◆ m_mutex

template<typename KeyT , typename ResourceT >
std::mutex mage::PersistentResourcePool< KeyT, ResourceT >::m_mutex
mutableprivate

The mutex for accessing the resource map of this persistent resource pool.

◆ m_resource_map

template<typename KeyT , typename ResourceT >
ResourceMap mage::PersistentResourcePool< KeyT, ResourceT >::m_resource_map
private

The resource map of this persistent resource pool.