HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
iterator.h
Go to the documentation of this file.
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 
31 #include "pxr/usd/sdf/primSpec.h"
33 #include "pxr/usd/sdf/site.h"
34 
35 #include "pxr/base/tf/iterator.h"
36 
37 #include <hboost/iterator/iterator_facade.hpp>
38 #include <hboost/iterator/reverse_iterator.hpp>
39 #include <iterator>
40 
42 
43 class PcpPrimIndex;
44 class PcpPrimIndex_Graph;
45 class PcpPropertyIndex;
46 
47 /// \class PcpNodeIterator
48 ///
49 /// Object used to iterate over nodes in the prim index graph in strong-to-weak
50 /// order.
51 ///
53  : public hboost::iterator_facade<
54  /* Derived = */ PcpNodeIterator,
55  /* ValueType = */ PcpNodeRef,
56  /* Category = */ hboost::random_access_traversal_tag,
57  /* RefType = */ PcpNodeRef
58  >
59 {
60 public:
61  /// Constructs an invalid iterator.
62  PcpNodeIterator() : _graph(0), _nodeIdx(PCP_INVALID_INDEX) {}
63 
64  // Returns a compressed Sd site. For internal use only.
65  Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
66  {
67  return Pcp_CompressedSdSite(_nodeIdx, layerIndex);
68  }
69 
70 private:
71  friend class PcpPrimIndex;
72  PcpNodeIterator(PcpPrimIndex_Graph* graph, size_t nodeIdx) :
73  _graph(graph), _nodeIdx(nodeIdx) {}
74 
76 
77  void increment() { ++_nodeIdx; }
78  void decrement() { --_nodeIdx; }
79  void advance(difference_type n) { _nodeIdx += n; }
80  difference_type distance_to(const PcpNodeIterator& other) const {
81  return (difference_type)(other._nodeIdx) - _nodeIdx;
82  }
83  bool equal(const PcpNodeIterator& other) const {
84  return (_graph == other._graph) & (_nodeIdx == other._nodeIdx);
85  }
86  reference dereference() const {
87  return PcpNodeRef(_graph, _nodeIdx);
88  }
89 
90 private:
91  PcpPrimIndex_Graph* _graph;
92  size_t _nodeIdx;
93 };
94 
95 /// \class PcpNodeReverseIterator
96 ///
97 /// Object used to iterate over nodes in the prim index graph in weak-to-strong
98 /// order.
99 ///
101  : public hboost::reverse_iterator<PcpNodeIterator>
102 {
103 public:
106  : hboost::reverse_iterator<PcpNodeIterator>(iter) { }
107 };
108 
109 /// \class PcpPrimIterator
110 ///
111 /// Object used to iterate over prim specs in the prim index graph in
112 /// strong-to-weak order.
113 ///
115  : public hboost::iterator_facade<
116  /* Derived = */ PcpPrimIterator,
117  /* Value = */ SdfSite,
118  /* Category = */ hboost::random_access_traversal_tag,
119  /* Ref = */ SdfSite
120  >
121 {
122 public:
123  /// Constructs an invalid iterator.
124  PCP_API
125  PcpPrimIterator();
126 
127  /// Constructs a prim iterator beginning at position \p pos in the
128  /// prim stack of prim index \p primIndex.
129  PCP_API
130  PcpPrimIterator(const PcpPrimIndex* primIndex, size_t pos);
131 
132  /// Returns the PcpNode from which the current prim originated.
133  PCP_API
134  PcpNodeRef GetNode() const;
135 
136  // Returns the \c Pcp_SdSiteRef from which the current prim originated.
137  // For internal use only.
138  PCP_API
139  Pcp_SdSiteRef _GetSiteRef() const;
140 
141 private:
143  PCP_API
144  void increment();
145  PCP_API
146  void decrement();
147  PCP_API
148  void advance(difference_type n);
149  PCP_API
150  difference_type distance_to(const PcpPrimIterator& other) const;
151  PCP_API
152  bool equal(const PcpPrimIterator& other) const;
153  PCP_API
154  reference dereference() const;
155 
156 private:
157  const PcpPrimIndex* _primIndex;
158  size_t _pos;
159 };
160 
161 /// \class PcpPrimReverseIterator
162 ///
163 /// Object used to iterate over prim specs in the prim index graph in
164 /// weak-to-strong order.
165 ///
167  : public hboost::reverse_iterator<PcpPrimIterator>
168 {
169 public:
172  : hboost::reverse_iterator<PcpPrimIterator>(iter) { }
173 
175  {
176  PcpPrimIterator tmp = base();
177  return (--tmp).GetNode();
178  }
179 
181  {
182  PcpPrimIterator tmp = base();
183  return (--tmp)._GetSiteRef();
184  }
185 };
186 
187 /// \class PcpPropertyIterator
188 ///
189 /// Object used to iterate over property specs in a property index in
190 /// strong-to-weak order.
191 ///
193  : public hboost::iterator_facade<
194  /* Derived = */ PcpPropertyIterator,
195  /* Value = */ const SdfPropertySpecHandle,
196  /* Category = */ hboost::random_access_traversal_tag
197  >
198 {
199 public:
200  /// Constructs an invalid iterator.
201  PCP_API
203 
204  /// Constructs a property iterator for \p index beginning at position
205  /// \p pos in the property stack.
206  PCP_API
207  PcpPropertyIterator(const PcpPropertyIndex& index, size_t pos = 0);
208 
209  /// Returns the PcpNode from which the current property originated.
210  PCP_API
211  PcpNodeRef GetNode() const;
212 
213  /// Returns true if the current property is local to the owning
214  /// property index's layer stack, false otherwise.
215  PCP_API
216  bool IsLocal() const;
217 
218 private:
220  PCP_API
221  void increment();
222  PCP_API
223  void decrement();
224  PCP_API
225  void advance(difference_type n);
226  PCP_API
227  difference_type distance_to(const PcpPropertyIterator& other) const;
228  PCP_API
229  bool equal(const PcpPropertyIterator& other) const;
230  PCP_API
231  reference dereference() const;
232 
233 private:
234  const PcpPropertyIndex* _propertyIndex;
235  size_t _pos;
236 };
237 
238 /// \class PcpPropertyReverseIterator
239 ///
240 /// Object used to iterate over property specs in a property index in
241 /// weak-to-strong order.
242 ///
244  : public std::reverse_iterator<PcpPropertyIterator>
245 {
246 public:
249  : std::reverse_iterator<PcpPropertyIterator>(iter) { }
250 
252  {
253  PcpPropertyIterator tmp = base();
254  return (--tmp).GetNode();
255  }
256 
257  bool IsLocal() const
258  {
259  PcpPropertyIterator tmp = base();
260  return (--tmp).IsLocal();
261  }
262 };
263 
264 // Helper macro for defining iterator ranges, which are simply pairs of
265 // iterators denoting the [start, end) of a series of values. These ranges
266 // may be used with TF_FOR_ALL and TF_REVERSE_FOR_ALL.
267 #define PCP_DEFINE_RANGE(Range, Iterator, ReverseIterator) \
268  typedef std::pair<Iterator, Iterator> Range; \
269  \
270  inline Iterator begin(Range &range) { return range.first; } \
271  inline Iterator begin(const Range &range) { return range.first; } \
272  inline Iterator end(Range &range) { return range.second; } \
273  inline Iterator end(const Range &range) { return range.second; } \
274  \
275  template <> \
276  struct Tf_IteratorInterface<Range, false> { \
277  typedef Iterator IteratorType; \
278  static IteratorType Begin(Range &c) { return c.first; } \
279  static IteratorType End(Range &c) { return c.second; } \
280  }; \
281  \
282  template <> \
283  struct Tf_IteratorInterface<const Range, false> { \
284  typedef Iterator IteratorType; \
285  static IteratorType Begin(Range const &c) { return c.first; } \
286  static IteratorType End(Range const &c) { return c.second; } \
287  }; \
288  \
289  template <> \
290  struct Tf_IteratorInterface<Range, true> { \
291  typedef ReverseIterator IteratorType; \
292  static IteratorType Begin(Range &c) \
293  { return IteratorType(c.second); } \
294  static IteratorType End(Range &c) \
295  { return IteratorType(c.first); } \
296  }; \
297  \
298  template <> \
299  struct Tf_IteratorInterface<const Range, true> { \
300  typedef ReverseIterator IteratorType; \
301  static IteratorType Begin(Range const &c) \
302  { return IteratorType(c.second); } \
303  static IteratorType End(Range const &c) \
304  { return IteratorType(c.first); } \
305  }; \
306  \
307  template <> \
308  struct Tf_ShouldIterateOverCopy<Range> : std::true_type {}; \
309  \
310  template <> \
311  struct Tf_ShouldIterateOverCopy<const Range> : std::true_type {}
312 
315 PCP_DEFINE_RANGE(PcpPropertyRange, PcpPropertyIterator,
317 
318 /// \class PcpIteratorTraits
319 ///
320 /// Traits class for retrieving useful characteristics about one of the
321 /// Pcp iterator types above.
322 ///
323 template <class Iterator> struct PcpIteratorTraits;
324 
325 template <>
327 {
328  typedef PcpNodeRange RangeType;
330 };
331 
332 template <>
334 {
335  typedef PcpPrimRange RangeType;
337 };
338 
339 template <>
341 {
342  typedef PcpPropertyRange RangeType;
344 };
345 
347 
348 #endif // PXR_USD_PCP_ITERATOR_H
PcpPropertyReverseIterator(const PcpPropertyIterator &iter)
Definition: iterator.h:248
PCP_API PcpPropertyIterator()
Constructs an invalid iterator.
PcpNodeRef GetNode() const
Definition: iterator.h:251
friend class hboost::iterator_core_access
Definition: iterator.h:219
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current prim originated.
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
PcpNodeRef GetNode() const
Definition: iterator.h:174
PcpNodeIterator()
Constructs an invalid iterator.
Definition: iterator.h:62
GLdouble n
Definition: glcorearb.h:2008
PcpPropertyReverseIterator ReverseIteratorType
Definition: iterator.h:343
friend class hboost::iterator_core_access
Definition: iterator.h:142
#define PCP_DEFINE_RANGE(Range, Iterator, ReverseIterator)
Definition: iterator.h:267
PcpPrimReverseIterator(const PcpPrimIterator &iter)
Definition: iterator.h:171
PcpPrimReverseIterator ReverseIteratorType
Definition: iterator.h:336
constexpr size_t PCP_INVALID_INDEX
Definition: types.h:217
PcpNodeReverseIterator ReverseIteratorType
Definition: iterator.h:329
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
bool IsLocal() const
Definition: iterator.h:257
Pcp_SdSiteRef _GetSiteRef() const
Definition: iterator.h:180
GLuint index
Definition: glcorearb.h:786
friend class hboost::iterator_core_access
Definition: iterator.h:75
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
PCP_API bool IsLocal() const
Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
Definition: iterator.h:65
PcpNodeReverseIterator(const PcpNodeIterator &iter)
Definition: iterator.h:105
PCP_API PcpPrimIterator()
Constructs an invalid iterator.
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current property originated.
PCP_API Pcp_SdSiteRef _GetSiteRef() const
#define PCP_API
Definition: api.h:40