FieldCache< Data_T > Class Template Reference

`#include <FieldCache.h>`

## Public Types

typedef std::map< std::string, CacheEntry > | Cache
typedef std::pair< WeakPtr, Field_T * > | CacheEntry
typedef Field< Data_T > | Field_T
typedef Field_T::Ptr | FieldPtr
typedef Field_T::WeakPtr | WeakPtr

## Public Member Functions

void | cacheField (FieldPtr field, const std::string &filename, const std::string &layerPath)
| Adds the given field to the cache.
FieldPtr | getCachedField (const std::string &filename, const std::string &layerPath)
| Checks the cache for a previously loaded field.
long long int | memSize () const
| Returns the memory use of all currently loaded fields.

## Static Public Member Functions

static FieldCache & | singleton ()
| Returns a reference to the FieldCache singleton.

## Private Member Functions

std::string | key (const std::string &filename, const std::string &layerPath)
| Constructs the cache key for a given file and layer path.

## Private Attributes

Cache | m_cache
| The cache itself. Maps a 'key' to a weak pointer and a raw pointer.

## Static Private Attributes

static boost::mutex | ms_accessMutex
| Mutex to prevent reading from and writing to the cache concurrently.
static boost::mutex | ms_creationMutex
| Mutex to prevent multiple allocaation of the singleton.
static boost::scoped_ptr< FieldCache > | ms_singleton
| The singleton instance.

## Detailed Description

template<typename Data_T>
class FieldCache< Data_T >

Definition at line 80 of file FieldCache.h.

## Member Typedef Documentation

## Field_T

template<typename Data_T>

typedef Field<Data_T> FieldCache< Data_T >::Field_T

Definition at line 86 of file FieldCache.h.

## FieldPtr

template<typename Data_T>

typedef Field_T::Ptr FieldCache< Data_T >::FieldPtr

Definition at line 87 of file FieldCache.h.

## WeakPtr

template<typename Data_T>

typedef Field_T::WeakPtr FieldCache< Data_T >::WeakPtr

Definition at line 88 of file FieldCache.h.

## CacheEntry

template<typename Data_T>

typedef std::pair<WeakPtr, Field_T*> FieldCache< Data_T >::CacheEntry

Definition at line 89 of file FieldCache.h.

## Cache

template<typename Data_T>

typedef std::map<std::string, CacheEntry> FieldCache< Data_T >::Cache

Definition at line 90 of file FieldCache.h.

## Member Function Documentation

## singleton()

template<typename Data_T>

FieldCache< Data_T > & FieldCache< Data_T >::singleton | ( |  | ) |

static

Returns a reference to the FieldCache singleton.

Definition at line 135 of file FieldCache.h.

{
boost::mutex::scoped_lock lock(ms_creationMutex);
if (ms_singleton.get() == NULL) {
ms_singleton.reset(new FieldCache);
}
return *ms_singleton;
}
FieldCache
Definition FieldCache.h:81
FieldCache::ms_singleton
static boost::scoped_ptr< FieldCache > ms_singleton
The singleton instance.
Definition FieldCache.h:123
FieldCache::ms_creationMutex
static boost::mutex ms_creationMutex
Mutex to prevent multiple allocaation of the singleton.
Definition FieldCache.h:125

References ms_creationMutex, and ms_singleton.

Referenced by Field3DInputFile::readLayer(), and Field3DInputFileHDF5::readLayer().

## getCachedField()

template<typename Data_T>

FieldCache< Data_T >::FieldPtr FieldCache< Data_T >::getCachedField | ( | const std::string & | filename,
|  | const std::string & | layerPath )

Checks the cache for a previously loaded field.

Returns
  A pointer to the cached field, if it is still in memory. Null otherwise.

Definition at line 148 of file FieldCache.h.

{
boost::mutex::scoped_lock lock(ms_accessMutex);
// First see if the request has ever been processed
typename Cache::iterator i = m_cache.find(key(filename, layerPath));
if (i == m_cache.end()) {
return FieldPtr();
}
// Next, check if all weak_ptrs are valid
CacheEntry &entry = i->second;
WeakPtr weakPtr = entry.first;
if (weakPtr.expired()) {
return FieldPtr();
}
return FieldPtr(entry.second);
}
FieldCache::key
std::string key(const std::string &filename, const std::string &layerPath)
Constructs the cache key for a given file and layer path.
Definition FieldCache.h:203
FieldCache::m_cache
Cache m_cache
The cache itself. Maps a 'key' to a weak pointer and a raw pointer.
Definition FieldCache.h:121
FieldCache::CacheEntry
std::pair< WeakPtr, Field_T * > CacheEntry
Definition FieldCache.h:89
FieldCache::ms_accessMutex
static boost::mutex ms_accessMutex
Mutex to prevent reading from and writing to the cache concurrently.
Definition FieldCache.h:127
FieldCache::WeakPtr
Field_T::WeakPtr WeakPtr
Definition FieldCache.h:88
FieldCache::FieldPtr
Field_T::Ptr FieldPtr
Definition FieldCache.h:87

References key(), m_cache, and ms_accessMutex.

Referenced by Field3DInputFile::readLayer(), and Field3DInputFileHDF5::readLayer().

## cacheField()

template<typename Data_T>

void FieldCache< Data_T >::cacheField | ( | FieldPtr | field,
|  | const std::string & | filename,
|  | const std::string & | layerPath )

Adds the given field to the cache.

Definition at line 169 of file FieldCache.h.

{
boost::mutex::scoped_lock lock(ms_accessMutex);
m_cache[key(filename, layerPath)] =
std::make_pair(field->weakPtr(), field.get());
}

References key(), m_cache, and ms_accessMutex.

Referenced by Field3DInputFile::readLayer(), and Field3DInputFileHDF5::readLayer().

## memSize()

template<typename Data_T>

long long int FieldCache< Data_T >::memSize | ( |  | ) | const

Returns the memory use of all currently loaded fields.

Definition at line 180 of file FieldCache.h.

{
boost::mutex::scoped_lock lock(ms_accessMutex);
long long int memSize = 0;
BOOST_FOREACH (const typename Cache::value_type &i, m_cache) {
// Check if pointer is valid
WeakPtr weakPtr = i.second.first;
if (weakPtr.expired()) {
continue;
} else {
// If valid, accumulate memory
memSize += i.second.second->memSize();
}
}
return memSize;
}
FieldCache::memSize
long long int memSize() const
Returns the memory use of all currently loaded fields.
Definition FieldCache.h:180

References m_cache, memSize(), and ms_accessMutex.

Referenced by memSize().

## key()

template<typename Data_T>

std::string FieldCache< Data_T >::key | ( | const std::string & | filename,
|  | const std::string & | layerPath )

private

Constructs the cache key for a given file and layer path.

Definition at line 203 of file FieldCache.h.

{
return filename + "/" + layerPath;
}

Referenced by cacheField(), and getCachedField().

## Member Data Documentation

## m_cache

template<typename Data_T>

Cache FieldCache< Data_T >::m_cache

private

The cache itself. Maps a 'key' to a weak pointer and a raw pointer.

Definition at line 121 of file FieldCache.h.

Referenced by cacheField(), getCachedField(), and memSize().

## ms_singleton

template<typename Data_T>

boost::scoped_ptr<FieldCache> FieldCache< Data_T >::ms_singleton

static private

The singleton instance.

Definition at line 123 of file FieldCache.h.

Referenced by singleton().

## ms_creationMutex

template<typename Data_T>

boost::mutex FieldCache< Data_T >::ms_creationMutex

static private

Mutex to prevent multiple allocaation of the singleton.

Definition at line 125 of file FieldCache.h.

Referenced by singleton().

## ms_accessMutex

template<typename Data_T>

boost::mutex FieldCache< Data_T >::ms_accessMutex

static private

Mutex to prevent reading from and writing to the cache concurrently.

Definition at line 127 of file FieldCache.h.

Referenced by cacheField(), getCachedField(), and memSize().

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

- export/FieldCache.h