Clipper
clipper::ObjectCache< T > Class Template Reference

Object Cache manager. More...

#include <clipper_memory.h>

Classes

class  Reference
 ObjectCache reference class. More...
 

Public Types

enum  MODE { NORMAL , MINMEM , MAXMEM }
 

Public Member Functions

 ObjectCache ()
 constructor
 
 ~ObjectCache ()
 destructor, can message on contents
 
void set_mode (const MODE &mode)
 set garbage collection mode
 
void purge ()
 purge unreferenced objects from cache
 
void destroy ()
 VERY DANGEROUS, DO NOT USE.
 
void debug () const
 
Reference cache (const typename T::Key &key)
 cache or return data by key
 

Detailed Description

template<class T>
class clipper::ObjectCache< T >

Object Cache manager.

The object cache is a tool for storing information which may appear several times. Examples include tables of information for spacegroups or crystallographic maps. When a new object is created, a check is first done to see if such an object already exists in the cache, in which case that copy is used. Otherwise a new copy is added to the cache.

A cached object must implement:

  • a constructor from a type T
  • a method 'matches(T)' which tests if it constructed from that object
  • a 'format()' method, returning a string description of the contents The type T should be a compact unique description of the object.

Referring to the cache returns an ObjectCache<T>::Reference to a cache object. This object performs reference counting, which is used for garbage collection.

To retrieve the actual cached data, use the ObjectCache<T>::Reference::data() method. The data is held at a fixed memory location, therefore pointers to the data may be safely kept, as long as they are discarded as soon as the reference is discarded (at which point garbage collection may occur).

Ideally this would be a class with static members only, but some compilers have trouble with static members of template classes.

Garbage collection modes include:

  • ObjectCache<T>::NORMAL : Remove an old object only when a new object is required and an old object is no longer in use. (default)
  • ObjectCache<T>::MINMEM : Remove an old object as soon as it is no longer in use.
  • ObjectCache<T>::MAXMEM : Never remove old objects. The more memory hungry modes may improve performance for some problems where a new object may be created which was already used and destroyed before.

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