Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QCache Class

TheQCache class is a template class that provides a cache.More...

Header:#include <QCache>

Note: All functions in this class arereentrant.

Public Functions

QCache(int maxCost = 100)
~QCache()
voidclear()
boolcontains(const Key & key) const
intcount() const
boolinsert(const Key & key, T * object, int cost = 1)
boolisEmpty() const
QList<Key>keys() const
intmaxCost() const
T *object(const Key & key) const
boolremove(const Key & key)
voidsetMaxCost(int cost)
intsize() const
T *take(const Key & key)
inttotalCost() const
T *operator[](const Key & key) const

Detailed Description

TheQCache class is a template class that provides a cache.

QCache<Key, T> defines a cache that stores objects of type T associated with keys of type Key. For example, here's the definition of a cache that stores objects of type Employee associated with an integer key:

QCache<int, Employee> cache;

Here's how to insert an object in the cache:

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

The advantage of usingQCache over some other key-based data structure (such asQMap orQHash) is thatQCache automatically takes ownership of the objects that are inserted into the cache and deletes them to make room for new objects, if necessary. When inserting an object into the cache, you can specify acost, which should bear some approximate relationship to the amount of memory taken by the object. When the sum of all objects' costs (totalCost()) exceeds the cache's limit (maxCost()),QCache starts deleting objects in the cache to keep under the limit, starting with less recently accessed objects.

By default,QCache'smaxCost() is 100. You can specify a different value in theQCache constructor:

QCache<int, MyDataStructure> cache(5000);

Each time you callinsert(), you can specify a cost as third argument (after the key and a pointer to the object to insert). After the call, the inserted object is owned by theQCache, which may delete it at any time to make room for other objects.

To look up objects in the cache, useobject() or operator[](). This function looks up an object by its key, and returns either a pointer to the cached object (which is owned by the cache) or 0.

If you want to remove an object from the cache for a particular key, callremove(). This will also delete the object. If you want to remove an object from the cache without theQCache deleting it, usetake().

See alsoQPixmapCache,QHash, andQMap.

Member Function Documentation

QCache::QCache(int maxCost = 100)

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

QCache::~QCache()

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

void QCache::clear()

Deletes all the objects in the cache.

See alsoremove() andtake().

bool QCache::contains(constKey & key) const

Returns true if the cache contains an object associated with keykey; otherwise returns false.

See alsotake() andremove().

int QCache::count() const

Same assize().

bool QCache::insert(constKey & key,T * object,int cost = 1)

Insertsobject into the cache with keykey and associated costcost. Any object with the same key already in the cache will be removed.

After this call,object is owned by theQCache and may be deleted at any time. In particular, ifcost is greater thanmaxCost(), the object will be deleted immediately.

The function returns true if the object was inserted into the cache; otherwise it returns false.

See alsotake() andremove().

bool QCache::isEmpty() const

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

See alsosize().

QList<Key> QCache::keys() const

Returns a list of the keys in the cache.

int QCache::maxCost() const

Returns the maximum allowed total cost of the cache.

See alsosetMaxCost() andtotalCost().

T * QCache::object(constKey & key) const

Returns the object associated with keykey, or 0 if the key does not exist in the cache.

Warning: The returned object is owned byQCache and may be deleted at any time.

See alsotake() andremove().

bool QCache::remove(constKey & key)

Deletes the object associated with keykey. Returns true if the object was found in the cache; otherwise returns false.

See alsotake() andclear().

void QCache::setMaxCost(int cost)

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

See alsomaxCost() andtotalCost().

int QCache::size() const

Returns the number of objects in the cache.

See alsoisEmpty().

T * QCache::take(constKey & key)

Takes the object associated with keykey out of the cache without deleting it. Returns a pointer to the object taken out, or 0 if the key does not exist in the cache.

The ownership of the returned object is passed to the caller.

See alsoremove().

int QCache::totalCost() const

Returns the total cost of the objects in the cache.

This value is normally belowmaxCost(), butQCache makes an exception for Qt'simplicitly shared classes. If a cached object shares its internal data with another instance,QCache may keep the object lying around, possibly contributing to making totalCost() larger thanmaxCost().

See alsosetMaxCost().

T * QCache::operator[](constKey & key) const

Returns the object associated with keykey, or 0 if the key does not exist in the cache.

This is the same asobject().

Warning: The returned object is owned byQCache and may be deleted at any time.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp