Field3D
FieldCache< Data_T > Class Template Reference

#include <FieldCache.h>

Public Types

typedef std::map< std::string, CacheEntryCache
 
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. More...
 
FieldPtr getCachedField (const std::string &filename, const std::string &layerPath)
 Checks the cache for a previously loaded field. More...
 
long long int memSize () const
 Returns the memory use of all currently loaded fields. More...
 

Static Public Member Functions

static FieldCachesingleton ()
 Returns a reference to the FieldCache singleton. More...
 

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. More...
 

Private Attributes

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

Static Private Attributes

static boost::mutex ms_accessMutex
 Mutex to prevent reading from and writing to the cache concurrently. More...
 
static boost::mutex ms_creationMutex
 Mutex to prevent multiple allocaation of the singleton. More...
 
static FieldCachems_singleton
 The singleton instance. More...
 

Detailed Description

template<typename Data_T>
class FieldCache< Data_T >

Definition at line 79 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 85 of file FieldCache.h.

◆ FieldPtr

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

Definition at line 86 of file FieldCache.h.

◆ WeakPtr

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

Definition at line 87 of file FieldCache.h.

◆ CacheEntry

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

Definition at line 88 of file FieldCache.h.

◆ Cache

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

Definition at line 89 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 134 of file FieldCache.h.

135 {
136  boost::mutex::scoped_lock lock(ms_creationMutex);
137  if (!ms_singleton) {
138  ms_singleton = new FieldCache;
139  }
140  return *ms_singleton;
141 }

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 147 of file FieldCache.h.

149 {
150  boost::mutex::scoped_lock lock(ms_accessMutex);
151  // First see if the request has ever been processed
152  typename Cache::iterator i = m_cache.find(key(filename, layerPath));
153  if (i == m_cache.end()) {
154  return FieldPtr();
155  }
156  // Next, check if all weak_ptrs are valid
157  CacheEntry &entry = i->second;
158  WeakPtr weakPtr = entry.first;
159  if (weakPtr.expired()) {
160  return FieldPtr();
161  }
162  return FieldPtr(entry.second);
163 }

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 168 of file FieldCache.h.

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

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 179 of file FieldCache.h.

180 {
181  boost::mutex::scoped_lock lock(ms_accessMutex);
182 
183  long long int memSize = 0;
184 
185  BOOST_FOREACH (const typename Cache::value_type &i, m_cache) {
186  // Check if pointer is valid
187  WeakPtr weakPtr = i.second.first;
188  if (weakPtr.expired()) {
189  continue;
190  } else {
191  // If valid, accumulate memory
192  memSize += i.second.second->memSize();
193  }
194  }
195 
196  return memSize;
197 }

◆ 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 202 of file FieldCache.h.

204 {
205  return filename + "/" + layerPath;
206 }

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 120 of file FieldCache.h.

◆ ms_singleton

template<typename Data_T>
FieldCache< Data_T > * FieldCache< Data_T >::ms_singleton
staticprivate

The singleton instance.

Definition at line 122 of file FieldCache.h.

◆ ms_creationMutex

template<typename Data_T>
FIELD3D_NAMESPACE_OPEN boost::mutex FieldCache< Data_T >::ms_creationMutex
staticprivate

Mutex to prevent multiple allocaation of the singleton.

Definition at line 124 of file FieldCache.h.

◆ ms_accessMutex

template<typename Data_T>
boost::mutex FieldCache< Data_T >::ms_accessMutex
staticprivate

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

Definition at line 126 of file FieldCache.h.


The documentation for this class was generated from the following files:
FieldCache::ms_accessMutex
static boost::mutex ms_accessMutex
Mutex to prevent reading from and writing to the cache concurrently.
Definition: FieldCache.h:126
FieldCache::memSize
long long int memSize() const
Returns the memory use of all currently loaded fields.
Definition: FieldCache.h:179
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:202
FieldCache::CacheEntry
std::pair< WeakPtr, Field_T * > CacheEntry
Definition: FieldCache.h:88
FieldCache::m_cache
Cache m_cache
The cache itself. Maps a 'key' to a weak pointer and a raw pointer.
Definition: FieldCache.h:120
FieldCache::WeakPtr
Field_T::WeakPtr WeakPtr
Definition: FieldCache.h:87
FieldCache::FieldPtr
Field_T::Ptr FieldPtr
Definition: FieldCache.h:86
FieldCache::ms_creationMutex
static boost::mutex ms_creationMutex
Mutex to prevent multiple allocaation of the singleton.
Definition: FieldCache.h:124
FieldCache::ms_singleton
static FieldCache * ms_singleton
The singleton instance.
Definition: FieldCache.h:122
FieldCache
Definition: FieldCache.h:79