OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
objectcache.hpp
Go to the documentation of this file.
1 // Resource ObjectCache for OpenMW, forked from osgDB ObjectCache by Robert Osfield, see copyright notice below.
2 // The main change from the upstream version is that removeExpiredObjectsInCache no longer keeps a lock while the unref happens.
3 
4 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
5  *
6  * This library is open source and may be redistributed and/or modified under
7  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
8  * (at your option) any later version. The full license is in LICENSE file
9  * included with this distribution, and on the openscenegraph.org website.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * OpenSceneGraph Public License for more details.
15 */
16 
17 #ifndef OPENMW_COMPONENTS_RESOURCE_OBJECTCACHE
18 #define OPENMW_COMPONENTS_RESOURCE_OBJECTCACHE
19 
20 #include <osg/Referenced>
21 #include <osg/ref_ptr>
22 
23 #include <string>
24 #include <map>
25 
26 namespace osg
27 {
28  class Object;
29  class State;
30  class NodeVisitor;
31 }
32 
33 namespace Resource {
34 
35 class ObjectCache : public osg::Referenced
36 {
37  public:
38 
39  ObjectCache();
40 
48 
53  void removeExpiredObjectsInCache(double expiryTime);
54 
56  void clear();
57 
59  void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0);
60 
62  void removeFromObjectCache(const std::string& fileName);
63 
65  osg::ref_ptr<osg::Object> getRefFromObjectCache(const std::string& fileName);
66 
68  bool checkInObjectCache(const std::string& fileName, double timeStamp);
69 
71  void releaseGLObjects(osg::State* state);
72 
74  void accept(osg::NodeVisitor& nv);
75 
77  template <class Functor>
78  void call(Functor& f)
79  {
80  OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
81  for (ObjectCacheMap::iterator it = _objectCache.begin(); it != _objectCache.end(); ++it)
82  f(it->second.first.get());
83  }
84 
86  unsigned int getCacheSize() const;
87 
88  protected:
89 
90  virtual ~ObjectCache();
91 
92  typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
93  typedef std::map<std::string, ObjectTimeStampPair > ObjectCacheMap;
94 
96  mutable OpenThreads::Mutex _objectCacheMutex;
97 
98 };
99 
100 }
101 
102 #endif
void removeFromObjectCache(const std::string &fileName)
Definition: objectcache.cpp:114
void call(Functor &f)
Definition: objectcache.hpp:78
std::pair< osg::ref_ptr< osg::Object >, double > ObjectTimeStampPair
Definition: objectcache.hpp:92
std::map< std::string, ObjectTimeStampPair > ObjectCacheMap
Definition: objectcache.hpp:93
ObjectCacheMap _objectCache
Definition: objectcache.hpp:95
void addEntryToObjectCache(const std::string &filename, osg::Object *object, double timestamp=0.0)
Definition: objectcache.cpp:35
void accept(osg::NodeVisitor &nv)
Definition: objectcache.cpp:140
unsigned int getCacheSize() const
Definition: objectcache.cpp:158
ObjectCache()
Definition: objectcache.cpp:26
Definition: objectcache.hpp:35
void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime)
Definition: objectcache.cpp:69
virtual ~ObjectCache()
Definition: objectcache.cpp:31
State
Definition: state.hpp:6
void releaseGLObjects(osg::State *state)
Definition: objectcache.cpp:127
void clear()
Definition: objectcache.cpp:121
OpenThreads::Mutex _objectCacheMutex
Definition: objectcache.hpp:96
bool checkInObjectCache(const std::string &fileName, double timeStamp)
Definition: objectcache.cpp:57
void removeExpiredObjectsInCache(double expiryTime)
Definition: objectcache.cpp:87
osg::ref_ptr< osg::Object > getRefFromObjectCache(const std::string &fileName)
Definition: objectcache.cpp:46