Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QCache Class Reference
[QtCore module]

The QCache class is a template class that provides a cache. More...

#include <QCache>

Note: All the functions in this class are reentrant.

Public Functions


Detailed Description

The QCache 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 using QCache over some other key-based data structure (such as QMap or QHash) is that QCache 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 a cost, 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's maxCost() is 100. You can specify a different value in the QCache constructor:

    QCache<int, MyDataStructure> cache(5000);

Each time you call insert(), 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 the QCache, which may delete it at any time to make room for other objects.

To look up objects in the cache, use object() 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, call remove(). This will also delete the object. If you want to remove an object from the cache without the QCache deleting it, use take().

See also QPixmapCache, QHash, and QMap.


Member Function Documentation

QCache::QCache ( int maxCost = 100 )

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

QCache::~QCache ()

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

void QCache::clear ()

Deletes all the objects in the cache.

See also remove() and take().

bool QCache::contains ( const Key & key ) const

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

See also take() and remove().

int QCache::count () const

Same as size().

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

Inserts object into the cache with key key and associated cost 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. In particular, if cost is greater than maxCost(), the object will be deleted immediately.

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

See also take() and remove().

bool QCache::isEmpty () const

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

See also size().

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 also setMaxCost() and totalCost().

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

Returns the object associated with key key, or 0 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() and remove().

bool QCache::remove ( const Key & key )

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

See also take() and clear().

void QCache::setMaxCost ( int cost )

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() and totalCost().

int QCache::size () const

Returns the number of objects in the cache.

See also isEmpty().

T * QCache::take ( const Key & key )

Takes the object associated with key key 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 also remove().

int QCache::totalCost () const

Returns the total cost of the objects in the cache.

This value is normally below maxCost(), but QCache makes an exception for Qt's implicitly 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 than maxCost().

See also setMaxCost().

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

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

This is the same as object().

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

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 53
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Quarterly au hasard

Logo

XQuery et la météo

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.1
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web