libUTL++
utl::ObjectCache Class Reference

LRU object cache. More...

#include <ObjectCache.h>

Inheritance diagram for utl::ObjectCache:

Public Member Functions

 ObjectCache (size_t size)
 Constructor. More...
 
void access (const Object *object)
 Access the given object, so that it becomes the MRU object. More...
 
virtual bool add (const Object *object)
 Add the given object to the cache and make it the MRU object. More...
 
virtual BidItbeginNew () const
 Return a const iterator pointing to the beginning of the collection. More...
 
virtual BidItbeginNew ()
 Return an iterator pointing to the beginning of the collection. More...
 
virtual void clear ()
 Clear the cache. More...
 
virtual BidItendNew () const
 Return a const iterator pointing to the end of the collection. More...
 
virtual BidItendNew ()
 Return an iterator pointing to the end of the collection. More...
 
virtual Objectfind (const Object &key) const
 Find an object matching a given key. More...
 
CachedObjectfind (const Object &key, bool frozen) const
 Find the object matching the given key. More...
 
ListIt findIt (const Object &key) const
 Find the object matching the given key. More...
 
virtual void findIt (const Object &key, BidIt &it)
 Find an object matching a given key. More...
 
ObjectgetLRU () const
 Get the LRU object. More...
 
ObjectgetMRU () const
 Get the MRU object. More...
 
size_t getSize () const
 Get the cache size. More...
 
bool isFrozen () const
 Get the frozen flag. More...
 
bool isFull () const
 Determine whether the cache is full. More...
 
virtual bool remove (const Object &key)
 Remove the object matching the given key. More...
 
virtual void removeIt (BidIt &it)
 Remove the object the given iterator points to. More...
 
void set (size_t size)
 Clear the cache and set the cache size. More...
 
void setFrozen (bool frozen)
 Set the frozen flag. More...
 
- Public Member Functions inherited from utl::Collection
void assertOwner ()
 Assert ownership of contained objects. More...
 
virtual void copy (const Object &rhs)
 Copy another instance. More...
 
virtual void steal (Object &rhs)
 "Steal" the internal representation from another instance. More...
 
virtual void vclone (const Object &rhs)
 Make an exact copy of another instance. More...
 
virtual void dump (Stream &os, uint_t level=uint_t_max) const
 Dump a human-readable representation of self to the given output stream. More...
 
virtual void serialize (Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize to or from a stream. More...
 
void serialize (const RunTimeClass *rtc, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize to/from the given stream. More...
 
virtual size_t innerAllocatedSize () const
 Get the "inner" allocated size. More...
 
virtual bool update (const Object *object)
 Update the given object. More...
 
bool isOwner () const
 Get the owner flag. More...
 
void setOwner (bool owner)
 Set the owner flag. More...
 
const Orderingordering () const
 Get the ordering. More...
 
Orderingordering ()
 Get the ordering. More...
 
void setOrdering (const Ordering &ordering, uint_t algorithm=sort_quickSort)
 Set the ordering, and optionally sort the collection to reflect the new ordering. More...
 
virtual void setOrdering (Ordering *ordering, uint_t algorithm=sort_quickSort)
 Set the ordering, and optionally sort the collection to reflect the new ordering. More...
 
bool empty () const
 Determine whether the collection is empty. More...
 
bool isMultiSet () const
 Get the multiSet flag. More...
 
void setMultiSet (bool multiSet)
 Set the multiSet flag. More...
 
bool isMarked () const
 Get marked flag. More...
 
void setMarked (bool marked=true)
 Set marked flag. More...
 
size_t items () const
 Get the number of contained objects. More...
 
size_t size () const
 Get the number of contained objects. More...
 
bool add (const Object &object)
 Add an object to the collection. More...
 
void add (const Collection &collection)
 Add another collection's objects. More...
 
virtual BidItaddIt (const Object *object)
 Add an object to the collection. More...
 
ObjectaddOrFind (const Object &object)
 Add the given object, or find a matching object already contained. More...
 
virtual ObjectaddOrFind (const Object *object)
 Add the given object, or find a matching object already contained. More...
 
bool addOrUpdate (const Object &object)
 Add or update the given object. More...
 
virtual bool addOrUpdate (const Object *object)
 Add or update the given object. More...
 
CollectioncopyItems (const Collection *src, const Predicate *pred=nullptr, bool predVal=true)
 Copy objects from another collection. More...
 
CollectioncopyItems (const ListNode *src, const Predicate *pred=nullptr, bool predVal=true)
 Copy objects from a double-linked list. More...
 
CollectioncopyItems (const SlistNode *src, const Predicate *pred=nullptr, bool predVal=true)
 Copy objects from a single-linked list. More...
 
CollectionstealItems (Collection *src)
 "Steal" items from another collection. More...
 
Collectionoperator+= (const Object &rhs)
 Add an object to the collection. More...
 
Collectionoperator+= (const Object *rhs)
 Add an object to the collection. More...
 
Collectionoperator+= (const Collection &rhs)
 Add another collection's objects. More...
 
Collectionoperator-= (const Object &rhs)
 Remove an object from the collection. More...
 
void dump (Stream &os, uint_t level, bool key, bool printClassName, uint_t indent, const char *separator) const
 Dump contained objects to a stream. More...
 
virtual String toString () const
 Obtain a string representation by invoking Object::toString() on all contained objects.
 
String toString (bool key) const
 Obtain a string representation by invoking Object::toString() on all contained objects (or their keys). More...
 
String toString (const char *sep, bool key=false) const
 Obtain a string represenation by invoking Object::toString() on all contained objects (or their keys). More...
 
String toString (const String &sep, bool key=false) const
 Obtain a string represenation of the collection by invoking Object::toString() on all contained objects (or their keys). More...
 
iterator begin () const
 Return a const iterator pointing to the first object in the collection. More...
 
iterator begin ()
 Return an iterator pointing to the first object in the collection. More...
 
virtual BidItcreateIt () const
 Create a const iterator. More...
 
virtual BidItcreateIt ()
 Create an iterator. More...
 
iterator end () const
 Return a const iterator pointing to the end of the collection. More...
 
iterator end ()
 Return an iterator pointing to the end of the collection. More...
 
bool contains (const Object *key) const
 Determine whether the collection contains an object matching the given key. More...
 
bool contains (const Object &key) const
 Determine whether the collection contains an object matching the given key. More...
 
size_t count (const Predicate *pred=nullptr, bool predVal=true) const
 Count contained objects. More...
 
Objectfind (const Object *key) const
 Find an object matching a given key. More...
 
void findIt (const Object &key, BidIt &it) const
 Find an object matching a given key. More...
 
Objectfirst () const
 Return the first object (nullptr if empty). More...
 
bool has (const Object *key) const
 See contains(). More...
 
bool has (const Object &key) const
 See contains(). More...
 
Objectlast () const
 Get the last object (nullptr if empty). More...
 
bool remove (const Object *key)
 Remove the object matching the given key. More...
 
Objecttake (BidIt &it)
 Remove the object the given iterator points to, but do not delete the object even if the collection owns its objects. More...
 
Objectoperator[] (const Object *object)
 add-or-find access operator (returns Object*). More...
 
Objectoperator() (const Object *object)
 add-or-find access operator (returns Object&). More...
 
- Public Member Functions inherited from utl::Object
void clear ()
 Revert to initial state. More...
 
virtual int compare (const Object &rhs) const
 Compare with another object. More...
 
void dumpWithClassName (Stream &os, uint_t indent=4, uint_t level=uint_t_max) const
 Front-end for dump() that prints the object's class name. More...
 
virtual const ObjectgetKey () const
 Get the key for this object. More...
 
bool hasKey () const
 Determine whether or not the object has a key. More...
 
virtual const ObjectgetProxiedObject () const
 Get the proxied object (= self if none). More...
 
virtual ObjectgetProxiedObject ()
 Get the proxied object (= self if none). More...
 
virtual size_t hash (size_t size) const
 Get the hash code for the object. More...
 
bool _isA (const RunTimeClass *runTimeClass) const
 Determine whether self's class is a descendent of the given class. More...
 
 operator String () const
 Conversion to String. More...
 
size_t allocatedSize () const
 Get the total allocated size of this object. More...
 
virtual void addOwnedIt (const class FwdIt *it) const
 Notify self that it owns the given iterator. More...
 
virtual void removeOwnedIt (const class FwdIt *it) const
 Notify self that the given owned iterator has been destroyed. More...
 
bool operator< (const Object &rhs) const
 Less-than operator. More...
 
bool operator<= (const Object &rhs) const
 Less-than-or-equal-to operator. More...
 
bool operator> (const Object &rhs) const
 Greater-than operator. More...
 
bool operator>= (const Object &rhs) const
 Greater-than-or-equal-to operator. More...
 
bool operator== (const Object &rhs) const
 Equal-to operator. More...
 
bool operator!= (const Object &rhs) const
 Unequal-to operator. More...
 
void serializeIn (Stream &is, uint_t mode=ser_default)
 Serialize from an input stream. More...
 
void serializeOut (Stream &os, uint_t mode=ser_default) const
 Serialize to an output stream. More...
 
void serializeOutBoxed (Stream &os, uint_t mode=ser_default) const
 Serialize a boxed object to an output stream. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from utl::Object
static ObjectserializeInNullable (Stream &is, uint_t mode=ser_default)
 Serialize a nullptr-able object from an input stream. More...
 
static void serializeOutNullable (const Object *object, Stream &os, uint_t mode=ser_default)
 Serialize a nullptr-able object to an output stream. More...
 
static void serializeNullable (Object *&object, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a nullptr-able object to or from a stream. More...
 
static ObjectserializeInBoxed (Stream &is, uint_t mode=ser_default)
 Serialize a boxed object from an input stream. More...
 
static void serializeBoxed (Object *&object, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a boxed object to or from a stream. More...
 
- Protected Member Functions inherited from utl::FlagsMI
 FlagsMI ()
 Constructor. More...
 
virtual ~FlagsMI ()
 Destructor. More...
 
void copyFlags (const FlagsMI &rhs)
 Copy the given flags. More...
 
void copyFlags (const FlagsMI &rhs, uint_t lsb, uint_t msb)
 Copy (some of) the given flags. More...
 
void copyFlags (uint64_t flags, uint_t lsb, uint_t msb)
 Copy (some of) the given flags. More...
 
bool getFlag (uint_t flagNum) const
 Get a user-defined flag. More...
 
void setFlag (uint_t flagNum, bool val)
 Set a user-defined flag. More...
 
uint64_t getFlagsNumber (uint64_t mask, uint64_t shift=0)
 Get a multi-bit value in the flags data (which is stored as one 64-bit integer). More...
 
void setFlagsNumber (uint64_t mask, uint64_t shift, uint64_t num)
 Set a multi-bit value in the flags data (which is stored as one 64-bit integer). More...
 
uint64_t getFlags () const
 Get the flags. More...
 
void setFlags (uint64_t flags)
 Set the flags. More...
 

Detailed Description

LRU object cache.

ObjectCache implements an LRU object cache. ObjectCache is designed to assist you in implementing a cache for externally stored objects (such as ExtCache).

Concepts

  • LRU object : The least-recently-used object. When the cache is full, the LRU object may be removed to make room to add() a new object.
  • MRU object : The most-recently-used object. When an object in the cache is accessed (see access()), it becomes the MRU object.

Attributes

  • size : The number of objects that can be held in the cache. When the cache is full, adding a new object to the cache will cause the LRU object to be removed.
  • frozen flag : When frozen, the cache will be unaffected by accesses to cached objects. In particular, accessing an object will not make it the MRU object.
Author
Adam McKee

Definition at line 133 of file ObjectCache.h.

Constructor & Destructor Documentation

◆ ObjectCache()

utl::ObjectCache::ObjectCache ( size_t  size)
inline

Constructor.

Parameters
sizecache size

Definition at line 144 of file ObjectCache.h.

References utl::init().

Member Function Documentation

◆ access()

void utl::ObjectCache::access ( const Object object)

Access the given object, so that it becomes the MRU object.

◆ add()

virtual bool utl::ObjectCache::add ( const Object object)
inlinevirtual

Add the given object to the cache and make it the MRU object.

Returns
true if object successfully added, false otherwise
Parameters
objectobject to be added

Implements utl::Collection.

Definition at line 158 of file ObjectCache.h.

◆ beginNew() [1/2]

virtual BidIt* utl::ObjectCache::beginNew ( ) const
inlinevirtual

Return a const iterator pointing to the beginning of the collection.

Implements utl::Collection.

Definition at line 170 of file ObjectCache.h.

◆ beginNew() [2/2]

virtual BidIt* utl::ObjectCache::beginNew ( )
inlinevirtual

Return an iterator pointing to the beginning of the collection.

Implements utl::Collection.

Definition at line 176 of file ObjectCache.h.

◆ clear()

virtual void utl::ObjectCache::clear ( )
inlinevirtual

Clear the cache.

Implements utl::Collection.

Definition at line 183 of file ObjectCache.h.

◆ endNew() [1/2]

virtual BidIt* utl::ObjectCache::endNew ( ) const
inlinevirtual

Return a const iterator pointing to the end of the collection.

Implements utl::Collection.

Definition at line 197 of file ObjectCache.h.

◆ endNew() [2/2]

virtual BidIt* utl::ObjectCache::endNew ( )
inlinevirtual

Return an iterator pointing to the end of the collection.

Implements utl::Collection.

Definition at line 203 of file ObjectCache.h.

◆ find() [1/2]

virtual Object* utl::ObjectCache::find ( const Object key) const
inlinevirtual

Find an object matching a given key.

Returns
found object (nullptr if none)
Parameters
keysearch key

Reimplemented from utl::Collection.

Definition at line 209 of file ObjectCache.h.

References const_cast_this, IFDEBUG, and utl::FwdIt::setConst().

◆ find() [2/2]

CachedObject* utl::ObjectCache::find ( const Object key,
bool  frozen 
) const

Find the object matching the given key.

Returns
found object (nullptr if none)
Parameters
keysearch key
frozenfrozen flag

◆ findIt() [1/2]

ListIt utl::ObjectCache::findIt ( const Object key) const

Find the object matching the given key.

Returns
iterator of found object (end() if not found)
Parameters
keykey to search for

◆ findIt() [2/2]

virtual void utl::ObjectCache::findIt ( const Object key,
BidIt it 
)
virtual

Find an object matching a given key.

Parameters
keysearch key
itresult iterator (= end() if no object found)

Reimplemented from utl::Collection.

◆ getLRU()

Object* utl::ObjectCache::getLRU ( ) const
inline

Get the LRU object.

Definition at line 240 of file ObjectCache.h.

◆ getMRU()

Object* utl::ObjectCache::getMRU ( ) const
inline

Get the MRU object.

Definition at line 247 of file ObjectCache.h.

◆ getSize()

size_t utl::ObjectCache::getSize ( ) const
inline

Get the cache size.

Definition at line 254 of file ObjectCache.h.

◆ isFrozen()

bool utl::ObjectCache::isFrozen ( ) const
inline

Get the frozen flag.

Definition at line 261 of file ObjectCache.h.

◆ isFull()

bool utl::ObjectCache::isFull ( ) const
inline

Determine whether the cache is full.

Definition at line 268 of file ObjectCache.h.

References ASSERTD.

◆ remove()

virtual bool utl::ObjectCache::remove ( const Object key)
inlinevirtual

Remove the object matching the given key.

Returns
true if object found and removed, false otherwise
Parameters
keysearch key

Reimplemented from utl::Collection.

Definition at line 281 of file ObjectCache.h.

◆ removeIt()

virtual void utl::ObjectCache::removeIt ( BidIt it)
virtual

Remove the object the given iterator points to.

The iterator will be updated so that it points to the removed object's successor.

Parameters
ititerator specifying object to be removed

Reimplemented from utl::Collection.

◆ set()

void utl::ObjectCache::set ( size_t  size)

Clear the cache and set the cache size.

◆ setFrozen()

void utl::ObjectCache::setFrozen ( bool  frozen)
inline

Set the frozen flag.

Definition at line 293 of file ObjectCache.h.

References utl::deInit(), and utl::init().


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