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