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