vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PersistentCache.h
Go to the documentation of this file.
1 
10 #ifndef PERSISTENT_CACHE_H
11 #define PERSISTENT_CACHE_H
12 
13 #include <vrecko/Export>
14 #include <vrecko/MTLock.h>
15 #include <helpers/DynamicArray.h>
16 #include <helpers/BufFile.h>
17 
18 #include <osg/Node>
19 
20 
21 
22 namespace vrecko {
23 
24 
25 class EnvironmentObject;
26 
27 
55 class VRECKO_EXPORT PersistentCache {
56 public:
57 
59  friend PersistentCache;
60  public:
62  ~OpenedCacheFile();
63 
64  inline const std::string& getTranslatedFileName() { return translatedFileName; }
65  inline const std::string& getCachedFileName() { return cachedFileName; }
66 
67  protected:
69  std::string translatedFileName;
70 
72  std::string cachedFileName;
73 
75  int iReaders, iWaitingWriters, iWriters;
76  };
77 
78 
80  ~PersistentCache();
81 
83  void setEnable(bool bNewEnableCache);
84 
86  inline bool getEnable() { return bEnableCache; }
87 
88  const char* getCacheDir() { return cacheDir; }
89 
94  osg::Node* loadModel(const char *relatedFileName, vrecko::EnvironmentObject *obj);
95 
100  bool saveModel(const char *relatedFileName, osg::Node *node, vrecko::EnvironmentObject *obj);
101 
108  inline bool openCacheFileForReading(const char *dataName, const char *relatedFileName, unsigned long version,
109  OpenedCacheFile** cacheData, vrecko::BufferedFile **bfile, unsigned long ulWaitTimeout = 0)
110  { return internalOpenReadWrite(dataName, relatedFileName, version, cacheData, bfile, NULL, ulWaitTimeout); }
111 
113  void closeCacheFile(OpenedCacheFile* cacheData, vrecko::BufferedFile *bfile);
114 
120  inline bool openCacheFileForWriting(const char *dataName, const char *relatedFileName, unsigned long version,
121  OpenedCacheFile** cacheData, vrecko::BufferedFileWrite **bfile, unsigned long ulWaitTimeout = 0)
122  { return internalOpenReadWrite(dataName, relatedFileName, version, cacheData, NULL, bfile, ulWaitTimeout); }
123 
125  void closeCacheFile(OpenedCacheFile* cacheData, vrecko::BufferedFileWrite *bfile);
126 
127 
128 protected:
129 
131 
135 
139 
141  CHAR baseDir[1000];
142 
144  CHAR cacheDir[1000];
145 
150  void translateName(char *outTranslatedName, const char *inDataName, const char *inRelatedFileName);
151 
152 
153 
154  //bool isInCache(const char *dataName, const char *relatedFileName, char *outFullCachedFileName = NULL);
155  // Check whether a specified file/data is in the cache.
156  // The REAL file MUST exist - its time stamp is taken into account when checking the validity
157  // of the cached version (if the cached version is stored on the disk).
158 
170  int internalFindAndLock(const char *originalFileName, const char *translatedFileName, unsigned long version,
171  const char* desiredExtension, bool bReading, unsigned long ulWaitTimeout);
172 
173  bool internalOpenReadWrite(const char *dataName, const char *relatedFileName, unsigned long version,
174  OpenedCacheFile** cacheData, vrecko::BufferedFile **bfile, vrecko::BufferedFileWrite **bfileWrite, unsigned long ulWaitTimeout);
175 
181  void internalDecrementReaderWriter(OpenedCacheFile* cacheData, bool bReading);
182 
186  bool testVersionIsAtLeast(const char *fileName, int minVersion);
187 
193  void constructModelDataName(char *outDataName, vrecko::EnvironmentObject* obj);
194 };
195 
196 
197 }; // namespace vrecko
198 
199 #endif