Loading...
Searching...
No Matches
stageCache.h
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
21PXR_NAMESPACE_OPEN_SCOPE
22
23
24SDF_DECLARE_HANDLES(SdfLayer);
26
28
29class UsdStageCacheRequest;
30
68{
69public:
81 struct Id {
83 Id() : _value(-1) {}
84
87 static Id FromLongInt(long int val) { return Id(val); }
88
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
104 long int ToLongInt() const { return _value; }
105
107 std::string ToString() const {
108 return TfStringify(ToLongInt());
109 }
110
112 bool IsValid() const { return _value != -1; }
113
115 explicit operator bool() const { return IsValid(); }
116
117 private:
119 friend bool operator==(const Id &lhs, const Id &rhs) {
120 return lhs.ToLongInt() == rhs.ToLongInt();
121 }
123 friend bool operator!=(const Id &lhs, const Id &rhs) {
124 return !(lhs == rhs);
125 }
127 friend bool operator<(const Id &lhs, const Id &rhs) {
128 return lhs.ToLongInt() < rhs.ToLongInt();
129 }
131 friend bool operator<=(const Id &lhs, const Id &rhs) {
132 return !(rhs < lhs);
133 }
135 friend bool operator>(const Id &lhs, const Id &rhs) {
136 return rhs < lhs;
137 }
139 friend bool operator>=(const Id &lhs, const Id &rhs) {
140 return !(lhs < rhs);
141 }
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
153 USD_API
155
157 USD_API
159
161 USD_API
163
165 USD_API
167
169 USD_API
170 void swap(UsdStageCache &other);
171
173 USD_API
174 std::vector<UsdStageRefPtr> GetAllStages() const;
175
177 USD_API
178 size_t Size() const;
179
181 bool IsEmpty() const { return Size() == 0; }
182
206 USD_API
207 std::pair<UsdStageRefPtr, bool>
208 RequestStage(UsdStageCacheRequest &&request);
209
213 USD_API
214 UsdStageRefPtr Find(Id id) const;
215
220 USD_API
221 UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer) const;
222
227 USD_API
228 UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer,
229 const SdfLayerHandle &sessionLayer) const;
230
236 USD_API
237 UsdStageRefPtr FindOneMatching(
238 const SdfLayerHandle &rootLayer,
239 const ArResolverContext &pathResolverContext) const;
240
246 USD_API
247 UsdStageRefPtr FindOneMatching(
248 const SdfLayerHandle &rootLayer,
249 const SdfLayerHandle &sessionLayer,
250 const ArResolverContext &pathResolverContext) const;
251
254 USD_API
255 std::vector<UsdStageRefPtr>
256 FindAllMatching(const SdfLayerHandle &rootLayer) const;
257
260 USD_API
261 std::vector<UsdStageRefPtr>
262 FindAllMatching(const SdfLayerHandle &rootLayer,
263 const SdfLayerHandle &sessionLayer) const;
264
268 USD_API
269 std::vector<UsdStageRefPtr>
270 FindAllMatching(const SdfLayerHandle &rootLayer,
271 const ArResolverContext &pathResolverContext) const;
272
277 USD_API
278 std::vector<UsdStageRefPtr>
279 FindAllMatching(const SdfLayerHandle &rootLayer,
280 const SdfLayerHandle &sessionLayer,
281 const ArResolverContext &pathResolverContext) const;
282
285 USD_API
286 Id GetId(const UsdStageRefPtr &stage) const;
287
289 bool Contains(const UsdStageRefPtr &stage) const {
290 return static_cast<bool>(GetId(stage));
291 }
292
294 bool Contains(Id id) const { return Find(id); }
295
299 USD_API
300 Id Insert(const UsdStageRefPtr &stage);
301
307 USD_API
308 bool Erase(Id id);
309
314 USD_API
315 bool Erase(const UsdStageRefPtr &stage);
316
321 USD_API
322 size_t EraseAll(const SdfLayerHandle &rootLayer);
323
328 USD_API
329 size_t EraseAll(const SdfLayerHandle &rootLayer,
330 const SdfLayerHandle &sessionLayer);
331
337 USD_API
338 size_t EraseAll(const SdfLayerHandle &rootLayer,
339 const SdfLayerHandle &sessionLayer,
340 const ArResolverContext &pathResolverContext);
341
346 USD_API
347 void Clear();
348
352 USD_API
353 void SetDebugName(const std::string &debugName);
354
357 USD_API
358 std::string GetDebugName() const;
359
360private:
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
370class UsdStageCacheRequest
371{
372public:
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
387private:
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
399PXR_NAMESPACE_CLOSE_SCOPE
400
401#endif // PXR_USD_USD_STAGE_CACHE_H
An asset resolver context allows clients to provide additional data to the resolver for use during re...
A scene description container that can combine with other such containers to form simple component as...
Definition: layer.h:84
A strongly concurrency safe collection of UsdStageRefPtr s, enabling sharing across multiple clients ...
Definition: stageCache.h:68
USD_API UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer, const ArResolverContext &pathResolverContext) const
Find a stage in this cache with rootLayer and pathResolverContext.
USD_API std::pair< UsdStageRefPtr, bool > RequestStage(UsdStageCacheRequest &&request)
Find an existing stage in the cache that satisfies request, or invoke request.Manufacture() to create...
USD_API ~UsdStageCache()
Destructor.
USD_API size_t EraseAll(const SdfLayerHandle &rootLayer)
Erase all stages present in the cache with rootLayer and return the number erased.
bool Contains(const UsdStageRefPtr &stage) const
Return true if stage is present in this cache, false otherwise.
Definition: stageCache.h:289
bool Contains(Id id) const
Return true if id is present in this cache, false otherwise.
Definition: stageCache.h:294
USD_API size_t Size() const
Return the number of stages present in this cache.
USD_API Id GetId(const UsdStageRefPtr &stage) const
Return the Id associated with stage in this cache.
USD_API UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer) const
Find a stage in this cache with rootLayer.
USD_API std::vector< UsdStageRefPtr > FindAllMatching(const SdfLayerHandle &rootLayer, const ArResolverContext &pathResolverContext) const
Find all stages in this cache with rootLayer and pathResolverContext.
USD_API UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer, const SdfLayerHandle &sessionLayer, const ArResolverContext &pathResolverContext) const
Find a stage in this cache with rootLayer, sessionLayer, and pathResolverContext.
USD_API void swap(UsdStageCache &other)
Swap the contents of this cache with other.
USD_API UsdStageCache & operator=(const UsdStageCache &other)
Replace the contents of this cache with a copy of other.
USD_API std::vector< UsdStageRefPtr > FindAllMatching(const SdfLayerHandle &rootLayer, const SdfLayerHandle &sessionLayer, const ArResolverContext &pathResolverContext) const
Find all stages in this cache with rootLayer, sessionLayer, and pathResolverContext.
USD_API UsdStageCache(const UsdStageCache &other)
Construct a new cache as a copy of other.
USD_API std::string GetDebugName() const
Retrieve this cache's debug name, set with SetDebugName().
bool IsEmpty() const
Return true if this cache holds no stages, false otherwise.
Definition: stageCache.h:181
USD_API bool Erase(Id id)
Erase the stage identified by id from this cache and return true.
USD_API bool Erase(const UsdStageRefPtr &stage)
Erase stage from this cache and return true.
USD_API UsdStageRefPtr Find(Id id) const
Find the stage in this cache corresponding to id in this cache.
USD_API UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer, const SdfLayerHandle &sessionLayer) const
Find a stage in this cache with rootLayer and sessionLayer.
USD_API Id Insert(const UsdStageRefPtr &stage)
Insert stage into this cache and return its associated Id.
USD_API UsdStageCache()
Default construct an empty cache.
USD_API std::vector< UsdStageRefPtr > GetAllStages() const
Return a vector containing the stages present in this cache.
USD_API void SetDebugName(const std::string &debugName)
Assign a debug name to this cache.
USD_API size_t EraseAll(const SdfLayerHandle &rootLayer, const SdfLayerHandle &sessionLayer, const ArResolverContext &pathResolverContext)
Erase all stages present in the cache with rootLayer, sessionLayer, and pathResolverContext and retur...
USD_API std::vector< UsdStageRefPtr > FindAllMatching(const SdfLayerHandle &rootLayer, const SdfLayerHandle &sessionLayer) const
Find all stages in this cache with rootLayer and sessionLayer.
USD_API size_t EraseAll(const SdfLayerHandle &rootLayer, const SdfLayerHandle &sessionLayer)
Erase all stages present in the cache with rootLayer and sessionLayer and return the number erased.
USD_API void Clear()
Remove all entries from this cache, leaving it empty and equivalent to a default-constructed cache.
USD_API std::vector< UsdStageRefPtr > FindAllMatching(const SdfLayerHandle &rootLayer) const
Find all stages in this cache with rootLayer.
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition: stage.h:136
Standard pointer typedefs.
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:58
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:68
std::string TfStringify(const T &v)
Convert an arbitrary type into a string.
Definition: stringUtils.h:562
TF_API long TfStringToLong(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to a long int value.
Definitions of basic string utilities in tf.
A lightweight identifier that may be used to identify a particular cached stage within a UsdStageCach...
Definition: stageCache.h:81
std::string ToString() const
Convert this Id to a string representation.
Definition: stageCache.h:107
friend bool operator<=(const Id &lhs, const Id &rhs)
Less-than or equal comparison.
Definition: stageCache.h:131
friend size_t hash_value(Id id)
Hash.
Definition: stageCache.h:143
friend bool operator!=(const Id &lhs, const Id &rhs)
Inequality comparison.
Definition: stageCache.h:123
friend bool operator>(const Id &lhs, const Id &rhs)
Greater-than comparison.
Definition: stageCache.h:135
friend bool operator<(const Id &lhs, const Id &rhs)
Less-than comparison.
Definition: stageCache.h:127
static Id FromString(const std::string &s)
Create an Id from a string value.
Definition: stageCache.h:91
bool IsValid() const
Return true if this Id is valid.
Definition: stageCache.h:112
long int ToLongInt() const
Convert this Id to an integral representation.
Definition: stageCache.h:104
friend bool operator>=(const Id &lhs, const Id &rhs)
Greater-than or equal comparison.
Definition: stageCache.h:139
static Id FromLongInt(long int val)
Create an Id from an integral value.
Definition: stageCache.h:87
Id()
Default construct an invalid id.
Definition: stageCache.h:83
friend bool operator==(const Id &lhs, const Id &rhs)
Equality comparison.
Definition: stageCache.h:119