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

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

Member Typedef Documentation

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

Definition at line 84 of file FieldCache.h.

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

Definition at line 85 of file FieldCache.h.

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

Definition at line 86 of file FieldCache.h.

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

Definition at line 87 of file FieldCache.h.

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

Definition at line 88 of file FieldCache.h.

Member Function Documentation

template<typename Data_T >
FieldCache< Data_T > & FieldCache< Data_T >::singleton ( )
static

Returns a reference to the FieldCache singleton.

Definition at line 142 of file FieldCache.h.

References FieldCache< Data_T >::ms_creationMutex, and FieldCache< Data_T >::ms_singleton.

Referenced by Field3DInputFile::readLayer().

143 {
144  boost::mutex::scoped_lock lock(ms_creationMutex);
145  if (!ms_singleton) {
146  ms_singleton = new FieldCache;
147  }
148  return *ms_singleton;
149 }
static boost::mutex ms_creationMutex
Mutex to prevent multiple allocaation of the singleton.
Definition: FieldCache.h:121
static FieldCache * ms_singleton
The singleton instance.
Definition: FieldCache.h:119
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 155 of file FieldCache.h.

References FieldCache< Data_T >::key(), FieldCache< Data_T >::m_cache, and FieldCache< Data_T >::ms_accessMutex.

Referenced by Field3DInputFile::readLayer().

157 {
158  boost::mutex::scoped_lock lock(ms_accessMutex);
159  // First see if the request has ever been processed
160  typename Cache::iterator i = m_cache.find(key(filename, layerPath));
161  if (i == m_cache.end()) {
162  return FieldPtr();
163  }
164  // Next, check if all weak_ptrs are valid
165  CacheEntry &entry = i->second;
166  WeakPtr weakPtr = entry.first;
167  if (weakPtr.expired()) {
168  return FieldPtr();
169  }
170  return FieldPtr(entry.second);
171 }
Field_T::WeakPtr WeakPtr
Definition: FieldCache.h:86
Field_T::Ptr FieldPtr
Definition: FieldCache.h:85
static boost::mutex ms_accessMutex
Mutex to prevent reading from and writing to the cache concurrently.
Definition: FieldCache.h:123
std::pair< WeakPtr, Field_T * > CacheEntry
Definition: FieldCache.h:87
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:187
Cache m_cache
The cache itself. Maps a &#39;key&#39; to a weak pointer and a raw pointer.
Definition: FieldCache.h:117
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 176 of file FieldCache.h.

References FieldCache< Data_T >::key(), FieldCache< Data_T >::m_cache, and FieldCache< Data_T >::ms_accessMutex.

Referenced by Field3DInputFile::readLayer().

178 {
179  boost::mutex::scoped_lock lock(ms_accessMutex);
180  m_cache[key(filename, layerPath)] =
181  std::make_pair(field->weakPtr(), field.get());
182 }
static boost::mutex ms_accessMutex
Mutex to prevent reading from and writing to the cache concurrently.
Definition: FieldCache.h:123
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:187
Cache m_cache
The cache itself. Maps a &#39;key&#39; to a weak pointer and a raw pointer.
Definition: FieldCache.h:117
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 187 of file FieldCache.h.

References FIELD3D_NAMESPACE_HEADER_CLOSE.

Referenced by FieldCache< Data_T >::cacheField(), and FieldCache< Data_T >::getCachedField().

189 {
190  return filename + "/" + layerPath;
191 }

Member Data Documentation

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

Referenced by FieldCache< Data_T >::cacheField(), and FieldCache< Data_T >::getCachedField().

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

The singleton instance.

Definition at line 119 of file FieldCache.h.

Referenced by FieldCache< Data_T >::singleton().

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

Mutex to prevent multiple allocaation of the singleton.

Definition at line 121 of file FieldCache.h.

Referenced by FieldCache< Data_T >::singleton().

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

Referenced by FieldCache< Data_T >::cacheField(), and FieldCache< Data_T >::getCachedField().


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