osgEarth 2.1.1
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

osgEarth::MemCache Class Reference

Inheritance diagram for osgEarth::MemCache:
Collaboration diagram for osgEarth::MemCache:

List of all members.

Classes

struct  CachedObject

Public Member Functions

 MemCache (int maxTilesInCache=16)
 MemCache (const MemCache &rhs, const osg::CopyOp &op=osg::CopyOp::DEEP_COPY_ALL)
 META_Object (osgEarth, MemCache)
unsigned int getMaxNumTilesInCache () const
void setMaxNumTilesInCache (unsigned int max)
virtual bool isCached (const TileKey &key, const CacheSpec &spec) const
virtual bool getImage (const TileKey &key, const CacheSpec &spec, osg::ref_ptr< const osg::Image > &out_image)
virtual void setImage (const TileKey &key, const CacheSpec &spec, const osg::Image *image)
virtual bool getHeightField (const TileKey &key, const CacheSpec &spec, osg::ref_ptr< const osg::HeightField > &out_hf)
virtual void setHeightField (const TileKey &key, const CacheSpec &spec, const osg::HeightField *hf)
virtual bool purge (const std::string &cacheId, int olderThan, bool async)

Protected Types

typedef std::list< CachedObjectObjectList
typedef std::map< std::string,
ObjectList::iterator > 
KeyToIteratorMap

Protected Member Functions

bool getObject (const TileKey &key, const CacheSpec &spec, osg::ref_ptr< const osg::Object > &out_result)
void setObject (const TileKey &key, const CacheSpec &spec, const osg::Object *image)

Protected Attributes

ObjectList _objects
KeyToIteratorMap _keyToIterMap
unsigned int _maxNumTilesInCache
OpenThreads::Mutex _mutex

Detailed Description

In-memory tile cache.

Definition at line 301 of file Caching.


Member Typedef Documentation

typedef std::map<std::string,ObjectList::iterator> osgEarth::MemCache::KeyToIteratorMap [protected]

Definition at line 366 of file Caching.

typedef std::list<CachedObject> osgEarth::MemCache::ObjectList [protected]

Definition at line 363 of file Caching.


Constructor & Destructor Documentation

MemCache::MemCache ( int  maxTilesInCache = 16)

Definition at line 339 of file Caching.cpp.

                               :
_maxNumTilesInCache( maxSize )
{
    setName( "mem" );
}
MemCache::MemCache ( const MemCache rhs,
const osg::CopyOp &  op = osg::CopyOp::DEEP_COPY_ALL 
)

Definition at line 345 of file Caching.cpp.


Member Function Documentation

bool MemCache::getHeightField ( const TileKey key,
const CacheSpec spec,
osg::ref_ptr< const osg::HeightField > &  out_hf 
) [virtual]

Gets the cached heightfield for the given TileKey

Reimplemented from osgEarth::Cache.

Definition at line 381 of file Caching.cpp.

{
    osg::ref_ptr<const osg::Object> result;
    if ( getObject(key, spec, result) )
    {
        out_hf = dynamic_cast<const osg::HeightField*>(result.get());
        return out_hf.valid();
    }
    else return false;
}

Here is the call graph for this function:

bool MemCache::getImage ( const TileKey key,
const CacheSpec spec,
osg::ref_ptr< const osg::Image > &  out_image 
) [virtual]

Gets the cached image for the given TileKey

Implements osgEarth::Cache.

Definition at line 363 of file Caching.cpp.

{
    osg::ref_ptr<const osg::Object> result;
    if ( getObject(key, spec, result) )
    {
        out_image = dynamic_cast<const osg::Image*>( result.get() );
        return out_image.valid();
    }
    else return false;
}

Here is the call graph for this function:

unsigned int MemCache::getMaxNumTilesInCache ( ) const

Gets the maximum number of tiles to keep in the cache

Definition at line 351 of file Caching.cpp.

{
        return _maxNumTilesInCache;
}
bool MemCache::getObject ( const TileKey key,
const CacheSpec spec,
osg::ref_ptr< const osg::Object > &  out_result 
) [protected]

Gets the cached object for the given TileKey

Definition at line 414 of file Caching.cpp.

{
  OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);

  std::string id = key.str() + spec.cacheId();
  KeyToIteratorMap::iterator itr = _keyToIterMap.find(id);
  if (itr != _keyToIterMap.end())
  {
    CachedObject entry = *itr->second;
    _objects.erase(itr->second);
    _objects.push_front(entry);
    itr->second = _objects.begin();
    output = itr->second->_object.get();
    return output.valid();
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool MemCache::isCached ( const TileKey key,
const CacheSpec spec 
) const [virtual]

Gets whether the given TileKey is cached or not

Reimplemented from osgEarth::Cache.

Definition at line 460 of file Caching.cpp.

{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock( const_cast<MemCache*>(this)->_mutex);
    std::string id = key.str() + spec.cacheId();
    return _keyToIterMap.find(id) != _keyToIterMap.end();
        //osg::ref_ptr<osg::Image> image = getImage(key,spec);
        //return image.valid();
}

Here is the call graph for this function:

osgEarth::MemCache::META_Object ( osgEarth  ,
MemCache   
)
bool MemCache::purge ( const std::string &  cacheId,
int  olderThan,
bool  async 
) [virtual]

Delete entries from the cache.

Reimplemented from osgEarth::Cache.

Definition at line 399 of file Caching.cpp.

{
    //TODO: can this be a ReadWriteLock instead?

    // MemCache does not support timestamps or async, so just clear it out altogether.
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);

    // MemCache does not support cacheId...
    _keyToIterMap.clear();
    _objects.clear();

    return true;
}
void MemCache::setHeightField ( const TileKey key,
const CacheSpec spec,
const osg::HeightField *  hf 
) [virtual]

Sets the cached heightfield for the given TileKey

Reimplemented from osgEarth::Cache.

Definition at line 393 of file Caching.cpp.

{
    setObject( key, spec, new osg::HeightField(*hf) );
}

Here is the call graph for this function:

void MemCache::setImage ( const TileKey key,
const CacheSpec spec,
const osg::Image *  image 
) [virtual]

Sets the cached image for the given TileKey

Implements osgEarth::Cache.

Definition at line 375 of file Caching.cpp.

{
    setObject( key, spec, ImageUtils::cloneImage(image) );
}

Here is the call graph for this function:

void MemCache::setMaxNumTilesInCache ( unsigned int  max)

Sets the maximum number of tiles to keep in the cache

Definition at line 357 of file Caching.cpp.

void MemCache::setObject ( const TileKey key,
const CacheSpec spec,
const osg::Object *  image 
) [protected]

Sets the cached object for the given TileKey

Definition at line 433 of file Caching.cpp.

{
  OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);

  std::string id = key.str() + spec.cacheId();

  _objects.push_front(CachedObject());
  CachedObject& entry = _objects.front();

  //CachedObject entry;
  entry._object = referenced;
  entry._key = id;
  //_objects.push_front(entry);

  _keyToIterMap[id] = _objects.begin();

  if (_objects.size() > _maxNumTilesInCache)
  {
      _keyToIterMap.erase( _objects.back()._key );
      _objects.pop_back();
    //CachedObject toRemove = _objects.back();
    //_objects.pop_back();
    //_keyToIterMap.erase(toRemove._key);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 367 of file Caching.

unsigned int osgEarth::MemCache::_maxNumTilesInCache [protected]

Definition at line 369 of file Caching.

OpenThreads::Mutex osgEarth::MemCache::_mutex [protected]

Definition at line 370 of file Caching.

Definition at line 364 of file Caching.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines