CopperSpice API  1.9.0
QCache< Key, T > Class Template Reference

The QCache class is used to store elements in a cache container. More...

Public Methods

 QCache (int maxCost=100)
 
 ~QCache ()
 
void clear ()
 
bool contains (const Key &key) const
 
int count () const
 
bool insert (const Key &key, T *object, int cost=1)
 
bool isEmpty () const
 
QList< Key > keys () const
 
int maxCost () const
 
T * object (const Key &key) const
 
T * operator[] (const Key &key) const
 
bool remove (const Key &key)
 
void setMaxCost (int cost)
 
int size () const
 
T * take (const Key &key)
 
int totalCost () const
 

Detailed Description

template<class Key, class T>
class QCache< Key, T >

The QCache class is used to store elements in a cache container. This is a templated class where Key and T are the template parameters. This class stores key / value pairs of type Key and a pointer to type T. When a new pair is added to the QCache the value must be passed as a pointer to an existing object of type T. The existing object must be allocated on the heap using operator new. QCache will take ownership of the value object and is responsible for deleting it.

When inserting an object into the cache you can specify a "cost" integer value. The value for the cost should bear some relationship to the amount of memory required to store the value object. The total cost can be retrieved by calling the method totalCost() and the cache limit can be found by calling maxCost().

When the total cost of all the value objects in the cache exceeds the cache limit, QCache will delete one or more key / value pairs to make room for new objects. Pairs which have not been accessed recently will be deleted first.

Example

The following is a declaration for a cache which stores objects of type Employee associated with an integer key. By default the maximum cost is 100. In this example it will be set to 5000.

This code shows how to insert an object in the cache.

Employee *employee = new Employee;
employee->setId(37);
employee->setName("Richard Smith");
cache.insert(employee->id(), employee);

Methods

To look up objects in the cache use the methods object() or operator[](). These methods look up an object by its key and return either a pointer to the cached object or a nullptr.

The method remove() is used to delete an key / value pair from the cache. Calling this method will also delete the value object. If you want to remove an key / value pair from the cache without the QCache deleting it, use the method take().

See also
QPixmapCache, QHash, QMap

Constructor & Destructor Documentation

template<class Key , class T >
QCache< Key, T >::QCache ( int  maxCost = 100)
inlineexplicit

Constructs a cache whose contents will never have a total cost greater than maxCost.

template<class Key , class T >
QCache< Key, T >::~QCache ( )
inline

Destroys the cache. Deletes all the objects in the cache.

Method Documentation

template<class Key , class T >
void QCache< Key, T >::clear ( )
inline

Deletes all the objects in the cache.

See also
remove(), take()
template<class Key , class T >
bool QCache< Key, T >::contains ( const Key &  key) const
inline

Returns true if the cache contains an object associated with key, otherwise returns false.

See also
take(), remove()
template<class Key , class T >
int QCache< Key, T >::count ( ) const
inline

Equivalent to calling size().

template<class Key , class T >
bool QCache< Key, T >::insert ( const Key &  key,
T *  object,
int  cost = 1 
)

Inserts object into the cache with key and the associated cost. Any object with the same key already in the cache will be removed. After this call, object is owned by the QCache and may be deleted at any time. If cost is greater than maxCost() the object will be deleted immediately.

The method returns true if the object was inserted into the cache, otherwise it returns false.

See also
take(), remove()
template<class Key , class T >
bool QCache< Key, T >::isEmpty ( ) const
inline

Returns true if the cache contains no objects, otherwise returns false.

See also
size()
template<class Key , class T >
QList< Key > QCache< Key, T >::keys ( ) const
inline

Returns a list of the keys in the cache.

template<class Key , class T >
int QCache< Key, T >::maxCost ( ) const
inline

Returns the maximum allowed total cost of the cache.

See also
setMaxCost(), totalCost()
template<class Key , class T >
T * QCache< Key, T >::object ( const Key &  key) const
inline

Returns the object associated with key or a nullptr if the key does not exist in the cache.

Warning
The returned object is owned by QCache and may be deleted at any time.
See also
take(), remove()
template<class Key , class T >
T * QCache< Key, T >::operator[] ( const Key &  key) const
inline

Returns the object associated with the given key or nullptr if the key does not exist in the cache. Equivalent to calling object().

Warning
The returned object is owned by QCache and may be deleted at any time.
template<class Key , class T >
bool QCache< Key, T >::remove ( const Key &  key)
inline

Deletes the object associated with the given key. Returns true if the object was found in the cache, otherwise returns false.

See also
take(), clear()
template<class Key , class T >
void QCache< Key, T >::setMaxCost ( int  cost)
inline

Sets the maximum allowed total cost of the cache to cost. If the current total cost is greater than cost, some objects are deleted immediately.

See also
maxCost(), totalCost()
template<class Key , class T >
int QCache< Key, T >::size ( ) const
inline

Returns the number of objects in the cache.

See also
isEmpty()
template<class Key , class T >
T * QCache< Key, T >::take ( const Key &  key)
inline

Takes the object associated with key out of the cache without deleting it. Returns a pointer to the removed object or a nullptr if the key does not exist in the cache. The ownership of the returned object is passed to the caller.

See also
remove()
template<class Key , class T >
int QCache< Key, T >::totalCost ( ) const
inline

Returns the total cost of the objects in the cache.

See also
setMaxCost()