All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
node_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 PCP_NODE_ITERATOR_H
25 #define PCP_NODE_ITERATOR_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/pcp/node.h"
31 #include "pxr/usd/pcp/primIndex_Graph.h"
32 
33 PXR_NAMESPACE_OPEN_SCOPE
34 
35 // These classes exist because we want to optimize the iteration of a
36 // node's children while not exposing the PcpPrimIndex_Graph implementation
37 // detail outside of Pcp. PcpNodeRef_ChildrenIterator and
38 // PcpNodeRef_ChildrenReverseIterator perform the same functions but can't
39 // inline access to PcpPrimIndex_Graph.
40 
46 class PcpNodeRef_PrivateChildrenConstIterator
47  : public boost::iterator_facade<
48  /* Derived = */ PcpNodeRef_PrivateChildrenConstIterator,
49  /* ValueType = */ const PcpNodeRef,
50  /* Category = */ boost::forward_traversal_tag
51  >
52 {
53 public:
54  // Required by TF_FOR_ALL but always assigned to afterwards.
55  PcpNodeRef_PrivateChildrenConstIterator() { }
56 
59  PcpNodeRef_PrivateChildrenConstIterator(const PcpNodeRef& node,
60  bool end = false) :
61  _node(node),
62  _nodes(&_node._graph->_data->nodes[0])
63  {
64  _node._nodeIdx = end
65  ? PcpPrimIndex_Graph::_Node::_invalidNodeIndex
66  : _nodes[_node._nodeIdx].smallInts.firstChildIndex;
67  }
68 
69 private:
70  friend class boost::iterator_core_access;
71  void increment()
72  {
73  _node._nodeIdx = _nodes[_node._nodeIdx].smallInts.nextSiblingIndex;
74  }
75 
76  bool equal(const PcpNodeRef_PrivateChildrenConstIterator& other) const
77  {
78  return _node == other._node;
79  }
80 
81  reference dereference() const
82  {
83  return _node;
84  }
85 
86 private:
87  // Current graph node this iterator is pointing at.
88  PcpNodeRef _node;
89  const PcpPrimIndex_Graph::_Node* _nodes;
90 };
91 
97 class PcpNodeRef_PrivateChildrenConstReverseIterator
98  : public boost::iterator_facade<
99  /* Derived = */ PcpNodeRef_PrivateChildrenConstReverseIterator,
100  /* ValueType = */ const PcpNodeRef,
101  /* Category = */ boost::forward_traversal_tag
102  >
103 {
104 public:
105  // Required by TF_FOR_ALL but always assigned to afterwards.
106  PcpNodeRef_PrivateChildrenConstReverseIterator() { }
107 
110  PcpNodeRef_PrivateChildrenConstReverseIterator(const PcpNodeRef& node,
111  bool end = false) :
112  _node(node),
113  _nodes(&_node._graph->_data->nodes[0])
114  {
115  _node._nodeIdx = end
116  ? PcpPrimIndex_Graph::_Node::_invalidNodeIndex
117  : _nodes[_node._nodeIdx].smallInts.lastChildIndex;
118  }
119 
120 private:
121  friend class boost::iterator_core_access;
122  void increment()
123  {
124  _node._nodeIdx = _nodes[_node._nodeIdx].smallInts.prevSiblingIndex;
125  }
126 
127  bool equal(const PcpNodeRef_PrivateChildrenConstReverseIterator& other)const
128  {
129  return _node == other._node;
130  }
131 
132  reference dereference() const
133  {
134  return _node;
135  }
136 
137 private:
138  // Current graph node this iterator is pointing at.
139  PcpNodeRef _node;
140  const PcpPrimIndex_Graph::_Node* _nodes;
141 };
142 
143 // Wrapper type for TF_FOR_ALL().
144 class PcpNodeRef_PrivateChildrenConstRange {
145 public:
146  PcpNodeRef_PrivateChildrenConstRange(const PcpNodeRef& node_):node(node_){}
147  PcpNodeRef node;
148 };
149 
150 // TF_FOR_ALL() traits. We build the iterators on demand.
151 template <>
152 struct Tf_IteratorInterface<PcpNodeRef_PrivateChildrenConstRange, false> {
153  typedef PcpNodeRef_PrivateChildrenConstRange RangeType;
154  typedef PcpNodeRef_PrivateChildrenConstIterator IteratorType;
155  static IteratorType Begin(RangeType const &c)
156  {
157  return IteratorType(c.node, /* end = */ false);
158  }
159  static IteratorType End(RangeType const &c)
160  {
161  return IteratorType(c.node, /* end = */ true);
162  }
163 };
164 template <>
165 struct Tf_IteratorInterface<PcpNodeRef_PrivateChildrenConstRange, true> {
166  typedef PcpNodeRef_PrivateChildrenConstRange RangeType;
167  typedef PcpNodeRef_PrivateChildrenConstReverseIterator IteratorType;
168  static IteratorType Begin(RangeType const &c)
169  {
170  return IteratorType(c.node, /* end = */ false);
171  }
172  static IteratorType End(RangeType const &c)
173  {
174  return IteratorType(c.node, /* end = */ true);
175  }
176 };
177 template <>
178 struct Tf_ShouldIterateOverCopy<PcpNodeRef_PrivateChildrenConstRange> :
179  boost::true_type {};
180 
181 // Wrap a node for use by TF_FOR_ALL().
182 inline
183 PcpNodeRef_PrivateChildrenConstRange
184 Pcp_GetChildrenRange(const PcpNodeRef& node)
185 {
186  return PcpNodeRef_PrivateChildrenConstRange(node);
187 }
188 
189 // Return all of a node's children, strong-to-weak.
190 inline
191 PcpNodeRefVector
192 Pcp_GetChildren(const PcpNodeRef& node)
193 {
194  typedef PcpNodeRef_PrivateChildrenConstIterator IteratorType;
195  return PcpNodeRefVector(IteratorType(node, /* end = */ false),
196  IteratorType(node, /* end = */ true));
197 }
198 
199 PXR_NAMESPACE_CLOSE_SCOPE
200 
201 #endif // PCP_NODE_ITERATOR_H
PcpNode represents a node in an expression tree for compositing scene description.
Definition: node.h:65