osgEarth 2.1.1
Public Member Functions | Protected Types | Protected Attributes

osgEarth::Symbology::ResourceCache Class Reference

Inheritance diagram for osgEarth::Symbology::ResourceCache:
Collaboration diagram for osgEarth::Symbology::ResourceCache:

List of all members.

Public Member Functions

 ResourceCache (bool threadSafe=false)
osg::StateSet * getStateSet (SkinResource *skin)
const CacheStats getSkinStats () const
osg::Node * getMarkerNode (MarkerResource *marker)

Protected Types

typedef LRUCache< SkinResource
*, osg::ref_ptr< osg::StateSet > > 
SkinCache
typedef LRUCache
< MarkerResource
*, osg::ref_ptr< osg::Node > > 
MarkerCache

Protected Attributes

bool _threadSafe
Threading::ReadWriteMutex _mutex
SkinCache _skinCache
MarkerCache _markerCache

Detailed Description

Caches the runtime objects created by resources, so we can avoid creating them each time they are referenced.

This object is intended for use by a FilterContext, and therefore will only run in an isolated thread. No thread-safety is required in that scenario.

Definition at line 38 of file ResourceCache.


Member Typedef Documentation

typedef LRUCache<MarkerResource*, osg::ref_ptr<osg::Node> > osgEarth::Symbology::ResourceCache::MarkerCache [protected]

Definition at line 70 of file ResourceCache.

typedef LRUCache<SkinResource*, osg::ref_ptr<osg::StateSet> > osgEarth::Symbology::ResourceCache::SkinCache [protected]

Definition at line 67 of file ResourceCache.


Constructor & Destructor Documentation

osgEarth::Symbology::ResourceCache::ResourceCache ( bool  threadSafe = false) [inline]

Constructs a new resource cache.

Parameters:
threadSafeWhether to protect access to the cache so that you can use it from multiple threads (default = false)

Definition at line 46 of file ResourceCache.

: _threadSafe(threadSafe) { }

Member Function Documentation

osg::Node * ResourceCache::getMarkerNode ( MarkerResource marker)

Gets a node corresponding to a marker.

Definition at line 82 of file ResourceCache.cpp.

{
    osg::Node* result = 0L;

    if ( _threadSafe )
    {
        // first check if it exists
        {
            Threading::ScopedReadLock shared( _mutex );

            MarkerCache::Record rec = _markerCache.get( marker );
            if ( rec.valid() )
            {
                result = rec.value();
            }
        }

        // no? exclusive lock and create it.
        if ( !result )
        {
            Threading::ScopedWriteLock exclusive( _mutex );
            
            // double check to avoid race condition
            MarkerCache::Record rec = _markerCache.get( marker );
            if ( rec.valid() )
            {
                result = rec.value();
            }
            else
            {
                // still not there, make it.
                result = marker->createNode();
                if ( result )
                    _markerCache.insert( marker, result );
            }
        }
    }

    else
    {
        MarkerCache::Record rec = _markerCache.get( marker );
        if ( rec.valid() )
        {
            result = rec.value();
        }
        else
        {
            result = marker->createNode();
            if ( result )
                _markerCache.insert( marker, result );
        }
    }

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const CacheStats osgEarth::Symbology::ResourceCache::getSkinStats ( ) const [inline]

Get the statistics collected from the skin cache.

Definition at line 56 of file ResourceCache.

{ return _skinCache.getStats(); }
osg::StateSet * ResourceCache::getStateSet ( SkinResource skin)

Fetches the StateSet implementation corresponding to a Skin.

Definition at line 25 of file ResourceCache.cpp.

{
    osg::StateSet* result = 0L;

    if ( _threadSafe )
    {
        // first check if it exists
        {
            Threading::ScopedReadLock shared( _mutex );

            SkinCache::Record rec = _skinCache.get( skin );
            if ( rec.valid() )
            {
                result = rec.value();
            }
        }

        // no? exclusive lock and create it.
        if ( !result )
        {
            Threading::ScopedWriteLock exclusive( _mutex );
            
            // double check to avoid race condition
            SkinCache::Record rec = _skinCache.get( skin );
            if ( rec.valid() )
            {
                result = rec.value();
            }
            else
            {
                // still not there, make it.
                result = skin->createStateSet();
                if ( result )
                    _skinCache.insert( skin, result );
            }
        }
    }

    else
    {
        SkinCache::Record rec = _skinCache.get( skin );
        if ( rec.valid() )
        {
            result = rec.value();
        }
        else
        {
            result = skin->createStateSet();
            if ( result )
                _skinCache.insert( skin, result );
        }
    }

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 71 of file ResourceCache.

Definition at line 65 of file ResourceCache.

Definition at line 68 of file ResourceCache.

Definition at line 64 of file ResourceCache.


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