24#ifndef PXR_USD_SDF_CHILDREN_PROXY_H
25#define PXR_USD_SDF_CHILDREN_PROXY_H
30#include "pxr/usd/sdf/api.h"
31#include "pxr/usd/sdf/changeBlock.h"
32#include "pxr/base/vt/value.h"
40PXR_NAMESPACE_OPEN_SCOPE
43class SdfChildrenProxy {
46 typedef typename View::Adapter Adapter;
47 typedef typename View::ChildPolicy ChildPolicy;
48 typedef typename View::key_type key_type;
49 typedef typename View::value_type mapped_type;
50 typedef std::vector<mapped_type> mapped_vector_type;
51 typedef std::pair<const key_type, mapped_type> value_type;
52 typedef std::map<key_type, mapped_type> map_type;
53 typedef typename View::size_type size_type;
54 typedef SdfChildrenProxy<View> This;
57 typedef typename View::const_iterator _inner_iterator;
61 _ValueProxy() : _owner(NULL) { }
62 _ValueProxy(This* owner, _inner_iterator i) : _owner(owner), _pos(i)
67 operator mapped_type()
const
73 _ValueProxy& operator=(
const U& x)
75 _owner->_Set(*_pos, x);
79 bool operator==(
const mapped_type& other)
const
81 return *_pos == other;
91 explicit _PairProxy(This* owner, _inner_iterator i) :
92 first(owner->_view.key(i)), second(owner, i) { }
97 operator value_type()
const
99 return value_type(first, second);
102 friend class _PairProxy;
106 static value_type Dereference(
const This* owner, _inner_iterator i)
108 return value_type(owner->_view.key(i), *i);
111 static _PairProxy Dereference(This* owner, _inner_iterator i)
113 return _PairProxy(owner, i);
117 template <
class _Owner,
class _Iter,
class _Value>
121 _Value* operator->() {
return &_value; }
123 friend class _Iterator;
124 explicit _PtrProxy(
const _Value& value) : _value(value) {}
128 static_assert(!std::is_reference<_Value>::value &&
129 !std::is_pointer<_Value>::value,
130 "_Value cannot be a pointer or reference type.");
131 using iterator_category = std::bidirectional_iterator_tag;
132 using value_type = _Value;
133 using reference = _Value;
134 using pointer = _PtrProxy;
135 using difference_type = std::ptrdiff_t;
137 _Iterator() =
default;
138 _Iterator(_Owner owner, _inner_iterator i) : _owner(owner), _pos(i) { }
139 template <
class O2,
class I2,
class V2>
140 _Iterator(
const _Iterator<O2, I2, V2>& other) :
141 _owner(other._owner), _pos(other._pos) { }
143 reference operator*()
const {
return dereference(); }
144 pointer operator->()
const {
return pointer(dereference()); }
146 _Iterator& operator++() {
151 _Iterator& operator--() {
156 _Iterator operator++(
int) {
157 _Iterator result(*
this);
162 _Iterator operator--(
int) {
163 _Iterator result(*
this);
168 template <
class O2,
class I2,
class V2>
169 bool operator==(
const _Iterator<O2, I2, V2>& other)
const {
173 template <
class O2,
class I2,
class V2>
174 bool operator!=(
const _Iterator<O2, I2, V2>& other)
const {
175 return !equal(other);
179 _Value dereference()
const
181 return _Traits::Dereference(_owner, _pos);
184 template <
class O2,
class I2,
class V2>
185 bool equal(
const _Iterator<O2, I2, V2>& other)
const
187 return _pos == other._pos;
200 _inner_iterator _pos;
202 template <
class O2,
class I2,
class V2>
203 friend class _Iterator;
207 typedef _ValueProxy reference;
208 typedef _Iterator<This*, _inner_iterator, _PairProxy> iterator;
209 typedef Tf_ProxyReferenceReverseIterator<iterator> reverse_iterator;
210 typedef _Iterator<const This*, _inner_iterator, value_type> const_iterator;
211 typedef Tf_ProxyReferenceReverseIterator<const_iterator> const_reverse_iterator;
213 static const int CanSet = 1;
214 static const int CanInsert = 2;
215 static const int CanErase = 4;
217 SdfChildrenProxy(
const View& view,
const std::string& type,
218 int permission = CanSet | CanInsert | CanErase) :
219 _view(view), _type(type), _permission(permission)
225 SdfChildrenProxy(
const SdfChildrenProxy<U>& other) :
226 _view(other._view), _type(other._type), _permission(other._permission)
231 This& operator=(
const This& other)
233 if (other._Validate()) {
234 _Copy(other._view.values());
240 This& operator=(
const SdfChildrenProxy<U>& other)
242 if (other._Validate()) {
243 _Copy(other._view.values());
248 This& operator=(
const mapped_vector_type& values)
254 operator mapped_vector_type()
const
256 return _Validate() ? _view.values() : mapped_vector_type();
259 map_type items()
const
261 return _Validate() ? _view.template items_as<map_type>() :map_type();
266 return iterator(_GetThis(), _view.begin());
270 return iterator(_GetThis(), _view.end());
272 const_iterator begin()
const
274 return const_iterator(_GetThis(), _view.begin());
276 const_iterator end()
const
278 return const_iterator(_GetThis(), _view.end());
281 reverse_iterator rbegin()
283 return reverse_iterator(end());
285 reverse_iterator rend()
287 return reverse_iterator(begin());
289 const_reverse_iterator rbegin()
const
291 return reverse_iterator(end());
293 const_reverse_iterator rend()
const
295 return reverse_iterator(begin());
298 size_type size()
const
300 return _Validate() ? _view.size() : 0;
303 size_type max_size()
const
305 return _view.max_size();
310 return _Validate() ? _view.empty() :
true;
313 std::pair<iterator, bool> insert(
const mapped_type& value)
315 if (_Validate(CanInsert)) {
316 iterator i = find(_view.key(value));
318 if (_PrimInsert(value, size())) {
319 return std::make_pair(find(_view.key(value)),
true);
322 return std::make_pair(end(),
false);
326 return std::make_pair(i,
false);
330 return std::make_pair(iterator(),
false);
334 iterator insert(iterator pos,
const mapped_type& value)
336 return insert(value).first;
339 template <
class InputIterator>
340 void insert(InputIterator first, InputIterator last)
342 if (_Validate(CanInsert)) {
344 for (; first != last; ++first) {
345 _PrimInsert(*first, size());
350 void erase(iterator pos)
355 size_type erase(
const key_type& key)
357 return _Erase(key) ? 1 : 0;
360 void erase(iterator first, iterator last)
362 if (_Validate(CanErase)) {
364 while (first != last) {
365 const key_type& key = first->first;
374 _Copy(mapped_vector_type());
377 iterator find(
const key_type& key)
379 return _Validate() ? iterator(
this, _view.find(key)) : iterator();
382 const_iterator find(
const key_type& key)
const
384 return _Validate() ? const_iterator(
this, _view.find(key)) :
388 size_type count(
const key_type& key)
const
390 return _Validate() ? _view.count(key) : 0;
393 bool operator==(
const This& other)
const
395 return _view == other._view;
398 bool operator!=(
const This& other)
const
400 return !(*
this == other);
405 explicit operator bool()
const
407 return _view.IsValid();
411 const std::string& _GetType()
const
416 int _GetPermission()
const
423 return _Validate() ? this : NULL;
426 const This* _GetThis()
const
428 return _Validate() ? this : NULL;
431 bool _Validate()
const
433 if (_view.IsValid()) {
442 bool _Validate(
int permission)
447 if ((_permission & permission) == permission) {
450 const char* op =
"edit";
451 if (~_permission & permission & CanSet) {
454 else if (~_permission & permission & CanInsert) {
457 else if (~_permission & permission & CanErase) {
464 bool _Copy(
const mapped_vector_type& values)
466 return _Validate(CanSet) ? _PrimCopy(values) : false;
469 bool _Insert(
const mapped_type& value,
size_t index)
471 return _Validate(CanInsert) ? _PrimInsert(value, index) : false;
474 bool _Erase(
const key_type& key)
476 return _Validate(CanErase) ? _PrimErase(key) : false;
479 bool _PrimCopy(
const mapped_vector_type& values)
481 typedef std::vector<typename ChildPolicy::ValueType>
484 ChildrenValueVector v;
485 for (
size_t i = 0; i < values.size(); ++i)
486 v.push_back(Adapter::Convert(values[i]));
488 return _view.GetChildren().Copy(v, _type);
491 bool _PrimInsert(
const mapped_type& value,
size_t index)
493 return _view.GetChildren().Insert(
494 Adapter::Convert(value), index, _type);
497 bool _PrimErase(
const key_type& key)
499 return _view.GetChildren().Erase(key, _type);
507 template <
class V>
friend class SdfChildrenProxy;
508 template <
class V>
friend class SdfPyChildrenProxy;
512template <
typename _View>
513struct Tf_ShouldIterateOverCopy<SdfChildrenProxy<_View> > : std::true_type
518template <
class _View>
519struct Vt_DefaultValueFactory<SdfChildrenProxy<_View> > {
520 static Vt_DefaultValueHolder Invoke() =
delete;
523PXR_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.