Go to the documentation of this file.
44 #ifndef _INCLUDED_Field3D_FieldCache_H_
45 #define _INCLUDED_Field3D_FieldCache_H_
49 #include <boost/thread/mutex.hpp>
50 #include <boost/foreach.hpp>
78 template <
typename Data_T>
89 typedef std::map<std::string, CacheEntry>
Cache;
102 const std::string &layerPath);
105 const std::string &layerPath);
114 std::string
key(
const std::string &filename,
115 const std::string &layerPath);
133 template <
typename Data_T>
136 boost::mutex::scoped_lock lock(ms_creationMutex);
140 return *ms_singleton;
145 template <
typename Data_T>
148 const std::string &layerPath)
150 boost::mutex::scoped_lock lock(ms_accessMutex);
152 typename Cache::iterator i = m_cache.find(key(filename, layerPath));
153 if (i == m_cache.end()) {
159 if (weakPtr.expired()) {
167 template <
typename Data_T>
169 const std::string &layerPath)
171 boost::mutex::scoped_lock lock(ms_accessMutex);
172 m_cache[key(filename, layerPath)] =
173 std::make_pair(field->weakPtr(), field.get());
178 template <
typename Data_T>
181 boost::mutex::scoped_lock lock(ms_accessMutex);
183 long long int memSize = 0;
185 BOOST_FOREACH (
const typename Cache::value_type &i, m_cache) {
187 WeakPtr weakPtr = i.second.first;
188 if (weakPtr.expired()) {
192 memSize += i.second.second->memSize();
201 template <
typename Data_T>
203 const std::string &layerPath)
205 return filename +
"/" + layerPath;
#define FIELD3D_NAMESPACE_HEADER_CLOSE
static boost::mutex ms_accessMutex
Mutex to prevent reading from and writing to the cache concurrently.
boost::intrusive_ptr< Field > Ptr
static FieldCache & singleton()
Returns a reference to the FieldCache singleton.
long long int memSize() const
Returns the memory use of all currently loaded fields.
std::string key(const std::string &filename, const std::string &layerPath)
Constructs the cache key for a given file and layer path.
std::pair< WeakPtr, Field_T * > CacheEntry
Cache m_cache
The cache itself. Maps a 'key' to a weak pointer and a raw pointer.
Contains Field, WritableField and ResizableField classes.
std::map< std::string, CacheEntry > Cache
FieldPtr getCachedField(const std::string &filename, const std::string &layerPath)
Checks the cache for a previously loaded field.
static boost::mutex ms_creationMutex
Mutex to prevent multiple allocaation of the singleton.
boost::weak_ptr< RefBase > WeakPtr
void cacheField(FieldPtr field, const std::string &filename, const std::string &layerPath)
Adds the given field to the cache.
static FieldCache * ms_singleton
The singleton instance.