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