HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stageCacheContext.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_CONTEXT_H
8 #define PXR_USD_USD_STAGE_CACHE_CONTEXT_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usd/api.h"
12 #include "pxr/base/tf/stacked.h"
13 
14 #include <vector>
15 
17 
18 
19 class UsdStageCache;
20 
21 // Private helper wrapper class, holds a const reference to a stage cache.
24  : cache(cache) {}
26 };
27 
28 // Using a template arg for 'cache' in UsdUseButDoNotPopulateCache enforces
29 // lvalue requirement: rvalues will not bind to function template non-const
30 // reference parameters.
31 
32 /// Indicate that a UsdStageCacheContext should be bound in a read-only fashion.
33 /// Calls to UsdStage::Open() will attempt to find stages in \p cache when a
34 /// UsdStageCacheContext is present on the stack. See UsdStageCacheContext for
35 /// more details and example use.
36 template <class StageCache>
38 UsdUseButDoNotPopulateCache(StageCache &cache) {
40 }
41 
43 {
44  /// Indicate that a UsdStageCacheContext should ignore all currently bound
45  /// UsdStageCacheContexts, preventing reading from or writing to their
46  /// UsdStageCaches. See UsdStageCache for more details and example use.
48  /// Indicate that a UsdStageCacheContext should ignore all currently bound
49  /// writable UsdStageCacheContexts, writing to their UsdStageCaches. See
50  /// UsdStageCache for more details and example use.
52 
54 };
55 
56 /// \class UsdStageCacheContext
57 ///
58 /// A context object that lets the UsdStage::Open() API read from or read
59 /// from and write to a UsdStageCache instance during a scope of execution.
60 ///
61 /// Code examples illustrate typical use:
62 /// \code
63 /// {
64 /// // A stage cache to work with.
65 /// UsdStageCache stageCache;
66 ///
67 /// // Bind this cache. UsdStage::Open() will attempt to find a matching
68 /// // stage in the cache. If none is found, it will open a new stage and
69 /// // insert it into the cache.
70 /// UsdStageCacheContext context(stageCache);
71 ///
72 /// // Since the cache is currently empty, this Open call will not find an
73 /// // existing stage in the cache, but will insert the newly opened stage
74 /// // in it.
75 /// auto stage = UsdStage::Open(<args>);
76 ///
77 /// assert(stageCache.Contains(stage));
78 ///
79 /// // A subsequent Open() call with the same arguments will retrieve the
80 /// // stage from cache.
81 /// auto stage2 = UsdStage::Open(<args>);
82 /// assert(stage2 == stage);
83 /// }
84 /// \endcode
85 ///
86 /// The UsdStage::Open() API examines caches in UsdStageCacheContexts that exist
87 /// on the stack in the current thread in order starting with the most recently
88 /// created (deepest in the stack) to the least recently created.
89 ///
90 /// The UsdUseButDoNotPopulateCache() function makes a cache available for
91 /// UsdStage::Open() to find stages in, but newly opened stages will not be
92 /// published to it. This can be useful if you want to make use of a cache but
93 /// cannot or do not wish to mutate that cache.
94 ///
95 /// Passing UsdBlockStageCaches disables cache use entirely (as if no
96 /// UsdStageCacheContexts exist on the stack), while
97 /// UsdBlockStageCachePopulation disables writing to all bound caches (as if
98 /// they were all established with UsdUseButDoNotPopulateCache()).
99 ///
100 /// Threading note: Different threads have different call stacks, so
101 /// UsdStageCacheContext objects that exist in one thread's stack do not
102 /// influence calls to UsdStage::Open() from a different thread.
103 ///
105 {
106 public:
107  /// Bind a cache for calls to UsdStage::Open() to read from and write to.
108  explicit UsdStageCacheContext(UsdStageCache &cache)
109  : _rwCache(&cache)
110  , _isReadOnlyCache(false)
111  , _blockType(Usd_NoBlock) {}
112 
113  /// Bind a cache for calls to UsdStage::Open() to read from.
114  /// \see UsdUseButDoNotPopulateCache()
116  : _roCache(&holder.cache)
117  , _isReadOnlyCache(true)
118  , _blockType(Usd_NoBlock) {}
119 
120  /// Disable cache use completely (with UsdBlockStageCaches) or only
121  /// for writing (with UsdBlockStageCacheWrites).
123  : _blockType(blockType) {}
124 
125 private:
126  friend class UsdStage;
127 
128  static std::vector<const UsdStageCache *> _GetReadOnlyCaches();
129  static std::vector<const UsdStageCache *> _GetReadableCaches();
130  static std::vector<UsdStageCache *> _GetWritableCaches();
131 
132  // A blocking context is encoded with both members variables null.
133  union {
134  UsdStageCache *_rwCache;
135  const UsdStageCache *_roCache;
136  };
137  bool _isReadOnlyCache;
139 };
140 
141 
143 
144 #endif // PXR_USD_USD_STAGE_CACHE_CONTEXT_H
#define USD_API
Definition: api.h:23
TF_DEFINE_STACKED(UsdStageCacheContext, true, USD_API)
Usd_NonPopulatingStageCacheWrapper UsdUseButDoNotPopulateCache(StageCache &cache)
Usd_NonPopulatingStageCacheWrapper(const UsdStageCache &cache)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
UsdStageCacheContextBlockType
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74