HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
childrenView.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_SDF_CHILDREN_VIEW_H
25 #define PXR_USD_SDF_CHILDREN_VIEW_H
26 
27 /// \file sdf/childrenView.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/api.h"
31 #include "pxr/usd/sdf/children.h"
32 #include "pxr/base/tf/iterator.h"
33 
34 #include <hboost/compressed_pair.hpp>
35 #include <hboost/iterator/filter_iterator.hpp>
36 #include <hboost/iterator/iterator_facade.hpp>
37 #include <hboost/iterator/reverse_iterator.hpp>
38 #include <algorithm>
39 #include <vector>
40 
42 
43 /// \class SdfChildrenViewTrivialPredicate
44 ///
45 /// Special case predicate that always passes.
46 ///
47 /// \c T is the type exposed by the value traits.
48 ///
49 /// This predicate is compiled out.
50 ///
51 template <class T>
53 public:
54  bool operator()(const T& x) const { return true; }
55 };
56 
57 /// \class SdfChildrenViewTrivialAdapter
58 ///
59 /// Special case adapter that does no conversions.
60 ///
61 template <class T>
63 public:
64  typedef T PrivateType;
65  typedef T PublicType;
66  static const PublicType& Convert(const PrivateType& t) { return t; }
67 };
68 
69 /// \class Sdf_ChildrenViewTraits
70 /// This traits class defines the iterator for a particular ChildrenView
71 /// along with conversions to and from the view's internal un-filtered iterator.
72 ///
73 /// A specialization of the traits for trivial predicates allows the
74 /// internal iterator to be used directly.
75 ///
76 template <typename _Owner, typename _InnerIterator, typename _DummyPredicate>
78 private:
79 
80  // Internal predicate object which will be passed to the filter
81  // iterator. This just calls through to the owner's predicate.
82  class _Predicate {
83  public:
84  typedef typename _Owner::value_type value_type;
85 
86  _Predicate() : _owner(NULL) { }
87  _Predicate(const _Owner* owner) : _owner(owner) { }
88 
89  bool operator()(const value_type& x) const
90  {
91  return _owner->GetPredicate()(
92  _Owner::Adapter::Convert(x));
93  }
94 
95  private:
96  const _Owner* _owner;
97  };
98 
99 public:
100  typedef hboost::filter_iterator<_Predicate, _InnerIterator> const_iterator;
101 
102  // Convert from a private _InnerIterator to a public const_iterator.
103  // filter_iterator requires an end iterator, which is constructed using
104  // size.
105  static const_iterator GetIterator(const _Owner* owner,
106  const _InnerIterator& i,
107  size_t size)
108  {
109  _InnerIterator end(owner,size);
110  return const_iterator(_Predicate(owner), i, end);
111  }
112 
113  // Convert from a public const_iterator to a private _InnerIterator.
114  static const _InnerIterator& GetBase(const const_iterator& i)
115  {
116  return i.base();
117  }
118 };
119 
120 // Children view traits specialization for trivial predicates. This
121 // eliminates the predicate altogether and defines the public iterator type
122 // to be the same as the inner iterator type.
123 template <typename _Owner, typename _InnerIterator>
124 class Sdf_ChildrenViewTraits<_Owner, _InnerIterator,
125  SdfChildrenViewTrivialPredicate<typename _Owner::ChildPolicy::ValueType> > {
126 private:
127 
128 public:
129  typedef _InnerIterator const_iterator;
130 
131  static const const_iterator& GetIterator(const _Owner*,
132  const _InnerIterator& i, size_t size)
133  {
134  return i;
135  }
136 
137  static const _InnerIterator& GetBase(const const_iterator& i)
138  {
139  return i;
140  }
141 };
142 
143 /// \class SdfChildrenView
144 ///
145 /// Provides a view onto an object's children.
146 ///
147 /// The \c _ChildPolicy dictates the type of children being viewed by this
148 /// object. This policy defines the key type by which children are referenced
149 /// (e.g. a TfToken, or an SdfPath) and the value type of the children objects.
150 ///
151 /// The \c _Predicate takes a value type argument and returns \c true if the
152 /// object should be included in the view and \c false otherwise.
153 ///
154 /// The \c _Adapter allows the view to present the children objects as a
155 /// different type. The _Adapter class must provide functions to convert the
156 /// children object type defined by \c _ChildPolicy to the desired public
157 /// type and vice-versa. See SdfChildrenViewTrivialAdapter for an example.
158 /// By default, the view presents children objects as the value type defined
159 /// in \c _ChildPolicy.
160 ///
161 /// Note that all methods are const, i.e. the children cannot be changed
162 /// through a view.
163 ///
164 template <typename _ChildPolicy,
165  typename _Predicate =
167  typename _ChildPolicy::ValueType>,
168  typename _Adapter =
170  typename _ChildPolicy::ValueType> >
172 public:
174 
175  typedef _Adapter Adapter;
176  typedef _Predicate Predicate;
177  typedef _ChildPolicy ChildPolicy;
178  typedef typename ChildPolicy::KeyPolicy KeyPolicy;
180 
181  typedef typename ChildPolicy::KeyType key_type;
182  typedef typename Adapter::PublicType value_type;
183 
184 private:
185 
186  // An iterator type for the internal unfiltered data storage. This
187  // iterator holds a pointer to its owning object and an index into
188  // the owner's storage. That allows the iterator to operate without
189  // knowing anything about the specific data storage that's used,
190  // which is important for providing both Gd and Lsd backed storage.
191  class _InnerIterator :
192  public hboost::iterator_facade<_InnerIterator,
193  value_type,
194  std::random_access_iterator_tag,
195  value_type> {
196  public:
197  typedef value_type reference;
198  typedef size_t size_type;
199  typedef ptrdiff_t difference_type;
200 
201  _InnerIterator() :
202  _owner(NULL), _pos(0) { }
203  _InnerIterator(const This* owner, const size_t& pos) :
204  _owner(owner), _pos(pos) { }
205 
206  private:
207  friend class hboost::iterator_core_access;
208 
209  reference dereference() const
210  {
211  return _owner->_Get(_pos);
212  }
213 
214  bool equal(const _InnerIterator& other) const
215  {
216  return _pos == other._pos;
217  }
218 
219  void increment() {
220  ++_pos;
221  }
222 
223  void decrement() {
224  --_pos;
225  }
226 
227  void advance(difference_type n) {
228  _pos += n;
229  }
230 
231  difference_type distance_to(const _InnerIterator& other) const {
232  return other._pos-_pos;
233  }
234 
235  private:
236  const This* _owner;
237  size_t _pos;
238  };
239 
240 public:
243  typedef hboost::reverse_iterator<const_iterator> const_reverse_iterator;
244  typedef size_t size_type;
245  typedef ptrdiff_t difference_type;
246 
248  {
249  }
250 
251  SdfChildrenView(const SdfLayerHandle &layer, const SdfPath &path,
252  const TfToken &childrenKey,
253  const KeyPolicy& keyPolicy = KeyPolicy()) :
254  _children(layer, path, childrenKey, keyPolicy)
255  {
256  }
257 
258  SdfChildrenView(const SdfLayerHandle &layer, const SdfPath &path,
259  const TfToken &childrenKey,
260  const Predicate& predicate,
261  const KeyPolicy& keyPolicy = KeyPolicy()) :
262  _children(layer, path, childrenKey, keyPolicy),
263  _predicate(predicate)
264  {
265  }
266 
268  _children(other._children),
269  _predicate(other._predicate)
270  {
271  }
272 
273  template <class OtherAdapter>
275  OtherAdapter> &other) :
276  _children(other._children),
277  _predicate(other._predicate)
278  {
279  }
280 
282  {
283  }
284 
286  {
287  _children= other._children;
288  _predicate = other._predicate;
289  return *this;
290  }
291 
292  /// Returns an const_iterator pointing to the beginning of the vector.
294  _InnerIterator i(this,0);
295  return _Traits::GetIterator(this, i, _GetSize());
296  }
297 
298  /// Returns an const_iterator pointing to the end of the vector.
299  const_iterator end() const {
300  _InnerIterator i(this,_GetSize());
301  return _Traits::GetIterator(this, i, _GetSize());
302  }
303 
304  /// Returns an const_reverse_iterator pointing to the beginning of the
305  /// reversed vector.
307  return const_reverse_iterator(end());
308  }
309 
310  /// Returns an const_reverse_iterator pointing to the end of the
311  /// reversed vector.
313  return const_reverse_iterator(begin());
314  }
315 
316  /// Returns the size of the vector.
317  size_type size() const {
318  return std::distance(begin(), end());
319  }
320 
321  /// Returns \c true if the vector is empty.
322  bool empty() const {
323  return begin() == end();
324  }
325 
326  /// Returns the \p n'th element.
328  const_iterator i = begin();
329  std::advance(i, n);
330  return *i;
331  }
332 
333  /// Returns the first element.
334  value_type front() const {
335  return *begin();
336  }
337 
338  /// Returns the last element.
339  value_type back() const {
340  return *rbegin();
341  }
342 
343  /// Finds the element with key \p x.
344  const_iterator find(const key_type& x) const {
345  _InnerIterator inner(this, _children.Find(x));
346  const_iterator iter = _Traits::GetIterator(this, inner, _GetSize());
347 
348  // _Traits::GetIterator may return a filtered iterator. We need to
349  // check that that iterator actually corresponds to the desired item.
350  // This ensures that we return end() in the case where the element being
351  // searched for is present in the children but filtered out by the
352  // view's predicate.
353  return _Traits::GetBase(iter) == inner ? iter : end();
354  }
355 
356  /// Finds element \p x, if present in this view.
357  const_iterator find(const value_type& x) const {
358  const_iterator i = find(key(x));
359  return (i != end() && *i == x) ? i : end();
360  }
361 
362  /// Returns the key for an element.
363  key_type key(const const_iterator& x) const {
364  return key(*x);
365  }
366 
367  /// Returns the key for a value.
368  key_type key(const value_type& x) const {
369  return _children.FindKey(Adapter::Convert(x));
370  }
371 
372  /// Returns the elements, in order.
373  std::vector<value_type> values() const {
374  return std::vector<value_type>(begin(), end());
375  }
376 
377  /// Returns the elements, in order.
378  template <typename V>
379  V values_as() const {
380  V x;
381  std::copy(begin(), end(), std::inserter(x, x.begin()));
382  return x;
383  }
384 
385  /// Returns the keys for all elements, in order.
386  std::vector<key_type> keys() const {
387  std::vector<key_type> result;
388  result.reserve(size());
389  for (const_iterator i = begin(), n = end(); i != n; ++i) {
390  result.push_back(key(i));
391  }
392  return result;
393  }
394 
395  /// Returns the keys for all elements, in order.
396  template <typename V>
397  V keys_as() const {
398  std::vector<key_type> k = keys();
399  return V(k.begin(), k.end());
400  }
401 
402  /// Returns the elements as a dictionary.
403  template <typename Dict>
404  Dict items_as() const {
405  Dict result;
406  for (const_iterator i = begin(), n = end(); i != n; ++i) {
407  result.insert(std::make_pair(key(i), *i));
408  }
409  return result;
410  }
411 
412  /// Returns true if an element with key \p x is in the container.
413  bool has(const key_type& x) const {
414  return (_children.Find(x) != _GetSize());
415  }
416 
417  /// Returns true if an element with the same key as \p x is in
418  /// the container.
419  bool has(const value_type& x) const {
420  return has(key(Adapter::Convert(x)));
421  }
422 
423  /// Returns the number of elements with key \p x in the container.
424  size_type count(const key_type& x) const {
425  return has(x);
426  }
427 
428  /// Returns the element with key \p x or a default constructed value
429  /// if no such element exists.
430  value_type get(const key_type& x) const {
431  size_t index = _children.Find(x);
432  if (index == _GetSize()) {
433  return value_type();
434  }
435  return _Get(index);
436  }
437 
438  /// Returns the element with key \p x or the fallback if no such
439  /// element exists.
440  value_type get(const key_type& x, const value_type& fallback) const {
441  size_t index = _children.Find(x);
442  if (index == _GetSize()) {
443  return fallback;
444  }
445  return _Get(index);
446  }
447 
448  /// Returns the element with key \p x or a default constructed value
449  /// if no such element exists.
450  value_type operator[](const key_type& x) const {
451  return get(x);
452  }
453 
454  /// Compares children for equality. Children are equal if the
455  /// list edits are identical and the keys contain the same elements.
456  bool operator==(const This& other) const {
457  return _children.IsEqualTo(other._children);
458  }
459 
460  /// Compares children for inequality. Children are not equal if
461  /// list edits are not identical or the keys don't contain the same
462  /// elements.
463  bool operator!=(const This& other) const {
464  return !_children.IsEqualTo(other._children);
465  }
466 
467  // Return true if this object is valid
468  bool IsValid() const {
469  return _children.IsValid();
470  }
471 
472  // Return the Sd_Children object that this view is holding.
474  return _children;
475  }
476 
477  // Return this view's predicate.
478  const Predicate& GetPredicate() const {
479  return _predicate;
480  }
481 
482 private:
483  // Return the value that corresponds to the provided index.
484  value_type _Get(size_type index) const {
485  return Adapter::Convert(_children.GetChild(index));
486  }
487 
488  // Return the number of elements
489  size_t _GetSize() const {
490  return _children.GetSize();
491  }
492 
493 private:
494  template <class V, class P, class A> friend class SdfChildrenView;
495  ChildrenType _children;
496  Predicate _predicate;
497 };
498 
499 /// Helper class to convert a given view of type \c _View to an
500 /// adapted view using \c _Adapter as the adapter class.
501 template <class _View, class _Adapter>
503 {
504  typedef _View OriginalView;
505  typedef SdfChildrenView<typename _View::ChildPolicy,
506  typename _View::Predicate,
507  _Adapter> AdaptedView;
508 
510  {
511  return AdaptedView(view);
512  }
513 };
514 
515 // Allow TfIteration over children views.
516 template <typename C, typename P, typename A>
517 struct Tf_ShouldIterateOverCopy<SdfChildrenView<C, P, A> > : hboost::true_type
518 {
519 };
520 template <typename C, typename P, typename A>
524  static IteratorType Begin(Type const &c) { return c.begin(); }
525  static IteratorType End(Type const &c) { return c.end(); }
526 };
527 template <typename C, typename P, typename A>
531  static IteratorType Begin(Type const &c) { return c.rbegin(); }
532  static IteratorType End(Type const &c) { return c.rend(); }
533 };
534 
536 
537 #endif // PXR_USD_SDF_CHILDREN_VIEW_H
SDF_API bool IsEqualTo(const This &other) const
Return true if this object and other are equivalent.
Dict items_as() const
Returns the elements as a dictionary.
Definition: childrenView.h:404
_Adapter Adapter
Definition: childrenView.h:175
SDF_API size_t GetSize() const
Return the number of children that this object contains.
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
Sdf_Children< ChildPolicy > ChildrenType
Definition: childrenView.h:179
SdfChildrenView(const SdfLayerHandle &layer, const SdfPath &path, const TfToken &childrenKey, const KeyPolicy &keyPolicy=KeyPolicy())
Definition: childrenView.h:251
value_type back() const
Returns the last element.
Definition: childrenView.h:339
SdfChildrenView< typename _View::ChildPolicy, typename _View::Predicate, _Adapter > AdaptedView
Definition: childrenView.h:507
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
static const const_iterator & GetIterator(const _Owner *, const _InnerIterator &i, size_t size)
Definition: childrenView.h:131
static const _InnerIterator & GetBase(const const_iterator &i)
Definition: childrenView.h:114
const GLfloat * c
Definition: glew.h:16631
ptrdiff_t difference_type
Definition: childrenView.h:245
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
_Traits::const_iterator const_iterator
Definition: childrenView.h:242
bool IsValid() const
Definition: childrenView.h:468
GLenum const void GLuint GLint reference
Definition: glew.h:13927
ChildrenType & GetChildren()
Definition: childrenView.h:473
V values_as() const
Returns the elements, in order.
Definition: childrenView.h:379
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
_ChildPolicy ChildPolicy
Definition: childrenView.h:177
bool empty() const
Returns true if the vector is empty.
Definition: childrenView.h:322
bool has(const value_type &x) const
Definition: childrenView.h:419
GLdouble GLdouble t
Definition: glew.h:1403
const_iterator begin() const
Returns an const_iterator pointing to the beginning of the vector.
Definition: childrenView.h:293
SDF_API KeyType FindKey(const ValueType &value) const
uint64 value_type
Definition: GA_PrimCompat.h:29
GLint GLenum GLint x
Definition: glcorearb.h:409
GLsizeiptr size
Definition: glcorearb.h:664
V keys_as() const
Returns the keys for all elements, in order.
Definition: childrenView.h:397
ChildPolicy::KeyType key_type
Definition: childrenView.h:181
std::vector< value_type > values() const
Returns the elements, in order.
Definition: childrenView.h:373
size_type size() const
Returns the size of the vector.
Definition: childrenView.h:317
SdfChildrenView(const SdfChildrenView< ChildPolicy, Predicate, OtherAdapter > &other)
Definition: childrenView.h:274
size_type count(const key_type &x) const
Returns the number of elements with key x in the container.
Definition: childrenView.h:424
SDF_API ValueType GetChild(size_t index) const
Return the child at the specified index.
Definition: token.h:87
value_type front() const
Returns the first element.
Definition: childrenView.h:334
const_iterator find(const value_type &x) const
Finds element x, if present in this view.
Definition: childrenView.h:357
Adapter::PublicType value_type
Definition: childrenView.h:182
SdfChildrenView & operator=(const SdfChildrenView &other)
Definition: childrenView.h:285
GLsizei GLsizei GLfloat distance
Definition: glew.h:13923
GLuint GLuint end
Definition: glcorearb.h:475
const_reverse_iterator rend() const
Definition: childrenView.h:312
SdfChildrenView< _ChildPolicy, _Predicate, _Adapter > This
Definition: childrenView.h:173
bool operator()(const T &x) const
Definition: childrenView.h:54
key_type key(const value_type &x) const
Returns the key for a value.
Definition: childrenView.h:368
Definition: path.h:290
GLdouble n
Definition: glcorearb.h:2008
hboost::filter_iterator< _Predicate, _InnerIterator > const_iterator
Definition: childrenView.h:100
value_type operator[](size_type n) const
Returns the n'th element.
Definition: childrenView.h:327
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1394
bool has(const key_type &x) const
Returns true if an element with key x is in the container.
Definition: childrenView.h:413
SdfChildrenView(const SdfLayerHandle &layer, const SdfPath &path, const TfToken &childrenKey, const Predicate &predicate, const KeyPolicy &keyPolicy=KeyPolicy())
Definition: childrenView.h:258
key_type key(const const_iterator &x) const
Returns the key for an element.
Definition: childrenView.h:363
value_type operator[](const key_type &x) const
Definition: childrenView.h:450
GLuint index
Definition: glcorearb.h:786
static const PublicType & Convert(const PrivateType &t)
Definition: childrenView.h:66
const Predicate & GetPredicate() const
Definition: childrenView.h:478
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: core.h:982
static AdaptedView Create(const OriginalView &view)
Definition: childrenView.h:509
const_iterator end() const
Returns an const_iterator pointing to the end of the vector.
Definition: childrenView.h:299
std::vector< key_type > keys() const
Returns the keys for all elements, in order.
Definition: childrenView.h:386
hboost::reverse_iterator< const_iterator > const_reverse_iterator
Definition: childrenView.h:243
static const_iterator GetIterator(const _Owner *owner, const _InnerIterator &i, size_t size)
Definition: childrenView.h:105
const_reverse_iterator rbegin() const
Definition: childrenView.h:306
ChildPolicy::KeyPolicy KeyPolicy
Definition: childrenView.h:178
bool operator==(const This &other) const
Definition: childrenView.h:456
Sdf_ChildrenViewTraits< This, _InnerIterator, Predicate > _Traits
Definition: childrenView.h:241
const_iterator find(const key_type &x) const
Finds the element with key x.
Definition: childrenView.h:344
_Predicate Predicate
Definition: childrenView.h:176
SDF_API bool IsValid() const
Return whether this object is valid.
bool operator!=(const This &other) const
Definition: childrenView.h:463
SdfChildrenView(const SdfChildrenView &other)
Definition: childrenView.h:267
SDF_API size_t Find(const KeyType &key) const
Find the index of the specified key, or return the size if it's not found.