Loading...
Searching...
No Matches
stageCache.h
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"
32
33#include <string>
34#include <memory>
35#include <mutex>
36#include <vector>
37
38PXR_NAMESPACE_OPEN_SCOPE
39
40
41SDF_DECLARE_HANDLES(SdfLayer);
43
45
46class UsdStageCacheRequest;
47
85{
86public:
98 struct Id {
100 Id() : _value(-1) {}
101
104 static Id FromLongInt(long int val) { return Id(val); }
105
108 static Id FromString(const std::string &s) {
109 bool overflow = false;
110 const long int result = TfStringToLong(s, &overflow);
111 if (overflow) {
113 "'%s' overflowed during conversion to int64_t.",
114 s.c_str()
115 );
116 }
117 return FromLongInt(result);
118 }
119
121 long int ToLongInt() const { return _value; }
122
124 std::string ToString() const {
125 return TfStringify(ToLongInt());
126 }
127
129 bool IsValid() const { return _value != -1; }
130
132 explicit operator bool() const { return IsValid(); }
133
134 private:
136 friend bool operator==(const Id &lhs, const Id &rhs) {
137 return lhs.ToLongInt() == rhs.ToLongInt();
138 }
140 friend bool operator!=(const Id &lhs, const Id &rhs) {
141 return !(lhs == rhs);
142 }
144 friend bool operator<(const Id &lhs, const Id &rhs) {
145 return lhs.ToLongInt() < rhs.ToLongInt();
146 }
148 friend bool operator<=(const Id &lhs, const Id &rhs) {
149 return !(rhs < lhs);
150 }
152 friend bool operator>(const Id &lhs, const Id &rhs) {
153 return rhs < lhs;
154 }
156 friend bool operator>=(const Id &lhs, const Id &rhs) {
157 return !(lhs < rhs);
158 }
160 friend size_t hash_value(Id id) {
161 return ~size_t(id.ToLongInt());
162 }
163
164 explicit Id(long int val) : _value(val) {}
165
166 long int _value;
167 };
168
170 USD_API
172
174 USD_API
176
178 USD_API
180
182 USD_API
184
186 USD_API
187 void swap(UsdStageCache &other);
188
190 USD_API
191 std::vector<UsdStageRefPtr> GetAllStages() const;
192
194 USD_API
195 size_t Size() const;
196
198 bool IsEmpty() const { return Size() == 0; }
199
223 USD_API
224 std::pair<UsdStageRefPtr, bool>
225 RequestStage(UsdStageCacheRequest &&request);
226
230 USD_API
231 UsdStageRefPtr Find(Id id) const;
232
237 USD_API
238 UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer) const;
239
244 USD_API
245 UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer,
246 const SdfLayerHandle &sessionLayer) const;
247
253 USD_API
254 UsdStageRefPtr FindOneMatching(
255 const SdfLayerHandle &rootLayer,
256 const ArResolverContext &pathResolverContext) const;
257
263 USD_API
264 UsdStageRefPtr FindOneMatching(
265 const SdfLayerHandle &rootLayer,
266 const SdfLayerHandle &sessionLayer,
267 const ArResolverContext &pathResolverContext) const;
268
271 USD_API
272 std::vector<UsdStageRefPtr>
273 FindAllMatching(const SdfLayerHandle &rootLayer) const;
274
277 USD_API
278 std::vector<UsdStageRefPtr>
279 FindAllMatching(const SdfLayerHandle &rootLayer,
280 const SdfLayerHandle &sessionLayer) const;
281
285 USD_API
286 std::vector<UsdStageRefPtr>
287 FindAllMatching(const SdfLayerHandle &rootLayer,
288 const ArResolverContext &pathResolverContext) const;
289
294 USD_API
295 std::vector<UsdStageRefPtr>
296 FindAllMatching(const SdfLayerHandle &rootLayer,
297 const SdfLayerHandle &sessionLayer,
298 const ArResolverContext &pathResolverContext) const;
299
302 USD_API
303 Id GetId(const UsdStageRefPtr &stage) const;
304
306 bool Contains(const UsdStageRefPtr &stage) const {
307 return static_cast<bool>(GetId(stage));
308 }
309
311 bool Contains(Id id) const { return Find(id); }
312
316 USD_API
317 Id Insert(const UsdStageRefPtr &stage);
318
324 USD_API
325 bool Erase(Id id);
326
331 USD_API
332 bool Erase(const UsdStageRefPtr &stage);
333
338 USD_API
339 size_t EraseAll(const SdfLayerHandle &rootLayer);
340
345 USD_API
346 size_t EraseAll(const SdfLayerHandle &rootLayer,
347 const SdfLayerHandle &sessionLayer);
348
354 USD_API
355 size_t EraseAll(const SdfLayerHandle &rootLayer,
356 const SdfLayerHandle &sessionLayer,
357 const ArResolverContext &pathResolverContext);
358
363 USD_API
364 void Clear();
365
369 USD_API
370 void SetDebugName(const std::string &debugName);
371
374 USD_API
375 std::string GetDebugName() const;
376
377private:
378 friend void swap(UsdStageCache &lhs, UsdStageCache &rhs) {
379 lhs.swap(rhs);
380 }
381
382 typedef struct Usd_StageCacheImpl _Impl;
383 std::unique_ptr<_Impl> _impl;
384 mutable std::mutex _mutex;
385};
386
387class UsdStageCacheRequest
388{
389public:
390 USD_API
391 virtual ~UsdStageCacheRequest();
392
393 // Return true if the stage satisfies this request.
394 virtual bool IsSatisfiedBy(UsdStageRefPtr const &stage) const = 0;
395
396 // Return true if the pending request will satisfy this request, once
397 // complete.
398 virtual bool IsSatisfiedBy(UsdStageCacheRequest const &pending) const = 0;
399
400 // Invoked to manufacture a stage to insert in the cache. Postcondition:
401 // IsSatisfiedBy() must return true for the resulting stage.
402 virtual UsdStageRefPtr Manufacture() = 0;
403
404private:
405 friend class UsdStageCache;
406
407 struct _Mailbox;
408 void _Subscribe(_Mailbox *);
409
410 struct _Data;
411 struct _DataDeleter { void operator()(_Data *); };
412 std::unique_ptr<_Data, _DataDeleter> _data;
413};
414
415
416PXR_NAMESPACE_CLOSE_SCOPE
417
418#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:100
A strongly concurrency safe collection of UsdStageRefPtr s, enabling sharing across multiple clients ...
Definition: stageCache.h:85
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:306
bool Contains(Id id) const
Return true if id is present in this cache, false otherwise.
Definition: stageCache.h:311
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:198
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:151
Standard pointer typedefs.
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:75
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:85
std::string TfStringify(const T &v)
Convert an arbitrary type into a string.
Definition: stringUtils.h:572
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:98
std::string ToString() const
Convert this Id to a string representation.
Definition: stageCache.h:124
friend bool operator<=(const Id &lhs, const Id &rhs)
Less-than or equal comparison.
Definition: stageCache.h:148
friend size_t hash_value(Id id)
Hash.
Definition: stageCache.h:160
friend bool operator!=(const Id &lhs, const Id &rhs)
Inequality comparison.
Definition: stageCache.h:140
friend bool operator>(const Id &lhs, const Id &rhs)
Greater-than comparison.
Definition: stageCache.h:152
friend bool operator<(const Id &lhs, const Id &rhs)
Less-than comparison.
Definition: stageCache.h:144
static Id FromString(const std::string &s)
Create an Id from a string value.
Definition: stageCache.h:108
bool IsValid() const
Return true if this Id is valid.
Definition: stageCache.h:129
long int ToLongInt() const
Convert this Id to an integral representation.
Definition: stageCache.h:121
friend bool operator>=(const Id &lhs, const Id &rhs)
Greater-than or equal comparison.
Definition: stageCache.h:156
static Id FromLongInt(long int val)
Create an Id from an integral value.
Definition: stageCache.h:104
Id()
Default construct an invalid id.
Definition: stageCache.h:100
friend bool operator==(const Id &lhs, const Id &rhs)
Equality comparison.
Definition: stageCache.h:136