ObjectCache 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  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.
  7. *
  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.
  12. */
  13. #ifndef OSGDB_OBJECTCACHE
  14. #define OSGDB_OBJECTCACHE 1
  15. #include <osg/Node>
  16. #include <osgDB/ReaderWriter>
  17. #include <osgDB/DatabaseRevisions>
  18. #include <map>
  19. namespace osgDB {
  20. class OSGDB_EXPORT ObjectCache : public osg::Referenced
  21. {
  22. public:
  23. ObjectCache();
  24. /** For each object in the cache which has an reference count greater than 1
  25. * (and therefore referenced by elsewhere in the application) set the time stamp
  26. * for that object in the cache to specified time.
  27. * This would typically be called once per frame by applications which are doing database paging,
  28. * and need to prune objects that are no longer required.
  29. * The time used should be taken from the FrameStamp::getReferenceTime().*/
  30. void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime);
  31. /** Removed object in the cache which have a time stamp at or before the specified expiry time.
  32. * This would typically be called once per frame by applications which are doing database paging,
  33. * and need to prune objects that are no longer required, and called after the a called
  34. * after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(expirtyTime).*/
  35. void removeExpiredObjectsInCache(double expiryTime);
  36. /** Remove all objects in the cache regardless of having external references or expiry times.*/
  37. void clear();
  38. /** Add contents of specified ObjectCache to this object cache.*/
  39. void addObjectCache(ObjectCache* object);
  40. /** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/
  41. void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0, const Options *options = NULL);
  42. /** Remove Object from cache.*/
  43. void removeFromObjectCache(const std::string& fileName, const Options *options = NULL);
  44. /** Deprecated, the getFromObjectCache() returns a C pointer that is not thread safe when using database paging, please use the thread safe getRefFromObjectCache() method instead. */
  45. osg::Object* getFromObjectCache(const std::string& fileName, const Options *options = NULL);
  46. /** Get a thread safe ref_ptr<Object> from the object cache*/
  47. osg::ref_ptr<osg::Object> getRefFromObjectCache(const std::string& fileName, const Options *options = NULL);
  48. /** call rleaseGLObjects on all objects attached to the object cache.*/
  49. void releaseGLObjects(osg::State* state);
  50. protected:
  51. virtual ~ObjectCache();
  52. typedef std::pair<std::string, osg::ref_ptr<const osgDB::Options> > FileNameOptionsPair;
  53. struct ClassComp
  54. {
  55. bool operator() (const ObjectCache::FileNameOptionsPair& lhs, const ObjectCache::FileNameOptionsPair& rhs) const;
  56. };
  57. typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
  58. typedef std::map<FileNameOptionsPair, ObjectTimeStampPair, ClassComp> ObjectCacheMap;
  59. ObjectCacheMap::iterator find(const std::string& fileName, const osgDB::Options* options);
  60. ObjectCacheMap _objectCache;
  61. OpenThreads::Mutex _objectCacheMutex;
  62. };
  63. }
  64. #endif