Workspace 6.21.5
Public Types | Public Member Functions | List of all members
ExpiryCache< Key, T > Class Template Reference

Template class for a cache that removes items as soon as it exceeds maximum capacity. It uses shared pointers, so items will only be physically deleted once they go out of scope.

#include <Rendering/OpenGL/expirycache.h>

Inheritance diagram for ExpiryCache< Key, T >:
[legend]

Public Types

typedef T ItemType
 
using PtrType = QSharedPointer< T >
 
using WeakPtrType = QWeakPointer< T >
 

Public Member Functions

 ExpiryCache (int capacity=100)
 
virtual ~ExpiryCache ()
 
int capacity () const
 
void clear ()
 
bool contains (const Key &key) const
 
int count () const
 
bool insert (const Key &key, const PtrType &item, int cost=0)
 
bool isEmpty () const
 
PtrType object (const Key &key) const
 
PtrType operator[] (const Key &key) const
 
void setCapacity (int capacity)
 
int size () const
 
PtrType take (const Key &key)
 
int totalCost () const
 

Member Typedef Documentation

◆ ItemType

typedef T ItemType

◆ PtrType

using PtrType = QSharedPointer<T>

◆ WeakPtrType

using WeakPtrType = QWeakPointer<T>

Constructor & Destructor Documentation

◆ ExpiryCache()

ExpiryCache ( int  capacity = 100)

Create a cache with the specified size limit.

◆ ~ExpiryCache()

~ExpiryCache
virtual

We ensure the cache is empty, but we don't bother checking whether all of the items are deleted - they will clean themselves up eventually.

Member Function Documentation

◆ capacity()

int capacity

◆ clear()

void clear

◆ contains()

bool contains ( const Key &  key) const
Parameters
keyThe key used to find the object in the cache.
Returns
true if the cache contains the object, false otherwise.

◆ count()

int count

◆ insert()

bool insert ( const Key &  key,
const PtrType item,
int  cost = 0 
)
Parameters
keyThe key to associated with the object in the cache.
itemThe item to be stored in the cache.
costThe cost associated with the item in the cache.
Note
The cache does not allow duplicate keys. If an item is inserted with a key that is still in use (i.e. a valid pointer to its associated object still remains - in the cache or otherwise),
Returns
True if the item could be inserted, false otherwise.

◆ isEmpty()

bool isEmpty

◆ object()

ExpiryCache< Key, T >::PtrType object ( const Key &  key) const
Parameters
keyThe key used to find the object in the cache.
Returns
Attempts to retrieve an item from the cache. If the item doesn't exist in the cache, an invalid pointer is returned. Callers should always test for isNull() on the returned pointer.

◆ operator[]()

ExpiryCache< Key, T >::PtrType operator[] ( const Key &  key) const

◆ setCapacity()

void setCapacity ( int  capacity)
Parameters
capacityThe maximum size of the cache.

Sets the capacity of the cache; the size after which items will be deleted.

◆ size()

int size

◆ take()

ExpiryCache< Key, T >::PtrType take ( const Key &  key)
Parameters
keyThe key used to find the object in the cache.
Returns
Takes an item out of the cache (removes it), returning it to the user in a pointer. Once the last pointer goes out of scope, the item will be deleted, as it is no inter held by the cache.

◆ totalCost()

int totalCost