BARE2D
BARE2D::Cache< S, T > Class Template Reference

This is a skeleton cache class. This can only be used by the ResourceManager or other classes who actually maintain proper pointer ownership. More...

#include <Cache.hpp>

+ Inheritance diagram for BARE2D::Cache< S, T >:
+ Collaboration diagram for BARE2D::Cache< S, T >:

Public Member Functions

 Cache ()
 
T * findItem (S &key)
 Finds an item based on a key. More...
 
T * operator[] (S &key)
 An operator overload that returns the object stored at key. Analagous to findItem();. More...
 
bool addItem (S &key, T *item)
 Simply adds an item. More...
 
bool setItem (S &key, T *item)
 Adds or overwrites an item. More...
 
T * createItem (S &key, std::function< T *()> creationFunc=[]() -> T *{ return new T();})
 Creates an item with a given key and uses the constructor to fill out the rest. If it already exists, overwrite it and return that pointer. More...
 
void clear ()
 Clears the cache. Destroys all data, calling their respective destructors, then delete. More...
 
unsigned int getCount ()
 Returns the number of items in the cache. More...
 
std::unordered_map< S, T * >::iterator begin ()
 
std::unordered_map< S, T * >::iterator end ()
 
bool operator!= (Cache &other)
 

Private Attributes

std::unordered_map< S, T * > m_data
 

Detailed Description

template<typename S, typename T>
class BARE2D::Cache< S, T >

This is a skeleton cache class. This can only be used by the ResourceManager or other classes who actually maintain proper pointer ownership.

Definition at line 15 of file Cache.hpp.

Constructor & Destructor Documentation

◆ Cache()

template<typename S , typename T >
BARE2D::Cache< S, T >::Cache ( )
inline

Definition at line 18 of file Cache.hpp.

Member Function Documentation

◆ addItem()

template<typename S , typename T >
bool BARE2D::Cache< S, T >::addItem ( S &  key,
T *  item 
)

Simply adds an item.

Parameters
keyThe key for the item - how it is stored
itemThe item itself
Returns
True if successful, false otherwise.

Referenced by BARE2D::XMLDataManager::addData().

+ Here is the caller graph for this function:

◆ begin()

template<typename S , typename T >
std::unordered_map<S, T*>::iterator BARE2D::Cache< S, T >::begin ( )

◆ clear()

template<typename S , typename T >
void BARE2D::Cache< S, T >::clear ( )

Clears the cache. Destroys all data, calling their respective destructors, then delete.

◆ createItem()

template<typename S , typename T >
T* BARE2D::Cache< S, T >::createItem ( S &  key,
std::function< T *()>  creationFunc = []() -> T *{ return new T();} 
)

Creates an item with a given key and uses the constructor to fill out the rest. If it already exists, overwrite it and return that pointer.

Parameters
keyThe key to give the item.
creationFuncA function that allocates and returns a new object. Defaults, but it can slice data if you're using inheritance.
Returns
A pointer to that item.

◆ end()

template<typename S , typename T >
std::unordered_map<S, T*>::iterator BARE2D::Cache< S, T >::end ( )

◆ findItem()

template<typename S , typename T >
T* BARE2D::Cache< S, T >::findItem ( S &  key)

Finds an item based on a key.

Parameters
keyThe key to find the item by.
Returns
A pointer to the data, or nullptr if no such data exists

Referenced by BARE2D::XMLDataManager::addData().

+ Here is the caller graph for this function:

◆ getCount()

template<typename S , typename T >
unsigned int BARE2D::Cache< S, T >::getCount ( )

Returns the number of items in the cache.

◆ operator!=()

template<typename S , typename T >
bool BARE2D::Cache< S, T >::operator!= ( Cache< S, T > &  other)

◆ operator[]()

template<typename S , typename T >
T* BARE2D::Cache< S, T >::operator[] ( S &  key)

An operator overload that returns the object stored at key. Analagous to findItem();.

Parameters
key

◆ setItem()

template<typename S , typename T >
bool BARE2D::Cache< S, T >::setItem ( S &  key,
T *  item 
)

Adds or overwrites an item.

Parameters
keyThe key for the item - how it is stored
itemThe item itself
Returns
True if successful, false otherwise.

Referenced by BARE2D::XMLDataManager::setData().

+ Here is the caller graph for this function:

Field Documentation

◆ m_data

template<typename S , typename T >
std::unordered_map<S, T*> BARE2D::Cache< S, T >::m_data
private

Definition at line 78 of file Cache.hpp.


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