All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
31 
32 #include <boost/lexical_cast.hpp>
33 #include <boost/operators.hpp>
34 
35 #include <string>
36 #include <memory>
37 #include <mutex>
38 #include <vector>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 
43 SDF_DECLARE_HANDLES(SdfLayer);
45 
46 class ArResolverContext;
47 
48 class UsdStageCacheRequest;
49 
87 {
88 public:
100  struct Id : private boost::totally_ordered<Id> {
102  Id() : _value(-1) {}
103 
106  static Id FromLongInt(long int val) { return Id(val); }
107 
110  static Id FromString(const std::string &s) {
111  return FromLongInt(boost::lexical_cast<long int>(s));
112  }
113 
115  long int ToLongInt() const { return _value; }
116 
118  std::string ToString() const {
119  return boost::lexical_cast<std::string>(ToLongInt());
120  }
121 
123  bool IsValid() const { return _value != -1; }
124 
126  explicit operator bool() const { return IsValid(); }
127 
128  private:
130  friend bool operator==(const Id &lhs, const Id &rhs) {
131  return lhs.ToLongInt() == rhs.ToLongInt();
132  }
134  friend bool operator<(const Id &lhs, const Id &rhs) {
135  return lhs.ToLongInt() < rhs.ToLongInt();
136  }
138  friend size_t hash_value(Id id) {
139  return ~size_t(id.ToLongInt());
140  }
141 
142  explicit Id(long int val) : _value(val) {}
143 
144  long int _value;
145  };
146 
148  USD_API
149  UsdStageCache();
150 
152  USD_API
153  UsdStageCache(const UsdStageCache &other);
154 
156  USD_API
157  ~UsdStageCache();
158 
160  USD_API
161  UsdStageCache &operator=(const UsdStageCache &other);
162 
164  USD_API
165  void swap(UsdStageCache &other);
166 
168  USD_API
169  std::vector<UsdStageRefPtr> GetAllStages() const;
170 
172  USD_API
173  size_t Size() const;
174 
176  bool IsEmpty() const { return Size() == 0; }
177 
201  USD_API
202  std::pair<UsdStageRefPtr, bool>
203  RequestStage(UsdStageCacheRequest &&request);
204 
208  USD_API
209  UsdStageRefPtr Find(Id id) const;
210 
215  USD_API
216  UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer) const;
217 
222  USD_API
223  UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer,
224  const SdfLayerHandle &sessionLayer) const;
225 
231  USD_API
232  UsdStageRefPtr FindOneMatching(
233  const SdfLayerHandle &rootLayer,
234  const ArResolverContext &pathResolverContext) const;
235 
241  USD_API
242  UsdStageRefPtr FindOneMatching(
243  const SdfLayerHandle &rootLayer,
244  const SdfLayerHandle &sessionLayer,
245  const ArResolverContext &pathResolverContext) const;
246 
249  USD_API
250  std::vector<UsdStageRefPtr>
251  FindAllMatching(const SdfLayerHandle &rootLayer) const;
252 
255  USD_API
256  std::vector<UsdStageRefPtr>
257  FindAllMatching(const SdfLayerHandle &rootLayer,
258  const SdfLayerHandle &sessionLayer) const;
259 
263  USD_API
264  std::vector<UsdStageRefPtr>
265  FindAllMatching(const SdfLayerHandle &rootLayer,
266  const ArResolverContext &pathResolverContext) const;
267 
272  USD_API
273  std::vector<UsdStageRefPtr>
274  FindAllMatching(const SdfLayerHandle &rootLayer,
275  const SdfLayerHandle &sessionLayer,
276  const ArResolverContext &pathResolverContext) const;
277 
280  USD_API
281  Id GetId(const UsdStageRefPtr &stage) const;
282 
284  bool Contains(const UsdStageRefPtr &stage) const {
285  return static_cast<bool>(GetId(stage));
286  }
287 
289  bool Contains(Id id) const { return Find(id); }
290 
294  USD_API
295  Id Insert(const UsdStageRefPtr &stage);
296 
302  USD_API
303  bool Erase(Id id);
304 
309  USD_API
310  bool Erase(const UsdStageRefPtr &stage);
311 
316  USD_API
317  size_t EraseAll(const SdfLayerHandle &rootLayer);
318 
323  USD_API
324  size_t EraseAll(const SdfLayerHandle &rootLayer,
325  const SdfLayerHandle &sessionLayer);
326 
332  USD_API
333  size_t EraseAll(const SdfLayerHandle &rootLayer,
334  const SdfLayerHandle &sessionLayer,
335  const ArResolverContext &pathResolverContext);
336 
341  USD_API
342  void Clear();
343 
347  USD_API
348  void SetDebugName(const std::string &debugName);
349 
352  USD_API
353  std::string GetDebugName() const;
354 
355 private:
356  friend void swap(UsdStageCache &lhs, UsdStageCache &rhs) {
357  lhs.swap(rhs);
358  }
359 
360  typedef struct Usd_StageCacheImpl _Impl;
361  std::unique_ptr<_Impl> _impl;
362  mutable std::mutex _mutex;
363 };
364 
365 class UsdStageCacheRequest
366 {
367 public:
368  USD_API
369  virtual ~UsdStageCacheRequest();
370 
371  // Return true if the stage satisfies this request.
372  virtual bool IsSatisfiedBy(UsdStageRefPtr const &stage) const = 0;
373 
374  // Return true if the pending request will satisfy this request, once
375  // complete.
376  virtual bool IsSatisfiedBy(UsdStageCacheRequest const &pending) const = 0;
377 
378  // Invoked to manufacture a stage to insert in the cache. Postcondition:
379  // IsSatisfiedBy() must return true for the resulting stage.
380  virtual UsdStageRefPtr Manufacture() = 0;
381 
382 private:
383  friend class UsdStageCache;
384 
385  struct _Mailbox;
386  void _Subscribe(_Mailbox *);
387 
388  struct _Data;
389  struct _DataDeleter { void operator()(_Data *); };
390  std::unique_ptr<_Data, _DataDeleter> _data;
391 };
392 
393 
394 PXR_NAMESPACE_CLOSE_SCOPE
395 
396 #endif // PXR_USD_USD_STAGE_CACHE_H
static Id FromString(const std::string &s)
Create an Id from a string value.
Definition: stageCache.h:110
A unit of scene description that you combine with other units of scene description to form a shot...
Definition: layer.h:96
USD_API std::vector< UsdStageRefPtr > GetAllStages() const
Return a vector containing the stages present in this cache.
long int ToLongInt() const
Convert this Id to an integral representation.
Definition: stageCache.h:115
bool IsValid() const
Return true if this Id is valid.
Definition: stageCache.h:123
Standard pointer typedefs.
USD_API std::string GetDebugName() const
Retrieve this cache&#39;s debug name, set with SetDebugName().
USD_API void Clear()
Remove all entries from this cache, leaving it empty and equivalent to a default-constructed cache...
Id()
Default construct an invalid id.
Definition: stageCache.h:102
bool Contains(const UsdStageRefPtr &stage) const
Return true if stage is present in this cache, false otherwise.
Definition: stageCache.h:284
USD_API void SetDebugName(const std::string &debugName)
Assign a debug name to this cache.
USD_API std::vector< UsdStageRefPtr > FindAllMatching(const SdfLayerHandle &rootLayer) const
Find all stages in this cache with rootLayer.
friend size_t hash_value(Id id)
Hash.
Definition: stageCache.h:138
USD_API Id GetId(const UsdStageRefPtr &stage) const
Return the Id associated with stage in this cache.
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition: stage.h:145
USD_API UsdStageRefPtr FindOneMatching(const SdfLayerHandle &rootLayer) const
Find a stage in this cache with rootLayer.
A strongly concurrency safe collection of UsdStageRefPtr s, enabling sharing across multiple clients ...
Definition: stageCache.h:86
friend bool operator==(const Id &lhs, const Id &rhs)
Equality comparison.
Definition: stageCache.h:130
A lightweight identifier that may be used to identify a particular cached stage within a UsdStageCach...
Definition: stageCache.h:100
USD_API UsdStageCache()
Default construct an empty cache.
USD_API size_t EraseAll(const SdfLayerHandle &rootLayer)
Erase all stages present in the cache with rootLayer and return the number erased.
USD_API Id Insert(const UsdStageRefPtr &stage)
Insert stage into this cache and return its associated Id.
USD_API UsdStageCache & operator=(const UsdStageCache &other)
Replace the contents of this cache with a copy of other.
static Id FromLongInt(long int val)
Create an Id from an integral value.
Definition: stageCache.h:106
USD_API size_t Size() const
Return the number of stages present in this cache.
bool Contains(Id id) const
Return true if id is present in this cache, false otherwise.
Definition: stageCache.h:289
bool IsEmpty() const
Return true if this cache holds no stages, false otherwise.
Definition: stageCache.h:176
friend bool operator<(const Id &lhs, const Id &rhs)
Less-than comparison.
Definition: stageCache.h:134
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:75
An asset resolver context allows clients to provide additional data to the resolver for use during re...
USD_API UsdStageRefPtr Find(Id id) const
Find the stage in this cache corresponding to id in this cache.
USD_API ~UsdStageCache()
Destructor.
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...
std::string ToString() const
Convert this Id to a string representation.
Definition: stageCache.h:118
USD_API bool Erase(Id id)
Erase the stage identified by id from this cache and return true.
USD_API void swap(UsdStageCache &other)
Swap the contents of this cache with other.