HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
childrenProxy.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_PROXY_H
25 #define PXR_USD_SDF_CHILDREN_PROXY_H
26 
27 /// \file sdf/childrenProxy.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/api.h"
32 #include "pxr/base/vt/value.h"
33 #include "pxr/base/tf/diagnostic.h"
34 #include "pxr/base/tf/iterator.h"
35 
36 #include <hboost/iterator/iterator_facade.hpp>
37 #include <hboost/iterator/reverse_iterator.hpp>
38 #include <iterator>
39 #include <map>
40 #include <utility>
41 
43 
44 template <class _View>
46 public:
47  typedef _View View;
48  typedef typename View::Adapter Adapter;
49  typedef typename View::ChildPolicy ChildPolicy;
50  typedef typename View::key_type key_type;
51  typedef typename View::value_type mapped_type;
52  typedef std::vector<mapped_type> mapped_vector_type;
53  typedef std::pair<const key_type, mapped_type> value_type;
54  typedef std::map<key_type, mapped_type> map_type;
55  typedef typename View::size_type size_type;
57 
58 private:
59  typedef typename View::const_iterator _inner_iterator;
60 
61  class _ValueProxy {
62  public:
63  _ValueProxy() : _owner(NULL) { }
64  _ValueProxy(This* owner, _inner_iterator i) : _owner(owner), _pos(i)
65  {
66  // Do nothing
67  }
68 
69  operator mapped_type() const
70  {
71  return *_pos;
72  }
73 
74  template <class U>
75  _ValueProxy& operator=(const U& x)
76  {
77  _owner->_Set(*_pos, x);
78  return *this;
79  }
80 
81  bool operator==(const mapped_type& other) const
82  {
83  return *_pos == other;
84  }
85 
86  private:
87  This* _owner;
88  _inner_iterator _pos;
89  };
90 
91  class _PairProxy {
92  public:
93  explicit _PairProxy(This* owner, _inner_iterator i) :
94  first(owner->_view.key(i)), second(owner, i) { }
95 
96  const key_type first;
97  _ValueProxy second;
98 
99  operator value_type() const
100  {
101  return value_type(first, second);
102  }
103  };
104  friend class _PairProxy;
105 
106  class _Traits {
107  public:
108  static value_type Dereference(const This* owner, _inner_iterator i)
109  {
110  return value_type(owner->_view.key(i), *i);
111  }
112 
113  static _PairProxy Dereference(This* owner, _inner_iterator i)
114  {
115  return _PairProxy(owner, i);
116  }
117  };
118 
119  template <class _Owner, class _Iter, class _Value>
120  class _Iterator :
121  public hboost::iterator_facade<
122  _Iterator<_Owner, _Iter, _Value>,
123  _Value,
124  std::bidirectional_iterator_tag,
125  _Value> {
126  public:
127  _Iterator() { }
128  _Iterator(_Owner owner, _inner_iterator i) : _owner(owner), _pos(i) { }
129  template <class O2, class I2, class V2>
130  _Iterator(const _Iterator<O2, I2, V2>& other) :
131  _owner(other._owner), _pos(other._pos) { }
132 
133  private:
134  friend class hboost::iterator_core_access;
135 
136  _Value dereference() const
137  {
138  return _Traits::Dereference(_owner, _pos);
139  }
140 
141  template <class O2, class I2, class V2>
142  bool equal(const _Iterator<O2, I2, V2>& other) const
143  {
144  return _pos == other._pos;
145  }
146 
147  void increment() {
148  ++_pos;
149  }
150 
151  void decrement() {
152  --_pos;
153  }
154 
155  private:
156  _Owner _owner;
157  _inner_iterator _pos;
158 
159  template <class O2, class I2, class V2>
160  friend class _Iterator;
161  };
162 
163 public:
164  typedef _ValueProxy reference;
165  typedef _Iterator<This*, _inner_iterator, _PairProxy> iterator;
166  typedef hboost::reverse_iterator<iterator> reverse_iterator;
167  typedef _Iterator<const This*, _inner_iterator, value_type> const_iterator;
168  typedef hboost::reverse_iterator<const_iterator> const_reverse_iterator;
169 
170  static const int CanSet = 1;
171  static const int CanInsert = 2;
172  static const int CanErase = 4;
173 
175  int permission = CanSet | CanInsert | CanErase) :
176  _view(view), _type(type), _permission(permission)
177  {
178  // Do nothing
179  }
180 
181  template <class U>
183  _view(other._view), _type(other._type), _permission(other._permission)
184  {
185  // Do nothing
186  }
187 
188  This& operator=(const This& other)
189  {
190  if (other._Validate()) {
191  _Copy(other._view.values());
192  }
193  return *this;
194  }
195 
196  template <class U>
198  {
199  if (other._Validate()) {
200  _Copy(other._view.values());
201  }
202  return *this;
203  }
204 
206  {
207  _Copy(values);
208  return *this;
209  }
210 
211  operator mapped_vector_type() const
212  {
213  return _Validate() ? _view.values() : mapped_vector_type();
214  }
215 
216  map_type items() const
217  {
218  return _Validate() ? _view.template items_as<map_type>() :map_type();
219  }
220 
222  {
223  return iterator(_GetThis(), _view.begin());
224  }
226  {
227  return iterator(_GetThis(), _view.end());
228  }
230  {
231  return const_iterator(_GetThis(), _view.begin());
232  }
234  {
235  return const_iterator(_GetThis(), _view.end());
236  }
237 
239  {
240  return reverse_iterator(end());
241  }
243  {
244  return reverse_iterator(begin());
245  }
247  {
248  return reverse_iterator(end());
249  }
251  {
252  return reverse_iterator(begin());
253  }
254 
255  size_type size() const
256  {
257  return _Validate() ? _view.size() : 0;
258  }
259 
261  {
262  return _view.max_size();
263  }
264 
265  bool empty() const
266  {
267  return _Validate() ? _view.empty() : true;
268  }
269 
270  std::pair<iterator, bool> insert(const mapped_type& value)
271  {
272  if (_Validate(CanInsert)) {
273  iterator i = find(_view.key(value));
274  if (i == end()) {
275  if (_PrimInsert(value, size())) {
276  return std::make_pair(find(_view.key(value)), true);
277  }
278  else {
279  return std::make_pair(end(), false);
280  }
281  }
282  else {
283  return std::make_pair(i, false);
284  }
285  }
286  else {
287  return std::make_pair(iterator(), false);
288  }
289  }
290 
292  {
293  return insert(value).first;
294  }
295 
296  template <class InputIterator>
297  void insert(InputIterator first, InputIterator last)
298  {
299  if (_Validate(CanInsert)) {
300  SdfChangeBlock block;
301  for (; first != last; ++first) {
302  _PrimInsert(*first, size());
303  }
304  }
305  }
306 
307  void erase(iterator pos)
308  {
309  _Erase(pos->first);
310  }
311 
313  {
314  return _Erase(key) ? 1 : 0;
315  }
316 
318  {
319  if (_Validate(CanErase)) {
320  SdfChangeBlock block;
321  while (first != last) {
322  const key_type& key = first->first;
323  ++first;
324  _PrimErase(key);
325  }
326  }
327  }
328 
329  void clear()
330  {
331  _Copy(mapped_vector_type());
332  }
333 
334  iterator find(const key_type& key)
335  {
336  return _Validate() ? iterator(this, _view.find(key)) : iterator();
337  }
338 
339  const_iterator find(const key_type& key) const
340  {
341  return _Validate() ? const_iterator(this, _view.find(key)) :
342  const_iterator();
343  }
344 
345  size_type count(const key_type& key) const
346  {
347  return _Validate() ? _view.count(key) : 0;
348  }
349 
350  bool operator==(const This& other) const
351  {
352  return _view == other._view;
353  }
354 
355  bool operator!=(const This& other) const
356  {
357  return !(*this == other);
358  }
359 
360  /// Explicit bool conversion operator. The proxy object converts to
361  /// \c true if it is valid, \c false otherwise.
362  explicit operator bool() const
363  {
364  return _view.IsValid();
365  }
366 
367 private:
368  const std::string& _GetType() const
369  {
370  return _type;
371  }
372 
373  int _GetPermission() const
374  {
375  return _permission;
376  }
377 
378  This* _GetThis()
379  {
380  return _Validate() ? this : NULL;
381  }
382 
383  const This* _GetThis() const
384  {
385  return _Validate() ? this : NULL;
386  }
387 
388  bool _Validate() const
389  {
390  if (_view.IsValid()) {
391  return true;
392  }
393  else {
394  TF_CODING_ERROR("Accessing expired %s", _type.c_str());
395  return false;
396  }
397  }
398 
399  bool _Validate(int permission)
400  {
401  if (!_Validate()) {
402  return false;
403  }
404  if ((_permission & permission) == permission) {
405  return true;
406  }
407  const char* op = "edit";
408  if (~_permission & permission & CanSet) {
409  op = "replace";
410  }
411  else if (~_permission & permission & CanInsert) {
412  op = "insert";
413  }
414  else if (~_permission & permission & CanErase) {
415  op = "remove";
416  }
417  TF_CODING_ERROR("Cannot %s %s", op, _type.c_str());
418  return false;
419  }
420 
421  bool _Copy(const mapped_vector_type& values)
422  {
423  return _Validate(CanSet) ? _PrimCopy(values) : false;
424  }
425 
426  bool _Insert(const mapped_type& value, size_t index)
427  {
428  return _Validate(CanInsert) ? _PrimInsert(value, index) : false;
429  }
430 
431  bool _Erase(const key_type& key)
432  {
433  return _Validate(CanErase) ? _PrimErase(key) : false;
434  }
435 
436  bool _PrimCopy(const mapped_vector_type& values)
437  {
438  typedef std::vector<typename ChildPolicy::ValueType>
439  ChildrenValueVector;
440 
441  ChildrenValueVector v;
442  for (size_t i = 0; i < values.size(); ++i)
443  v.push_back(Adapter::Convert(values[i]));
444 
445  return _view.GetChildren().Copy(v, _type);
446  }
447 
448  bool _PrimInsert(const mapped_type& value, size_t index)
449  {
450  return _view.GetChildren().Insert(
451  Adapter::Convert(value), index, _type);
452  }
453 
454  bool _PrimErase(const key_type& key)
455  {
456  return _view.GetChildren().Erase(key, _type);
457  }
458 
459 private:
460  View _view;
461  std::string _type;
462  int _permission;
463 
464  template <class V> friend class SdfChildrenProxy;
465  template <class V> friend class SdfPyChildrenProxy;
466 };
467 
468 // Allow TfIteration over children proxies.
469 template <typename _View>
470 struct Tf_ShouldIterateOverCopy<SdfChildrenProxy<_View> > : std::true_type
471 {
472 };
473 
474 // Cannot get from a VtValue except as the correct type.
475 template <class _View>
477  static Vt_DefaultValueHolder Invoke() = delete;
478 };
479 
481 
482 #endif // PXR_USD_SDF_CHILDREN_PROXY_H
const_iterator end() const
GLint first
Definition: glcorearb.h:405
_Iterator< const This *, _inner_iterator, value_type > const_iterator
This & operator=(const This &other)
size_type erase(const key_type &key)
const_reverse_iterator rbegin() const
reverse_iterator rbegin()
SdfChildrenProxy(const SdfChildrenProxy< U > &other)
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
This & operator=(const SdfChildrenProxy< U > &other)
View::key_type key_type
Definition: childrenProxy.h:50
#define TF_CODING_ERROR
bool operator==(const This &other) const
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
SdfChildrenProxy(const View &view, const std::string &type, int permission=CanSet|CanInsert|CanErase)
size_type max_size() const
uint64 value_type
Definition: GA_PrimCompat.h:29
View::size_type size_type
Definition: childrenProxy.h:55
bool empty() const
View::ChildPolicy ChildPolicy
Definition: childrenProxy.h:49
iterator begin()
std::map< key_type, mapped_type > map_type
Definition: childrenProxy.h:54
_ValueProxy reference
static const int CanErase
SdfChildrenProxy< View > This
Definition: childrenProxy.h:56
const_iterator begin() const
bool operator!=(const This &other) const
std::pair< iterator, bool > insert(const mapped_type &value)
size_type size() const
iterator find(const key_type &key)
hboost::reverse_iterator< iterator > reverse_iterator
iterator insert(iterator pos, const mapped_type &value)
friend class _PairProxy
hboost::reverse_iterator< const_iterator > const_reverse_iterator
View::Adapter Adapter
Definition: childrenProxy.h:48
GLint GLenum GLint x
Definition: glcorearb.h:409
std::vector< mapped_type > mapped_vector_type
Definition: childrenProxy.h:52
View::value_type mapped_type
Definition: childrenProxy.h:51
void erase(iterator first, iterator last)
_Iterator< This *, _inner_iterator, _PairProxy > iterator
void insert(InputIterator first, InputIterator last)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
static const int CanSet
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
map_type items() const
Definition: core.h:982
size_type count(const key_type &key) const
Definition: core.h:1131
This & operator=(const mapped_vector_type &values)
std::pair< const key_type, mapped_type > value_type
Definition: childrenProxy.h:53
reverse_iterator rend()
void erase(iterator pos)
static const int CanInsert
const_reverse_iterator rend() const
type
Definition: core.h:1059
static Vt_DefaultValueHolder Invoke()
Definition: value.h:1501
const_iterator find(const key_type &key) const