7#ifndef PXR_USD_SDF_CHILDREN_PROXY_H
8#define PXR_USD_SDF_CHILDREN_PROXY_H
13#include "pxr/usd/sdf/api.h"
14#include "pxr/usd/sdf/changeBlock.h"
15#include "pxr/base/vt/value.h"
23PXR_NAMESPACE_OPEN_SCOPE
26class SdfChildrenProxy {
29 typedef typename View::Adapter Adapter;
30 typedef typename View::ChildPolicy ChildPolicy;
31 typedef typename View::key_type key_type;
32 typedef typename View::value_type mapped_type;
33 typedef std::vector<mapped_type> mapped_vector_type;
34 typedef std::pair<const key_type, mapped_type> value_type;
35 typedef std::map<key_type, mapped_type> map_type;
36 typedef typename View::size_type size_type;
37 typedef SdfChildrenProxy<View> This;
40 typedef typename View::const_iterator _inner_iterator;
44 _ValueProxy() : _owner(NULL) { }
45 _ValueProxy(This* owner, _inner_iterator i) : _owner(owner), _pos(i)
50 operator mapped_type()
const
56 _ValueProxy& operator=(
const U& x)
58 _owner->_Set(*_pos, x);
62 bool operator==(
const mapped_type& other)
const
64 return *_pos == other;
74 explicit _PairProxy(This* owner, _inner_iterator i) :
75 first(owner->_view.key(i)), second(owner, i) { }
80 operator value_type()
const
82 return value_type(first, second);
85 friend class _PairProxy;
89 static value_type Dereference(
const This* owner, _inner_iterator i)
91 return value_type(owner->_view.key(i), *i);
94 static _PairProxy Dereference(This* owner, _inner_iterator i)
96 return _PairProxy(owner, i);
100 template <
class _Owner,
class _Iter,
class _Value>
104 _Value* operator->() {
return &_value; }
106 friend class _Iterator;
107 explicit _PtrProxy(
const _Value& value) : _value(value) {}
111 static_assert(!std::is_reference<_Value>::value &&
112 !std::is_pointer<_Value>::value,
113 "_Value cannot be a pointer or reference type.");
114 using iterator_category = std::bidirectional_iterator_tag;
115 using value_type = _Value;
116 using reference = _Value;
117 using pointer = _PtrProxy;
118 using difference_type = std::ptrdiff_t;
120 _Iterator() =
default;
121 _Iterator(_Owner owner, _inner_iterator i) : _owner(owner), _pos(i) { }
122 template <
class O2,
class I2,
class V2>
123 _Iterator(
const _Iterator<O2, I2, V2>& other) :
124 _owner(other._owner), _pos(other._pos) { }
126 reference operator*()
const {
return dereference(); }
127 pointer operator->()
const {
return pointer(dereference()); }
129 _Iterator& operator++() {
134 _Iterator& operator--() {
139 _Iterator operator++(
int) {
140 _Iterator result(*
this);
145 _Iterator operator--(
int) {
146 _Iterator result(*
this);
151 template <
class O2,
class I2,
class V2>
152 bool operator==(
const _Iterator<O2, I2, V2>& other)
const {
156 template <
class O2,
class I2,
class V2>
157 bool operator!=(
const _Iterator<O2, I2, V2>& other)
const {
158 return !equal(other);
162 _Value dereference()
const
164 return _Traits::Dereference(_owner, _pos);
167 template <
class O2,
class I2,
class V2>
168 bool equal(
const _Iterator<O2, I2, V2>& other)
const
170 return _pos == other._pos;
183 _inner_iterator _pos;
185 template <
class O2,
class I2,
class V2>
186 friend class _Iterator;
190 typedef _ValueProxy reference;
191 typedef _Iterator<This*, _inner_iterator, _PairProxy> iterator;
192 typedef Tf_ProxyReferenceReverseIterator<iterator> reverse_iterator;
193 typedef _Iterator<const This*, _inner_iterator, value_type> const_iterator;
194 typedef Tf_ProxyReferenceReverseIterator<const_iterator> const_reverse_iterator;
196 static const int CanSet = 1;
197 static const int CanInsert = 2;
198 static const int CanErase = 4;
200 SdfChildrenProxy(
const View& view,
const std::string& type,
201 int permission = CanSet | CanInsert | CanErase) :
202 _view(view), _type(type), _permission(permission)
208 SdfChildrenProxy(
const SdfChildrenProxy<U>& other) :
209 _view(other._view), _type(other._type), _permission(other._permission)
214 This& operator=(
const This& other)
216 if (other._Validate()) {
217 _Copy(other._view.values());
223 This& operator=(
const SdfChildrenProxy<U>& other)
225 if (other._Validate()) {
226 _Copy(other._view.values());
231 This& operator=(
const mapped_vector_type& values)
237 operator mapped_vector_type()
const
239 return _Validate() ? _view.values() : mapped_vector_type();
242 map_type items()
const
244 return _Validate() ? _view.template items_as<map_type>() :map_type();
249 return iterator(_GetThis(), _view.begin());
253 return iterator(_GetThis(), _view.end());
255 const_iterator begin()
const
257 return const_iterator(_GetThis(), _view.begin());
259 const_iterator end()
const
261 return const_iterator(_GetThis(), _view.end());
264 reverse_iterator rbegin()
266 return reverse_iterator(end());
268 reverse_iterator rend()
270 return reverse_iterator(begin());
272 const_reverse_iterator rbegin()
const
274 return reverse_iterator(end());
276 const_reverse_iterator rend()
const
278 return reverse_iterator(begin());
281 size_type size()
const
283 return _Validate() ? _view.size() : 0;
286 size_type max_size()
const
288 return _view.max_size();
293 return _Validate() ? _view.empty() :
true;
296 std::pair<iterator, bool> insert(
const mapped_type& value)
298 if (_Validate(CanInsert)) {
299 iterator i = find(_view.key(value));
301 if (_PrimInsert(value, size())) {
302 return std::make_pair(find(_view.key(value)),
true);
305 return std::make_pair(end(),
false);
309 return std::make_pair(i,
false);
313 return std::make_pair(iterator(),
false);
317 iterator insert(iterator pos,
const mapped_type& value)
319 return insert(value).first;
322 template <
class InputIterator>
323 void insert(InputIterator first, InputIterator last)
325 if (_Validate(CanInsert)) {
327 for (; first != last; ++first) {
328 _PrimInsert(*first, size());
333 void erase(iterator pos)
338 size_type erase(
const key_type& key)
340 return _Erase(key) ? 1 : 0;
343 void erase(iterator first, iterator last)
345 if (_Validate(CanErase)) {
347 while (first != last) {
348 const key_type& key = first->first;
357 _Copy(mapped_vector_type());
360 iterator find(
const key_type& key)
362 return _Validate() ? iterator(
this, _view.find(key)) : iterator();
365 const_iterator find(
const key_type& key)
const
367 return _Validate() ? const_iterator(
this, _view.find(key)) :
371 size_type count(
const key_type& key)
const
373 return _Validate() ? _view.count(key) : 0;
376 bool operator==(
const This& other)
const
378 return _view == other._view;
381 bool operator!=(
const This& other)
const
383 return !(*
this == other);
388 explicit operator bool()
const
390 return _view.IsValid();
394 const std::string& _GetType()
const
399 int _GetPermission()
const
406 return _Validate() ? this : NULL;
409 const This* _GetThis()
const
411 return _Validate() ? this : NULL;
414 bool _Validate()
const
416 if (_view.IsValid()) {
425 bool _Validate(
int permission)
430 if ((_permission & permission) == permission) {
433 const char* op =
"edit";
434 if (~_permission & permission & CanSet) {
437 else if (~_permission & permission & CanInsert) {
440 else if (~_permission & permission & CanErase) {
447 bool _Copy(
const mapped_vector_type& values)
449 return _Validate(CanSet) ? _PrimCopy(values) : false;
452 bool _Insert(
const mapped_type& value,
size_t index)
454 return _Validate(CanInsert) ? _PrimInsert(value, index) : false;
457 bool _Erase(
const key_type& key)
459 return _Validate(CanErase) ? _PrimErase(key) : false;
462 bool _PrimCopy(
const mapped_vector_type& values)
464 typedef std::vector<typename ChildPolicy::ValueType>
467 ChildrenValueVector v;
468 for (
size_t i = 0; i < values.size(); ++i)
469 v.push_back(Adapter::Convert(values[i]));
471 return _view.GetChildren().Copy(v, _type);
474 bool _PrimInsert(
const mapped_type& value,
size_t index)
476 return _view.GetChildren().Insert(
477 Adapter::Convert(value), index, _type);
480 bool _PrimErase(
const key_type& key)
482 return _view.GetChildren().Erase(key, _type);
490 template <
class V>
friend class SdfChildrenProxy;
491 template <
class V>
friend class SdfPyChildrenProxy;
495template <
typename _View>
496struct Tf_ShouldIterateOverCopy<SdfChildrenProxy<_View> > : std::true_type
501template <
class _View>
502struct Vt_DefaultValueFactory<SdfChildrenProxy<_View> > {
503 static Vt_DefaultValueHolder Invoke() =
delete;
506PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
A simple iterator adapter for STL containers.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.