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.
 
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 FieldCachesingleton ()
 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< FieldCachems_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.

136{
137 boost::mutex::scoped_lock lock(ms_creationMutex);
138 if (ms_singleton.get() == NULL) {
139 ms_singleton.reset(new FieldCache);
140 }
141 return *ms_singleton;
142}
#define FIELD3D_MTX_T
Definition StdMathLib.h:99
static boost::scoped_ptr< FieldCache > ms_singleton
The singleton instance.
Definition FieldCache.h:123
static boost::mutex ms_creationMutex
Mutex to prevent multiple allocaation of the singleton.
Definition FieldCache.h:125

References FIELD3D_MTX_T.

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.

150{
151 boost::mutex::scoped_lock lock(ms_accessMutex);
152 // First see if the request has ever been processed
153 typename Cache::iterator i = m_cache.find(key(filename, layerPath));
154 if (i == m_cache.end()) {
155 return FieldPtr();
156 }
157 // Next, check if all weak_ptrs are valid
158 CacheEntry &entry = i->second;
159 WeakPtr weakPtr = entry.first;
160 if (weakPtr.expired()) {
161 return FieldPtr();
162 }
163 return FieldPtr(entry.second);
164}
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
Cache m_cache
The cache itself. Maps a 'key' to a weak pointer and a raw pointer.
Definition FieldCache.h:121
std::pair< WeakPtr, Field_T * > CacheEntry
Definition FieldCache.h:89
static boost::mutex ms_accessMutex
Mutex to prevent reading from and writing to the cache concurrently.
Definition FieldCache.h:127
Field_T::WeakPtr WeakPtr
Definition FieldCache.h:88
Field_T::Ptr FieldPtr
Definition FieldCache.h:87

References FIELD3D_MTX_T.

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

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

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

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

References FIELD3D_MTX_T.

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

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

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.

◆ ms_singleton

template<typename Data_T >
boost::scoped_ptr< FieldCache< Data_T > > FieldCache< Data_T >::ms_singleton
staticprivate

The singleton instance.

Definition at line 123 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 125 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 127 of file FieldCache.h.


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