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