44#ifndef _INCLUDED_Field3D_FieldCache_H_
45#define _INCLUDED_Field3D_FieldCache_H_
49#include <boost/scoped_ptr.hpp>
50#include <boost/thread/mutex.hpp>
51#include <boost/foreach.hpp>
79template <
typename Data_T>
90 typedef std::map<std::string, CacheEntry>
Cache;
103 const std::string &layerPath);
106 const std::string &layerPath);
115 std::string
key(
const std::string &filename,
116 const std::string &layerPath);
134template <
typename Data_T>
137 boost::mutex::scoped_lock lock(ms_creationMutex);
138 if (ms_singleton.get() == NULL) {
141 return *ms_singleton;
146template <
typename Data_T>
149 const std::string &layerPath)
151 boost::mutex::scoped_lock lock(ms_accessMutex);
153 typename Cache::iterator i = m_cache.find(key(filename, layerPath));
154 if (i == m_cache.end()) {
160 if (weakPtr.expired()) {
168template <
typename Data_T>
170 const std::string &layerPath)
172 boost::mutex::scoped_lock lock(ms_accessMutex);
173 m_cache[key(filename, layerPath)] =
174 std::make_pair(field->weakPtr(), field.get());
179template <
typename Data_T>
182 boost::mutex::scoped_lock lock(ms_accessMutex);
184 long long int memSize = 0;
186 BOOST_FOREACH (
const typename Cache::value_type &i, m_cache) {
188 WeakPtr weakPtr = i.second.first;
189 if (weakPtr.expired()) {
193 memSize += i.second.second->memSize();
202template <
typename Data_T>
204 const std::string &layerPath)
206 return filename +
"/" + layerPath;
Contains Field, WritableField and ResizableField classes.
std::string key(const std::string &filename, const std::string &layerPath)
Constructs the cache key for a given file and layer path.
Cache m_cache
The cache itself. Maps a 'key' to a weak pointer and a raw pointer.
void cacheField(FieldPtr field, const std::string &filename, const std::string &layerPath)
Adds the given field to the cache.
long long int memSize() const
Returns the memory use of all currently loaded fields.
std::pair< WeakPtr, Field_T * > CacheEntry
static FieldCache & singleton()
Returns a reference to the FieldCache singleton.
static boost::mutex ms_accessMutex
Mutex to prevent reading from and writing to the cache concurrently.
FieldPtr getCachedField(const std::string &filename, const std::string &layerPath)
Checks the cache for a previously loaded field.
static boost::scoped_ptr< FieldCache > ms_singleton
The singleton instance.
std::map< std::string, CacheEntry > Cache
static boost::mutex ms_creationMutex
Mutex to prevent multiple allocaation of the singleton.
boost::intrusive_ptr< Field > Ptr
boost::weak_ptr< RefBase > WeakPtr
#define FIELD3D_NAMESPACE_HEADER_CLOSE