Cache< T > Class Template Reference

Class for loading, storing and finding cached objects. More...

#include <cache.h>

Inheritance diagram for Cache< T >:
CacheBase

List of all members.

Public Member Functions

 Cache (const std::string &cachedObjectsClassName, const std::type_info &assignedPortType)
 Ctor with parameters which are forwarded to CacheBase.
virtual ~Cache ()
 The dtor frees all objects in the local objectMap_ if they have been loaded by the cache.
T * find (Processor *const processor, Port *const port)
 For the given processor and the given port on it, this method returns the data from the cache which are assigned.
bool update (Processor *const processor)
 Updates the state of the cache and thereby of the cache index with the data from the given arguments by internally calling getPortData() and comparing the result with the return value of getInvalidValue().

Protected Types

typedef std::map< std::string,
CacheObject< T > * > 
ObjectMap
 The real cache: maps the key used by CacheIndexEntry to the object which are currently in memory.

Protected Member Functions

virtual T * getPortData (Port *const port) const
 Intended to return the data from the given port.
virtual T * loadObject (const std::string &filename) const =0
 This method has to be implemented to load the object of type T from the given file name.
virtual std::string saveObject (T *object, const std::string &directory, const std::string &filename)=0
 Method which has to save the given object to the file in the given file name in the directory, specified by directory.

Protected Attributes

ObjectMap objectMap_
CacheIndexcacheIndex_

Detailed Description

template<typename T>
class voreen::Cache< T >

Class for loading, storing and finding cached objects.

It basically defines three pure virtual methods which have to be defined by a derived class for caching concrete objects. All management for finding the entries for the cached objects, finding, loading and deleting files and updating them are done by this class. Authors of derived classes should have nothing do with it, once they have determined HOW the cached objects have to be stored and loaded from hard disk.

Author:
Dirk Feldmann, June/July 2009

Definition at line 194 of file cache.h.


Member Typedef Documentation

typedef std::map<std::string, CacheObject<T>* > ObjectMap [protected]

The real cache: maps the key used by CacheIndexEntry to the object which are currently in memory.

Objects which are loaded using loadObject() will be inserted into this map.

Definition at line 298 of file cache.h.


Constructor & Destructor Documentation

~Cache (  )  [inline, virtual]

The dtor frees all objects in the local objectMap_ if they have been loaded by the cache.

Objects which have not been loaded from files by the cache are freed elsewhere (usually by the concerned processor or in case of VolumeHandles by the VolumeSetContainer) and therefore MUST NOT be freed by this dtor.

Definition at line 327 of file cache.h.


Member Function Documentation

T * find ( Processor *const   processor,
Port *const   port 
) [inline]

For the given processor and the given port on it, this method returns the data from the cache which are assigned.

The method internally calls loadObject() which has to be defined in the concerete subclass. If not data have been found or the value returned from loadObject() is equal to the return value of getInvalidValue(), the return value of the latter will be returned, to indicate an error.

Parameters:
processor The processor to be handled.
outport The outport affected by the caching.
Returns:
The data which are cached for the given parameters. A concerete class has define interpret this value. The returned value can also be the result of getInvalidValue(), which might depend on the concrete subclass, to indicate that no object could be found.

Definition at line 337 of file cache.h.

virtual T* getPortData ( Port *const   port  )  const [inline, protected, virtual]

Intended to return the data from the given port.

Parameters:
port The port of which the data shall be returned.

Definition at line 251 of file cache.h.

virtual T* loadObject ( const std::string &  filename  )  const [protected, pure virtual]

This method has to be implemented to load the object of type T from the given file name.

Parameters:
filename Name of the file, including the absolute path, which shall be loaded.
Returns:
The object stored in that file.

Implemented in VolumeCache.

virtual std::string saveObject ( T *  object,
const std::string &  directory,
const std::string &  filename 
) [protected, pure virtual]

Method which has to save the given object to the file in the given file name in the directory, specified by directory.

If the file name is empty, a file name has to be generated and returned. If the name is not empty, the file shall assumed to be exisiting and shall be replaced. The method must return either the generated name, if filename was empty, filename itself, if it was not empty or an empty string, if an error has occured.

Parameters:
object The object to be saved.
directory The absolute path to the directory where the file will be stored.
filename The name of the file in which the object will be stored. This parameter may be an empty string (""), but then a name (preferably unique for that kind of objects) has to be generated and that name must be returned.
Returns:
If an error occurs an empty string must be returned. Otherwise, if filename was not empty, filename must be returned. If filename was empty, the generated string must be returned.

Implemented in VolumeCache.

bool update ( Processor *const   processor  )  [inline]

Updates the state of the cache and thereby of the cache index with the data from the given arguments by internally calling getPortData() and comparing the result with the return value of getInvalidValue().

As this facts may insert new data, and the limit for values to be cached might become exceeded, this method can cause cache entries or / and sub-entries to become displaced. Thererfore cleanup() is called to free the memory and delete files which will be no longer needed. If the insertion or replacement of the new data is successful, true will be returned, otherwise false.

Parameters:
processor The processor of which the data on ports affected by the concrete cache subclass shall be cached. All data on all ports of the type specified by the subclass'es ctor will be cached.
Returns:
True if the insertion / replacement of the data is successful, false otherwise.

Definition at line 379 of file cache.h.