HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GU_AgentFileCache.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GU_AgentFileCache.h (GU Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GU_AgentFileCache__
12 #define __GU_AgentFileCache__
13 
14 #include "GU_API.h"
15 
16 #include <UT/UT_FileStat.h>
17 #include <UT/UT_IntrusivePtr.h>
18 #include <UT/UT_Lock.h>
19 #include <UT/UT_StringMap.h>
20 #include <UT/UT_WorkBuffer.h>
21 
22 template <typename T>
24 {
25 public:
26  /// Searches for a cached file with the same key, and returns it if the
27  /// timestamp matches the file on disk. Otherwise, the callback is invoked
28  /// to load the file, which is then inserted into the cache and returned.
29  /// The key should include the filename and any dependencies on other agent
30  /// files (e.g. when loading the same clip for slightly different rigs).
31  template <typename F>
32  UT_IntrusivePtr<T> findCachedFileOrLoad(const UT_StringRef &filename,
33  const UT_StringRef &key,
34  F load_new_item);
35 
36  /// Removes a cached file with the given key.
37  void removeCachedFile(const UT_StringRef &key);
38 
39 private:
40  struct CachedFile
41  {
42  CachedFile(T *ptr = nullptr, time_t mod_time = 0)
43  : myPtr(ptr), myModTime(mod_time)
44  {
45  }
46 
47  T *myPtr;
48  time_t myModTime;
49  };
50 
51  UT_Lock myMutex;
53 };
54 
55 /// Builds a list of paths to use when attempting to load an external file,
56 /// ordered by priority.
57 GU_API extern void GUgetAgentFilePaths(UT_StringArray &paths,
58  const UT_StringHolder &condensed_path,
59  const UT_StringHolder &expanded_path);
60 
61 template <typename T>
62 template <typename F>
65  const UT_StringRef &key,
66  F load_new_item)
67 {
68  UT_FileStat info;
69  time_t mod_time = 0;
70  if (UTfileStat(filename, &info) == 0)
71  mod_time = info.myModTime;
72 
73  UT_Lock::Scope lock(myMutex);
74 
75  auto item = myFiles.find(key);
76  if (item != myFiles.end() && item->second.myModTime == mod_time)
77  {
78  return item->second.myPtr;
79  }
80  else
81  {
82  auto new_item = load_new_item();
83  if (new_item)
84  myFiles[key] = CachedFile(new_item.get(), mod_time);
85 
86  return new_item;
87  }
88 }
89 
90 template <typename T>
91 void
93 {
94  UT_Lock::Scope lock(myMutex);
95  myFiles.erase(key);
96 }
97 
98 #endif
GU_API void GUgetAgentFilePaths(UT_StringArray &paths, const UT_StringHolder &condensed_path, const UT_StringHolder &expanded_path)
png_voidp ptr
Definition: png.h:2145
UT_API int UTfileStat(const char *path, UT_FileStat *file_stat)
void removeCachedFile(const UT_StringRef &key)
Removes a cached file with the given key.
Wrapper around hboost::intrusive_ptr.
#define GU_API
Definition: GU_API.h:11
time_t myModTime
Definition: UT_FileStat.h:74
UT_IntrusivePtr< T > findCachedFileOrLoad(const UT_StringRef &filename, const UT_StringRef &key, F load_new_item)
png_infop png_timep * mod_time
Definition: png.h:2510