HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stageCache.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_USD_STAGE_CACHE_H
8 #define PXR_USD_USD_STAGE_CACHE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usd/api.h"
15 
16 #include <string>
17 #include <memory>
18 #include <mutex>
19 #include <vector>
20 
22 
23 
26 
27 class ArResolverContext;
28 
30 
31 /// \class UsdStageCache
32 ///
33 /// A strongly concurrency safe collection of UsdStageRefPtr s, enabling
34 /// sharing across multiple clients and threads. See UsdStageCacheContext for
35 /// typical use cases finding UsdStage s in a cache and publishing UsdStage s to
36 /// a cache.
37 ///
38 /// UsdStageCache is strongly thread safe: all operations other than
39 /// construction and destruction may be performed concurrently.
40 ///
41 /// Clients typically populate and fetch UsdStage s in caches by binding a
42 /// UsdStageCacheContext object to a cache, then using the UsdStage::Open() API.
43 /// See UsdStageCacheContext for more details. Clients may also populate and
44 /// fetch directly via UsdStageCache::Insert(), UsdStageCache::Find(),
45 /// UsdStageCache::FindOneMatching(), and UsdStageCache::FindAllMatching()
46 /// API.
47 ///
48 /// Caches provide a mechanism that associates a lightweight key,
49 /// UsdStageCache::Id, with a cached stage. A UsdStageCache::Id can be
50 /// converted to and from long int and string. This can be useful for
51 /// communicating within a third party application that cannot transmit
52 /// arbitrary C++ objects. See UsdStageCache::GetId().
53 ///
54 /// Clients may iterate all cache elements using UsdStageCache::GetAllStages()
55 /// and remove elements with UsdStageCache::Erase(),
56 /// UsdStageCache::EraseAll(), and UsdStageCache::Clear().
57 ///
58 /// Note that this class is a regular type: it can be copied and assigned at
59 /// will. It is not a singleton. Also, since it holds a collection of
60 /// UsdStageRefPtr objects, copying it does not create new UsdStage instances,
61 /// it merely copies the RefPtrs.
62 ///
63 /// Enabling the USD_STAGE_CACHE TF_DEBUG code will issue debug output for
64 /// UsdStageCache Find/Insert/Erase/Clear operations. Also see
65 /// UsdStageCache::SetDebugName() and UsdStageCache::GetDebugName().
66 ///
68 {
69 public:
70  /// \class Id
71  ///
72  /// A lightweight identifier that may be used to identify a
73  /// particular cached stage within a UsdStageCache. An identifier may be
74  /// converted to and from long int and string, to facilitate use within
75  /// restricted contexts.
76  ///
77  /// Id objects are only valid with the stage from which they were obtained.
78  /// It never makes sense to use an Id with a stage other than the one it was
79  /// obtained from.
80  ///
81  struct Id {
82  /// Default construct an invalid id.
83  Id() : _value(-1) {}
84 
85  /// Create an Id from an integral value. The supplied \p val must have
86  /// been obtained by calling ToLongInt() previously.
87  static Id FromLongInt(long int val) { return Id(val); }
88 
89  /// Create an Id from a string value. The supplied \p val must have
90  /// been obtained by calling ToString() previously.
91  static Id FromString(const std::string &s) {
92  bool overflow = false;
93  const long int result = TfStringToLong(s, &overflow);
94  if (overflow) {
96  "'%s' overflowed during conversion to int64_t.",
97  s.c_str()
98  );
99  }
100  return FromLongInt(result);
101  }
102 
103  /// Convert this Id to an integral representation.
104  long int ToLongInt() const { return _value; }
105 
106  /// Convert this Id to a string representation.
107  std::string ToString() const {
108  return TfStringify(ToLongInt());
109  }
110 
111  /// Return true if this Id is valid.
112  bool IsValid() const { return _value != -1; }
113 
114  /// Return true if this Id is valid.
115  explicit operator bool() const { return IsValid(); }
116 
117  private:
118  /// Equality comparison.
119  friend bool operator==(const Id &lhs, const Id &rhs) {
120  return lhs.ToLongInt() == rhs.ToLongInt();
121  }
122  /// Inequality comparison.
123  friend bool operator!=(const Id &lhs, const Id &rhs) {
124  return !(lhs == rhs);
125  }
126  /// Less-than comparison.
127  friend bool operator<(const Id &lhs, const Id &rhs) {
128  return lhs.ToLongInt() < rhs.ToLongInt();
129  }
130  /// Less-than or equal comparison.
131  friend bool operator<=(const Id &lhs, const Id &rhs) {
132  return !(rhs < lhs);
133  }
134  /// Greater-than comparison.
135  friend bool operator>(const Id &lhs, const Id &rhs) {
136  return rhs < lhs;
137  }
138  /// Greater-than or equal comparison.
139  friend bool operator>=(const Id &lhs, const Id &rhs) {
140  return !(lhs < rhs);
141  }
142  /// Hash.
143  friend size_t hash_value(Id id) {
144  return ~size_t(id.ToLongInt());
145  }
146 
147  explicit Id(long int val) : _value(val) {}
148 
149  long int _value;
150  };
151 
152  /// Default construct an empty cache.
153  USD_API
154  UsdStageCache();
155 
156  /// Construct a new cache as a copy of \p other.
157  USD_API
158  UsdStageCache(const UsdStageCache &other);
159 
160  /// Destructor.
161  USD_API
162  ~UsdStageCache();
163 
164  /// Replace the contents of this cache with a copy of \p other.
165  USD_API
166  UsdStageCache &operator=(const UsdStageCache &other);
167 
168  /// Swap the contents of this cache with \p other.
169  USD_API
170  void swap(UsdStageCache &other);
171 
172  /// Return a vector containing the stages present in this cache.
173  USD_API
174  std::vector<UsdStageRefPtr> GetAllStages() const;
175 
176  /// Return the number of stages present in this cache.
177  USD_API
178  size_t Size() const;
179 
180  /// Return true if this cache holds no stages, false otherwise.
181  bool IsEmpty() const { return Size() == 0; }
182 
183  /// Find an existing stage in the cache that satisfies \p request, or invoke
184  /// request.Manufacture() to create one and insert it into the cache.
185  /// Return the resulting stage and a bool indicating whether or not this
186  /// call manufactured the stage.
187  ///
188  /// This avoids race conditions in concurrent code that can occur using the
189  /// other public methods. Consider this racy example:
190  ///
191  /// \code
192  /// if (!cache.FindOneMatching(rootLayer)) {
193  /// auto stage = UsdStage::Open(rootLayer);
194  /// cache.Insert(stage);
195  /// }
196  /// \endcode
197  ///
198  /// This will race with another thread doing the same thing, resulting in
199  /// two stages with the same root layer inserted in the cache. This is
200  /// potentially rather inefficient since stage creation can be expensive,
201  /// depending on how many objects and how many prims & layers the stage
202  /// contains. RequestStage() avoids this by ensuring that there is no race
203  /// and the stage is created only once.
204  ///
205  /// Note that request should not be retained and must not be reused.
206  USD_API
207  std::pair<UsdStageRefPtr, bool>
209 
210  /// Find the stage in this cache corresponding to \p id in this cache. If
211  /// \p id is not valid (see Id::IsValid()) or if this cache does not have a
212  /// stage corresponding to \p id, return null.
213  USD_API
214  UsdStageRefPtr Find(Id id) const;
215 
216  /// Find a stage in this cache with \p rootLayer. If there is no matching
217  /// stage in this cache, return null. If there is more than one matching
218  /// stage in this cache, return an arbitrary matching one. See also
219  /// FindAllMatching().
220  USD_API
221  UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer) const;
222 
223  /// Find a stage in this cache with \p rootLayer and \p sessionLayer. If
224  /// there is no matching stage in this cache, return null. If there is more
225  /// than one matching stage in this cache, return an arbitrary matching one.
226  /// See also FindAllMatching().
227  USD_API
228  UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer,
229  const SdfLayerHandle &sessionLayer) const;
230 
231  /// Find a stage in this cache with \p rootLayer and \p pathResolverContext.
232  /// If there is no matching stage in this cache, return null. If there is
233  /// more than one matching stage in this cache, return an arbitrary matching
234  /// one.
235  /// \sa FindAllMatching()
236  USD_API
237  UsdStageRefPtr FindOneMatching(
238  const SdfLayerHandle &rootLayer,
239  const ArResolverContext &pathResolverContext) const;
240 
241  /// Find a stage in this cache with \p rootLayer, \p sessionLayer, and
242  /// \p pathResolverContext. If there is no matching stage in this cache,
243  /// return null. If there is more than one matching stage in this cache,
244  /// return an arbitrary matching one.
245  /// \sa FindAllMatching()
246  USD_API
247  UsdStageRefPtr FindOneMatching(
248  const SdfLayerHandle &rootLayer,
249  const SdfLayerHandle &sessionLayer,
250  const ArResolverContext &pathResolverContext) const;
251 
252  /// Find all stages in this cache with \p rootLayer. If there is no
253  /// matching stage in this cache, return an empty vector.
254  USD_API
255  std::vector<UsdStageRefPtr>
256  FindAllMatching(const SdfLayerHandle &rootLayer) const;
257 
258  /// Find all stages in this cache with \p rootLayer and \p sessionLayer.
259  /// If there is no matching stage in this cache, return an empty vector.
260  USD_API
261  std::vector<UsdStageRefPtr>
262  FindAllMatching(const SdfLayerHandle &rootLayer,
263  const SdfLayerHandle &sessionLayer) const;
264 
265  /// Find all stages in this cache with \p rootLayer and
266  /// \p pathResolverContext. If there is no matching stage in this cache,
267  /// return an empty vector.
268  USD_API
269  std::vector<UsdStageRefPtr>
270  FindAllMatching(const SdfLayerHandle &rootLayer,
271  const ArResolverContext &pathResolverContext) const;
272 
273  /// Find all stages in this cache with \p rootLayer, \p sessionLayer, and
274  /// \p pathResolverContext. If there is no matching stage in this cache,
275  /// return an empty vector. If there is more than one matching stage in
276  /// this cache, return an arbitrary matching one.
277  USD_API
278  std::vector<UsdStageRefPtr>
279  FindAllMatching(const SdfLayerHandle &rootLayer,
280  const SdfLayerHandle &sessionLayer,
281  const ArResolverContext &pathResolverContext) const;
282 
283  /// Return the Id associated with \p stage in this cache. If \p stage is
284  /// not present in this cache, return an invalid Id.
285  USD_API
286  Id GetId(const UsdStageRefPtr &stage) const;
287 
288  /// Return true if \p stage is present in this cache, false otherwise.
289  bool Contains(const UsdStageRefPtr &stage) const {
290  return static_cast<bool>(GetId(stage));
291  }
292 
293  /// Return true if \p id is present in this cache, false otherwise.
294  bool Contains(Id id) const { return Find(id); }
295 
296  /// Insert \p stage into this cache and return its associated Id. If the
297  /// given \p stage is already present in this cache, simply return its
298  /// associated Id.
299  USD_API
300  Id Insert(const UsdStageRefPtr &stage);
301 
302  /// Erase the stage identified by \p id from this cache and return true. If
303  /// \p id is invalid or there is no associated stage in this cache, do
304  /// nothing and return false. Since the cache contains UsdStageRefPtr,
305  /// erasing a stage from the cache will only destroy the stage if no other
306  /// UsdStageRefPtrs exist referring to it.
307  USD_API
308  bool Erase(Id id);
309 
310  /// Erase \p stage from this cache and return true. If \p stage is not
311  /// present in this cache, do nothing and return false. Since the cache
312  /// contains UsdStageRefPtr, erasing a stage from the cache will only
313  /// destroy the stage if no other UsdStageRefPtrs exist referring to it.
314  USD_API
315  bool Erase(const UsdStageRefPtr &stage);
316 
317  /// Erase all stages present in the cache with \p rootLayer and return the
318  /// number erased. Since the cache contains UsdStageRefPtr, erasing a stage
319  /// from the cache will only destroy the stage if no other UsdStageRefPtrs
320  /// exist referring to it.
321  USD_API
322  size_t EraseAll(const SdfLayerHandle &rootLayer);
323 
324  /// Erase all stages present in the cache with \p rootLayer and
325  /// \p sessionLayer and return the number erased. Since the cache contains
326  /// UsdStageRefPtr, erasing a stage from the cache will only destroy the
327  /// stage if no other UsdStageRefPtrs exist referring to it.
328  USD_API
329  size_t EraseAll(const SdfLayerHandle &rootLayer,
330  const SdfLayerHandle &sessionLayer);
331 
332  /// Erase all stages present in the cache with \p rootLayer,
333  /// \p sessionLayer, and \p pathResolverContext and return the number
334  /// erased. Since the cache contains UsdStageRefPtr, erasing a stage from
335  /// the cache will only destroy the stage if no other UsdStageRefPtrs
336  /// exist referring to it.
337  USD_API
338  size_t EraseAll(const SdfLayerHandle &rootLayer,
339  const SdfLayerHandle &sessionLayer,
340  const ArResolverContext &pathResolverContext);
341 
342  /// Remove all entries from this cache, leaving it empty and equivalent to a
343  /// default-constructed cache. Since the cache contains UsdStageRefPtr,
344  /// erasing a stage from the cache will only destroy the stage if no other
345  /// UsdStageRefPtrs exist referring to it.
346  USD_API
347  void Clear();
348 
349  /// Assign a debug name to this cache. This will be emitted in debug output
350  /// messages when the USD_STAGE_CACHES debug flag is enabled. If set to the
351  /// empty string, the cache's address will be used instead.
352  USD_API
353  void SetDebugName(const std::string &debugName);
354 
355  /// Retrieve this cache's debug name, set with SetDebugName(). If no debug
356  /// name has been assigned, return the empty string.
357  USD_API
358  std::string GetDebugName() const;
359 
360 private:
361  friend void swap(UsdStageCache &lhs, UsdStageCache &rhs) {
362  lhs.swap(rhs);
363  }
364 
365  typedef struct Usd_StageCacheImpl _Impl;
366  std::unique_ptr<_Impl> _impl;
367  mutable std::mutex _mutex;
368 };
369 
371 {
372 public:
373  USD_API
374  virtual ~UsdStageCacheRequest();
375 
376  // Return true if the stage satisfies this request.
377  virtual bool IsSatisfiedBy(UsdStageRefPtr const &stage) const = 0;
378 
379  // Return true if the pending request will satisfy this request, once
380  // complete.
381  virtual bool IsSatisfiedBy(UsdStageCacheRequest const &pending) const = 0;
382 
383  // Invoked to manufacture a stage to insert in the cache. Postcondition:
384  // IsSatisfiedBy() must return true for the resulting stage.
385  virtual UsdStageRefPtr Manufacture() = 0;
386 
387 private:
388  friend class UsdStageCache;
389 
390  struct _Mailbox;
391  void _Subscribe(_Mailbox *);
392 
393  struct _Data;
394  struct _DataDeleter { void operator()(_Data *); };
395  std::unique_ptr<_Data, _DataDeleter> _data;
396 };
397 
398 
400 
401 #endif // PXR_USD_USD_STAGE_CACHE_H
USD_API UsdStageCache()
Default construct an empty cache.
Definition: layer.h:81
virtual USD_API ~UsdStageCacheRequest()
#define USD_API
Definition: api.h:23
TF_API long TfStringToLong(const std::string &txt, bool *outOfRange=NULL)
#define TF_CODING_ERROR
GLdouble s
Definition: glad.h:3009
friend size_t hash_value(Id id)
Hash.
Definition: stageCache.h:143
USD_API std::vector< UsdStageRefPtr > GetAllStages() const
Return a vector containing the stages present in this cache.
USD_API std::pair< UsdStageRefPtr, bool > RequestStage(UsdStageCacheRequest &&request)
**But if you need a result
Definition: thread.h:622
static Id FromString(const std::string &s)
Definition: stageCache.h:91
USD_API UsdStageRefPtr Find(Id id) const
OutGridT const XformOp bool bool
USD_API UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer) const
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfLayer)
USD_API void Clear()
bool Contains(Id id) const
Return true if id is present in this cache, false otherwise.
Definition: stageCache.h:294
USD_API void SetDebugName(const std::string &debugName)
USD_API bool Erase(Id id)
virtual bool IsSatisfiedBy(UsdStageRefPtr const &stage) const =0
friend void swap(UsdStageCache &lhs, UsdStageCache &rhs)
Definition: stageCache.h:361
USD_API size_t Size() const
Return the number of stages present in this cache.
bool IsEmpty() const
Return true if this cache holds no stages, false otherwise.
Definition: stageCache.h:181
friend bool operator==(const Id &lhs, const Id &rhs)
Equality comparison.
Definition: stageCache.h:119
std::string ToString() const
Convert this Id to a string representation.
Definition: stageCache.h:107
USD_API Id GetId(const UsdStageRefPtr &stage) const
std::string TfStringify(const T &v)
Definition: stringUtils.h:556
bool Contains(const UsdStageRefPtr &stage) const
Return true if stage is present in this cache, false otherwise.
Definition: stageCache.h:289
Id()
Default construct an invalid id.
Definition: stageCache.h:83
USD_API UsdStageCache & operator=(const UsdStageCache &other)
Replace the contents of this cache with a copy of other.
friend bool operator>(const Id &lhs, const Id &rhs)
Greater-than comparison.
Definition: stageCache.h:135
USD_API Id Insert(const UsdStageRefPtr &stage)
USD_API ~UsdStageCache()
Destructor.
static Id FromLongInt(long int val)
Definition: stageCache.h:87
friend bool operator>=(const Id &lhs, const Id &rhs)
Greater-than or equal comparison.
Definition: stageCache.h:139
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
friend bool operator<(const Id &lhs, const Id &rhs)
Less-than comparison.
Definition: stageCache.h:127
USD_API std::vector< UsdStageRefPtr > FindAllMatching(const SdfLayerHandle &rootLayer) const
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USD_API void swap(UsdStageCache &other)
Swap the contents of this cache with other.
bool IsValid() const
Return true if this Id is valid.
Definition: stageCache.h:112
friend bool operator!=(const Id &lhs, const Id &rhs)
Inequality comparison.
Definition: stageCache.h:123
virtual UsdStageRefPtr Manufacture()=0
long int ToLongInt() const
Convert this Id to an integral representation.
Definition: stageCache.h:104
TF_DECLARE_REF_PTRS(UsdStage)
friend bool operator<=(const Id &lhs, const Id &rhs)
Less-than or equal comparison.
Definition: stageCache.h:131
USD_API size_t EraseAll(const SdfLayerHandle &rootLayer)
USD_API std::string GetDebugName() const