All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
iterator.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_PCP_ITERATOR_H
8#define PXR_USD_PCP_ITERATOR_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/pcp/api.h"
12#include "pxr/usd/pcp/node.h"
13
16#include "pxr/usd/sdf/site.h"
17
19
20#include <iterator>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24class PcpPrimIndex;
25class PcpPrimIndex_Graph;
27
34{
35 class _PtrProxy {
36 public:
37 PcpNodeRef* operator->() { return &_nodeRef; }
38 private:
39 friend class PcpNodeIterator;
40 explicit _PtrProxy(const PcpNodeRef& nodeRef) : _nodeRef(nodeRef) {}
41 PcpNodeRef _nodeRef;
42 };
43public:
44 using iterator_category = std::random_access_iterator_tag;
45 using value_type = PcpNodeRef;
46 using reference = PcpNodeRef;
47 using pointer = _PtrProxy;
48 using difference_type = std::ptrdiff_t;
49
51 PcpNodeIterator() = default;
52
53 // Returns a compressed Sd site. For internal use only.
54 Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
55 {
56 return Pcp_CompressedSdSite(_nodeIdx, layerIndex);
57 }
58
59 reference operator*() const { return dereference(); }
60 pointer operator->() const { return pointer(dereference()); }
61 reference operator[](const difference_type index) const {
62 PcpNodeIterator advanced(*this);
63 advanced.advance(index);
64 return advanced.dereference();
65 }
66
67 difference_type operator-(const PcpNodeIterator& other) const {
68 return -distance_to(other);
69 }
70
71 PcpNodeIterator& operator++() {
72 increment();
73 return *this;
74 }
75
76 PcpNodeIterator& operator--() {
77 decrement();
78 return *this;
79 }
80
81 PcpNodeIterator operator++(int) {
82 PcpNodeIterator result(*this);
83 increment();
84 return result;
85 }
86
87 PcpNodeIterator operator--(int) {
88 PcpNodeIterator result(*this);
89 decrement();
90 return result;
91 }
92
93 PcpNodeIterator operator+(const difference_type increment) const {
94 PcpNodeIterator result(*this);
95 result.advance(increment);
96 return result;
97 }
98
99 PcpNodeIterator operator-(const difference_type decrement) const {
100 PcpNodeIterator result(*this);
101 result.advance(-decrement);
102 return result;
103 }
104
105 PcpNodeIterator& operator+=(const difference_type increment) {
106 advance(increment);
107 return *this;
108 }
109
110 PcpNodeIterator& operator-=(const difference_type decrement) {
111 advance(-decrement);
112 return *this;
113 }
114
115 bool operator==(const PcpNodeIterator& other) const {
116 return equal(other);
117 }
118
119 bool operator!=(const PcpNodeIterator& other) const {
120 return !equal(other);
121 }
122
123 bool operator<(const PcpNodeIterator& other) const {
124 TF_DEV_AXIOM(_graph == other._graph);
125 return _nodeIdx < other._nodeIdx;
126 }
127
128 bool operator<=(const PcpNodeIterator& other) const {
129 TF_DEV_AXIOM(_graph == other._graph);
130 return _nodeIdx <= other._nodeIdx;
131 }
132
133 bool operator>(const PcpNodeIterator& other) const {
134 TF_DEV_AXIOM(_graph == other._graph);
135 return _nodeIdx > other._nodeIdx;
136 }
137
138 bool operator>=(const PcpNodeIterator& other) const {
139 TF_DEV_AXIOM(_graph == other._graph);
140 return _nodeIdx >= other._nodeIdx;
141 }
142
143private:
144 friend class PcpPrimIndex;
145 PcpNodeIterator(PcpPrimIndex_Graph* graph, size_t nodeIdx) :
146 _graph(graph), _nodeIdx(nodeIdx) {}
147
148 void increment() { ++_nodeIdx; }
149 void decrement() { --_nodeIdx; }
150 void advance(difference_type n) { _nodeIdx += n; }
151 difference_type distance_to(const PcpNodeIterator& other) const {
152 return (difference_type)(other._nodeIdx) - _nodeIdx;
153 }
154 bool equal(const PcpNodeIterator& other) const {
155 return (_graph == other._graph) & (_nodeIdx == other._nodeIdx);
156 }
157 reference dereference() const {
158 return PcpNodeRef(_graph, _nodeIdx);
159 }
160
161private:
162 PcpPrimIndex_Graph* _graph = nullptr;
163 size_t _nodeIdx = PCP_INVALID_INDEX;
164};
165
172 : public Tf_ProxyReferenceReverseIterator<PcpNodeIterator>
173{
174public:
176 explicit PcpNodeReverseIterator(const PcpNodeIterator& iter)
177 : Tf_ProxyReferenceReverseIterator<PcpNodeIterator>(iter) {}
178};
179
186{
187 class _PtrProxy {
188 public:
189 SdfSite* operator->() { return &_site; }
190 private:
191 friend class PcpPrimIterator;
192 explicit _PtrProxy(const SdfSite& site) : _site(site) {}
193 SdfSite _site;
194 };
195public:
196 using iterator_category = std::random_access_iterator_tag;
197 using value_type = SdfSite;
198 using reference = SdfSite;
199 using pointer = _PtrProxy;
200 using difference_type = std::ptrdiff_t;
201
203 PCP_API
205
208 PCP_API
209 PcpPrimIterator(const PcpPrimIndex* primIndex, size_t pos);
210
212 PCP_API
214
215 // Returns the \c Pcp_SdSiteRef from which the current prim originated.
216 // For internal use only.
217 PCP_API
218 Pcp_SdSiteRef _GetSiteRef() const;
219
220 reference operator*() const { return dereference(); }
221 pointer operator->() const { return pointer(dereference()); }
222 reference operator[](const difference_type index) const {
223 PcpPrimIterator advanced(*this);
224 advanced.advance(index);
225 return advanced.dereference();
226 }
227
228 difference_type operator-(const PcpPrimIterator& other) const {
229 return -distance_to(other);
230 }
231
232 PcpPrimIterator& operator++() {
233 increment();
234 return *this;
235 }
236
237 PcpPrimIterator& operator--() {
238 decrement();
239 return *this;
240 }
241
242 PcpPrimIterator operator++(int) {
243 PcpPrimIterator result(*this);
244 increment();
245 return result;
246 }
247
248 PcpPrimIterator operator--(int) {
249 PcpPrimIterator result(*this);
250 decrement();
251 return result;
252 }
253
254 PcpPrimIterator operator+(const difference_type increment) const {
255 PcpPrimIterator result(*this);
256 result.advance(increment);
257 return result;
258 }
259
260 PcpPrimIterator operator-(const difference_type decrement) const {
261 PcpPrimIterator result(*this);
262 result.advance(-decrement);
263 return result;
264 }
265
266 PcpPrimIterator& operator+=(const difference_type increment) {
267 advance(increment);
268 return *this;
269 }
270
271 PcpPrimIterator& operator-=(const difference_type decrement) {
272 advance(-decrement);
273 return *this;
274 }
275
276 bool operator==(const PcpPrimIterator& other) const {
277 return equal(other);
278 }
279
280 bool operator!=(const PcpPrimIterator& other) const {
281 return !equal(other);
282 }
283
284 bool operator<(const PcpPrimIterator& other) const {
285 TF_DEV_AXIOM(_primIndex == other._primIndex);
286 return _pos < other._pos;
287 }
288
289 bool operator<=(const PcpPrimIterator& other) const {
290 TF_DEV_AXIOM(_primIndex == other._primIndex);
291 return _pos <= other._pos;
292 }
293
294 bool operator>(const PcpPrimIterator& other) const {
295 TF_DEV_AXIOM(_primIndex == other._primIndex);
296 return _pos > other._pos;
297 }
298
299 bool operator>=(const PcpPrimIterator& other) const {
300 TF_DEV_AXIOM(_primIndex == other._primIndex);
301 return _pos >= other._pos;
302 }
303
304private:
305 PCP_API
306 void increment();
307 PCP_API
308 void decrement();
309 PCP_API
310 void advance(difference_type n);
311 PCP_API
312 difference_type distance_to(const PcpPrimIterator& other) const;
313 PCP_API
314 bool equal(const PcpPrimIterator& other) const;
315 PCP_API
316 reference dereference() const;
317
318private:
319 const PcpPrimIndex* _primIndex = nullptr;
320 size_t _pos = PCP_INVALID_INDEX;
321};
322
329 : public Tf_ProxyReferenceReverseIterator<PcpPrimIterator>
330{
331public:
333 explicit PcpPrimReverseIterator(const PcpPrimIterator& iter)
334 : Tf_ProxyReferenceReverseIterator<PcpPrimIterator>(iter) { }
335
336 PcpNodeRef GetNode() const
337 {
338 PcpPrimIterator tmp = base();
339 return (--tmp).GetNode();
340 }
341
342 Pcp_SdSiteRef _GetSiteRef() const
343 {
344 PcpPrimIterator tmp = base();
345 return (--tmp)._GetSiteRef();
346 }
347};
348
355{
356public:
357 using iterator_category = std::random_access_iterator_tag;
358 using value_type = const SdfPropertySpecHandle;
359 using reference = const SdfPropertySpecHandle&;
360 using pointer = const SdfPropertySpecHandle*;
361 using difference_type = std::ptrdiff_t;
362
364 PCP_API
366
369 PCP_API
370 PcpPropertyIterator(const PcpPropertyIndex& index, size_t pos = 0);
371
373 PCP_API
375
378 PCP_API
379 bool IsLocal() const;
380
381 reference operator*() const { return dereference(); }
382 pointer operator->() const { return &(dereference()); }
383 reference operator[](const difference_type index) const {
384 PcpPropertyIterator advanced(*this);
385 advanced.advance(index);
386 return advanced.dereference();
387 }
388
389 difference_type operator-(const PcpPropertyIterator& other) const {
390 return -distance_to(other);
391 }
392
393 PcpPropertyIterator& operator++() {
394 increment();
395 return *this;
396 }
397
398 PcpPropertyIterator& operator--() {
399 decrement();
400 return *this;
401 }
402
403 PcpPropertyIterator operator++(int) {
404 PcpPropertyIterator result(*this);
405 increment();
406 return result;
407 }
408
409 PcpPropertyIterator operator--(int) {
410 PcpPropertyIterator result(*this);
411 decrement();
412 return result;
413 }
414
415 PcpPropertyIterator operator+(const difference_type increment) const {
416 PcpPropertyIterator result(*this);
417 result.advance(increment);
418 return result;
419 }
420
421 PcpPropertyIterator operator-(const difference_type decrement) const {
422 PcpPropertyIterator result(*this);
423 result.advance(-decrement);
424 return result;
425 }
426
427 PcpPropertyIterator& operator+=(const difference_type increment) {
428 advance(increment);
429 return *this;
430 }
431
432 PcpPropertyIterator& operator-=(const difference_type decrement) {
433 advance(-decrement);
434 return *this;
435 }
436
437 bool operator==(const PcpPropertyIterator& other) const {
438 return equal(other);
439 }
440
441 bool operator!=(const PcpPropertyIterator& other) const {
442 return !equal(other);
443 }
444
445 bool operator<(const PcpPropertyIterator& other) const {
446 TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
447 return _pos < other._pos;
448 }
449
450 bool operator<=(const PcpPropertyIterator& other) const {
451 TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
452 return _pos <= other._pos;
453 }
454
455 bool operator>(const PcpPropertyIterator& other) const {
456 TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
457 return _pos > other._pos;
458 }
459
460 bool operator>=(const PcpPropertyIterator& other) const {
461 TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
462 return _pos >= other._pos;
463 }
464
465private:
466 PCP_API
467 void increment();
468 PCP_API
469 void decrement();
470 PCP_API
471 void advance(difference_type n);
472 PCP_API
473 difference_type distance_to(const PcpPropertyIterator& other) const;
474 PCP_API
475 bool equal(const PcpPropertyIterator& other) const;
476 PCP_API
477 reference dereference() const;
478
479private:
480 const PcpPropertyIndex* _propertyIndex = nullptr;
481 size_t _pos = 0;
482};
483
490 : public std::reverse_iterator<PcpPropertyIterator>
491{
492public:
495 : std::reverse_iterator<PcpPropertyIterator>(iter) { }
496
497 PcpNodeRef GetNode() const
498 {
499 PcpPropertyIterator tmp = base();
500 return (--tmp).GetNode();
501 }
502
503 bool IsLocal() const
504 {
505 PcpPropertyIterator tmp = base();
506 return (--tmp).IsLocal();
507 }
508};
509
510// Helper macro for defining iterator ranges, which are simply pairs of
511// iterators denoting the [start, end) of a series of values. These ranges
512// may be used with TF_FOR_ALL and TF_REVERSE_FOR_ALL.
513#define PCP_DEFINE_RANGE(Range, Iterator, ReverseIterator) \
514 typedef std::pair<Iterator, Iterator> Range; \
515 \
516 inline Iterator begin(Range &range) { return range.first; } \
517 inline Iterator begin(const Range &range) { return range.first; } \
518 inline Iterator end(Range &range) { return range.second; } \
519 inline Iterator end(const Range &range) { return range.second; } \
520 \
521 template <> \
522 struct Tf_IteratorInterface<Range, false> { \
523 typedef Iterator IteratorType; \
524 static IteratorType Begin(Range &c) { return c.first; } \
525 static IteratorType End(Range &c) { return c.second; } \
526 }; \
527 \
528 template <> \
529 struct Tf_IteratorInterface<const Range, false> { \
530 typedef Iterator IteratorType; \
531 static IteratorType Begin(Range const &c) { return c.first; } \
532 static IteratorType End(Range const &c) { return c.second; } \
533 }; \
534 \
535 template <> \
536 struct Tf_IteratorInterface<Range, true> { \
537 typedef ReverseIterator IteratorType; \
538 static IteratorType Begin(Range &c) \
539 { return IteratorType(c.second); } \
540 static IteratorType End(Range &c) \
541 { return IteratorType(c.first); } \
542 }; \
543 \
544 template <> \
545 struct Tf_IteratorInterface<const Range, true> { \
546 typedef ReverseIterator IteratorType; \
547 static IteratorType Begin(Range const &c) \
548 { return IteratorType(c.second); } \
549 static IteratorType End(Range const &c) \
550 { return IteratorType(c.first); } \
551 }; \
552 \
553 template <> \
554 struct Tf_ShouldIterateOverCopy<Range> : std::true_type {}; \
555 \
556 template <> \
557 struct Tf_ShouldIterateOverCopy<const Range> : std::true_type {}
558
559PCP_DEFINE_RANGE(PcpNodeRange, PcpNodeIterator, PcpNodeReverseIterator);
560PCP_DEFINE_RANGE(PcpPrimRange, PcpPrimIterator, PcpPrimReverseIterator);
561PCP_DEFINE_RANGE(PcpPropertyRange, PcpPropertyIterator,
563
569template <class Iterator> struct PcpIteratorTraits;
570
571template <>
573{
574 typedef PcpNodeRange RangeType;
575 typedef PcpNodeReverseIterator ReverseIteratorType;
576};
577
578template <>
580{
581 typedef PcpPrimRange RangeType;
582 typedef PcpPrimReverseIterator ReverseIteratorType;
583};
584
585template <>
587{
588 typedef PcpPropertyRange RangeType;
589 typedef PcpPropertyReverseIterator ReverseIteratorType;
590};
591
592PXR_NAMESPACE_CLOSE_SCOPE
593
594#endif // PXR_USD_PCP_ITERATOR_H
A simple iterator adapter for STL containers.
Object used to iterate over nodes in the prim index graph in strong-to-weak order.
Definition: iterator.h:34
PcpNodeIterator()=default
Constructs an invalid iterator.
PcpNode represents a node in an expression tree for compositing scene description.
Definition: node.h:47
Object used to iterate over nodes in the prim index graph in weak-to-strong order.
Definition: iterator.h:173
PcpPrimIndex is an index of the all sites of scene description that contribute opinions to a specific...
Definition: primIndex.h:62
Object used to iterate over prim specs in the prim index graph in strong-to-weak order.
Definition: iterator.h:186
PCP_API PcpPrimIterator()
Constructs an invalid iterator.
PCP_API PcpPrimIterator(const PcpPrimIndex *primIndex, size_t pos)
Constructs a prim iterator beginning at position pos in the prim stack of prim index primIndex.
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current prim originated.
Object used to iterate over prim specs in the prim index graph in weak-to-strong order.
Definition: iterator.h:330
PcpPropertyIndex is an index of all sites in scene description that contribute opinions to a specific...
Definition: propertyIndex.h:49
Object used to iterate over property specs in a property index in strong-to-weak order.
Definition: iterator.h:355
PCP_API PcpPropertyIterator(const PcpPropertyIndex &index, size_t pos=0)
Constructs a property iterator for index beginning at position pos in the property stack.
PCP_API PcpPropertyIterator()
Constructs an invalid iterator.
PCP_API bool IsLocal() const
Returns true if the current property is local to the owning property index's layer stack,...
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current property originated.
Object used to iterate over property specs in a property index in weak-to-strong order.
Definition: iterator.h:491
An SdfSite is a simple representation of a location in a layer where opinions may possibly be found.
Definition: site.h:26
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:205
Traits class for retrieving useful characteristics about one of the Pcp iterator types above.
Definition: iterator.h:569
constexpr size_t PCP_INVALID_INDEX
A value which indicates an invalid index.
Definition: types.h:198