1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGDB_OBJECTCACHE
15#define OSGDB_OBJECTCACHE 1
19#include <osgDB/ReaderWriter>
20#include <osgDB/DatabaseRevisions>
26class OSGDB_EXPORT ObjectCache : public osg::Referenced
32 /** For each object in the cache which has an reference count greater than 1
33 * (and therefore referenced by elsewhere in the application) set the time stamp
34 * for that object in the cache to specified time.
35 * This would typically be called once per frame by applications which are doing database paging,
36 * and need to prune objects that are no longer required.
37 * The time used should be taken from the FrameStamp::getReferenceTime().*/
38 void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime);
40 /** Removed object in the cache which have a time stamp at or before the specified expiry time.
41 * This would typically be called once per frame by applications which are doing database paging,
42 * and need to prune objects that are no longer required, and called after the a called
43 * after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(expirtyTime).*/
44 void removeExpiredObjectsInCache(double expiryTime);
46 /** Remove all objects in the cache regardless of having external references or expiry times.*/
49 /** Add contents of specified ObjectCache to this object cache.*/
50 void addObjectCache(ObjectCache* object);
52 /** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/
53 void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0, const Options *options = NULL);
55 /** Remove Object from cache.*/
56 void removeFromObjectCache(const std::string& fileName, const Options *options = NULL);
58 /** Deprecated, the getFromObjectCache() returns a C pointer that is not thread safe when using database paging, please use the thread safe getRefFromObjectCache() method instead. */
59 osg::Object* getFromObjectCache(const std::string& fileName, const Options *options = NULL);
61 /** Get a thread safe ref_ptr<Object> from the object cache*/
62 osg::ref_ptr<osg::Object> getRefFromObjectCache(const std::string& fileName, const Options *options = NULL);
64 /** call rleaseGLObjects on all objects attached to the object cache.*/
65 void releaseGLObjects(osg::State* state);
69 virtual ~ObjectCache();
71 typedef std::pair<std::string, osg::ref_ptr<const osgDB::Options> > FileNameOptionsPair;
75 bool operator() (const ObjectCache::FileNameOptionsPair& lhs, const ObjectCache::FileNameOptionsPair& rhs) const;
79 typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
80 typedef std::map<FileNameOptionsPair, ObjectTimeStampPair, ClassComp> ObjectCacheMap;
82 ObjectCacheMap::iterator find(const std::string& fileName, const osgDB::Options* options);
84 ObjectCacheMap _objectCache;
85 OpenThreads::Mutex _objectCacheMutex;