VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtOsgReferencedCollection.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2023 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <osg/ref_ptr>
13 #include <osg/Texture>
14 #include <boost/thread.hpp>
15 
16 #include <unordered_set>
17 #include <unordered_map>
18 #include <string>
19 
20 namespace osg
21 {
22  class State;
23 }
24 
25 namespace makVrv
26 {
30  template <typename OsgObject>
32  {
33  public:
34 
35  typedef std::unordered_map<std::string,osg::ref_ptr<OsgObject> >
37  typedef std::vector<std::pair<std::string,OsgObject*> > ReferenceList;
38 
40  DtOsgReferencedCollection(unsigned int initialCapacity);
41 
45  inline void lock() { myMutex.lock(); }
46 
50 
53  inline InstanceMap& getUnsafeMap() { return myInstanceMap; }
54 
56  unsigned int unsafeListSize() const;
57 
59  inline void unlock() { myMutex.unlock(); }
60 
67  bool findOrBlockWhilePending(const std::string& name, osg::ref_ptr<OsgObject>& object );
68 
70  osg::ref_ptr<OsgObject> find(const std::string& filename);
71 
74  void insert(const std::string& name,const osg::ref_ptr<OsgObject>& obj);
75 
77  void releaseGLObjects(osg::State* state);
78 
80  void clear();
81 
82 
86  bool findOrAddTexture(const std::string& filename,
87  osg::Texture::FilterMode minFilter,osg::Texture::FilterMode magFilter,
88  osg::Texture::WrapMode s, osg::Texture::WrapMode t, osg::ref_ptr<osg::Texture>& texture,
89  bool unloadImageDataFlag, bool compressTexturesFlag, const std::string& alternateFilename = "");
90 
93  bool findTexture(const std::string& filename,
94  osg::Texture::FilterMode minFilter,osg::Texture::FilterMode magFilter,
95  osg::Texture::WrapMode s, osg::Texture::WrapMode t, bool compressedTexturesFlag, osg::ref_ptr<osg::Texture>& texture);
96 
99  void addTexture(const std::string& filename,
100  osg::ref_ptr<osg::Texture> texture, bool unloadImageDataFlag, bool compressTexturesFlag, std::string alternateFilename = "");
101 
102 
105  unsigned int flush();
106 
109  unsigned int partialFlush(unsigned int maxToCheck,unsigned int maxToFlush);
110 
113  unsigned int flushTextures();
114 
117  unsigned int partialFlushTextures(unsigned int maxToCheck,unsigned int maxToFlush);
118 
119 
120  protected:
121 
123  {
124  const std::string& myStr;
125  FindReference(const std::string& str) : myStr(str){}
126  bool operator()(const std::pair<std::string,OsgObject*>& ref)
127  {
128  return ref.first == myStr;
129  }
130  };
131 
132  typedef std::unordered_set<std::string> PendingSet;
133  boost::mutex myMutex;
134  //use this condition variable to wait for the instance that is pending to be loaded
135  boost::condition_variable myPendingInstanceDone;
140  unsigned int myPartialFlushIndex;
141  };
142 
143 }
144 
bool findOrBlockWhilePending(const std::string &name, osg::ref_ptr< OsgObject > &object)
Find an object with a given name or save the name as pending. This function will block if the object ...
ReferenceList myRefInstanceList
Definition: DtOsgReferencedCollection.h:137
const std::string & myStr
Definition: DtOsgReferencedCollection.h:124
osg::ref_ptr< OsgObject > find(const std::string &filename)
Look for an object and return a value (null or not).
boost::mutex myMutex
Definition: DtOsgReferencedCollection.h:133
bool findTexture(const std::string &filename, osg::Texture::FilterMode minFilter, osg::Texture::FilterMode magFilter, osg::Texture::WrapMode s, osg::Texture::WrapMode t, bool compressedTexturesFlag, osg::ref_ptr< osg::Texture > &texture)
Find a texture with the given image name.
unsigned int flush()
Do a partial flush of items.
std::unordered_set< std::string > PendingSet
Definition: DtOsgReferencedCollection.h:132
bool findOrAddTexture(const std::string &filename, osg::Texture::FilterMode minFilter, osg::Texture::FilterMode magFilter, osg::Texture::WrapMode s, osg::Texture::WrapMode t, osg::ref_ptr< osg::Texture > &texture, bool unloadImageDataFlag, bool compressTexturesFlag, const std::string &alternateFilename="")
Find a texture with the given image name. If it is not found the texture input is added to the cache ...
std::vector< std::pair< std::string, OsgObject * > > ReferenceList
Definition: DtOsgReferencedCollection.h:37
DtOsgReferencedCollection(unsigned int initialCapacity)
Constructor.
void lock()
Lock the collection.
Definition: DtOsgReferencedCollection.h:45
A thread safe collection, used by the mult-threaded cache, of OSG referenced objects.
Definition: DtOsgReferencedCollection.h:31
unsigned int myPartialFlushIndex
Definition: DtOsgReferencedCollection.h:140
void releaseGLObjects(osg::State *state)
Release GL objects on all objects in the cache.
PendingSet myPendingInstances
Definition: DtOsgReferencedCollection.h:138
PendingSet myFailedInstances
Definition: DtOsgReferencedCollection.h:139
Definition: DtOsgReferencedCollection.h:122
void unlock()
Unlock the collection.
Definition: DtOsgReferencedCollection.h:59
void addTexture(const std::string &filename, osg::ref_ptr< osg::Texture > texture, bool unloadImageDataFlag, bool compressTexturesFlag, std::string alternateFilename="")
add a texture for the given image name. also processes the texture to put on a callback for handling ...
std::unordered_map< std::string, osg::ref_ptr< OsgObject > > InstanceMap
Definition: DtOsgReferencedCollection.h:36
unsigned int flushTextures()
Do a flush of all textures.
InstanceMap myInstanceMap
Definition: DtOsgReferencedCollection.h:136
FindReference(const std::string &str)
Definition: DtOsgReferencedCollection.h:125
InstanceMap & getUnsafeMap()
Get the unsafe map, to make this safe you MUST lock the collection first.
Definition: DtOsgReferencedCollection.h:53
unsigned int unsafeListSize() const
returns size of collection
boost::condition_variable myPendingInstanceDone
Definition: DtOsgReferencedCollection.h:135
void clear()
Clears all of the instances.
bool operator()(const std::pair< std::string, OsgObject * > &ref)
Definition: DtOsgReferencedCollection.h:126
unsigned int partialFlushTextures(unsigned int maxToCheck, unsigned int maxToFlush)
Do a partial flush of items.
void insert(const std::string &name, const osg::ref_ptr< OsgObject > &obj)
Insert an instance into the cache Needs to be called if an object is pending (pass 0 to remove from p...
unsigned int partialFlush(unsigned int maxToCheck, unsigned int maxToFlush)
Do a partial flush of items.
ReferenceList & getUnsafeList()
Get the unsafe list, to make this safe you MUST lock the collection first.
Definition: DtOsgReferencedCollection.h:49

Document ID: Generated on Tue Sep 24 19:28:17 EDT 2024 from SVN revision 269799
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)