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
55 bool operator==(
const mapped_type& other)
const
57 return *_pos == other;
67 explicit _PairProxy(This* owner, _inner_iterator i) :
68 first(owner->_view.key(i)), second(owner, i) { }
73 operator value_type()
const
75 return value_type(first, second);
78 friend class _PairProxy;
82 static value_type Dereference(
const This* owner, _inner_iterator i)
84 return value_type(owner->_view.key(i), *i);
87 static _PairProxy Dereference(This* owner, _inner_iterator i)
89 return _PairProxy(owner, i);
93 template <
class _Owner,
class _Iter,
class _Value>
97 _Value* operator->() {
return &_value; }
99 friend class _Iterator;
100 explicit _PtrProxy(
const _Value& value) : _value(value) {}
104 static_assert(!std::is_reference<_Value>::value &&
105 !std::is_pointer<_Value>::value,
106 "_Value cannot be a pointer or reference type.");
107 using iterator_category = std::bidirectional_iterator_tag;
108 using value_type = _Value;
109 using reference = _Value;
110 using pointer = _PtrProxy;
111 using difference_type = std::ptrdiff_t;
113 _Iterator() =
default;
114 _Iterator(_Owner owner, _inner_iterator i) : _owner(owner), _pos(i) { }
115 template <
class O2,
class I2,
class V2>
116 _Iterator(
const _Iterator<O2, I2, V2>& other) :
117 _owner(other._owner), _pos(other._pos) { }
119 reference operator*()
const {
return dereference(); }
120 pointer operator->()
const {
return pointer(dereference()); }
122 _Iterator& operator++() {
127 _Iterator& operator--() {
132 _Iterator operator++(
int) {
133 _Iterator result(*
this);
138 _Iterator operator--(
int) {
139 _Iterator result(*
this);
144 template <
class O2,
class I2,
class V2>
145 bool operator==(
const _Iterator<O2, I2, V2>& other)
const {
149 template <
class O2,
class I2,
class V2>
150 bool operator!=(
const _Iterator<O2, I2, V2>& other)
const {
151 return !equal(other);
155 _Value dereference()
const
157 return _Traits::Dereference(_owner, _pos);
160 template <
class O2,
class I2,
class V2>
161 bool equal(
const _Iterator<O2, I2, V2>& other)
const
163 return _pos == other._pos;
176 _inner_iterator _pos;
178 template <
class O2,
class I2,
class V2>
179 friend class _Iterator;
183 typedef _ValueProxy reference;
184 typedef _Iterator<This*, _inner_iterator, _PairProxy> iterator;
185 typedef Tf_ProxyReferenceReverseIterator<iterator> reverse_iterator;
186 typedef _Iterator<const This*, _inner_iterator, value_type> const_iterator;
187 typedef Tf_ProxyReferenceReverseIterator<const_iterator> const_reverse_iterator;
189 static const int CanSet = 1;
190 static const int CanInsert = 2;
191 static const int CanErase = 4;
193 SdfChildrenProxy(
const View& view,
const std::string& type,
194 int permission = CanSet | CanInsert | CanErase) :
195 _view(view), _type(type), _permission(permission)
201 SdfChildrenProxy(
const SdfChildrenProxy<U>& other) :
202 _view(other._view), _type(other._type), _permission(other._permission)
207 This& operator=(
const This& other)
209 if (other._Validate()) {
210 _Copy(other._view.values());
216 This& operator=(
const SdfChildrenProxy<U>& other)
218 if (other._Validate()) {
219 _Copy(other._view.values());
224 This& operator=(
const mapped_vector_type& values)
230 operator mapped_vector_type()
const
232 return _Validate() ? _view.values() : mapped_vector_type();
235 map_type items()
const
237 return _Validate() ? _view.template items_as<map_type>() :map_type();
242 return iterator(_GetThis(), _view.begin());
246 return iterator(_GetThis(), _view.end());
248 const_iterator begin()
const
250 return const_iterator(_GetThis(), _view.begin());
252 const_iterator end()
const
254 return const_iterator(_GetThis(), _view.end());
257 reverse_iterator rbegin()
259 return reverse_iterator(end());
261 reverse_iterator rend()
263 return reverse_iterator(begin());
265 const_reverse_iterator rbegin()
const
267 return reverse_iterator(end());
269 const_reverse_iterator rend()
const
271 return reverse_iterator(begin());
274 size_type size()
const
276 return _Validate() ? _view.size() : 0;
279 size_type max_size()
const
281 return _view.max_size();
286 return _Validate() ? _view.empty() :
true;
289 std::pair<iterator, bool> insert(
const mapped_type& value)
291 if (_Validate(CanInsert)) {
292 iterator i = find(_view.key(value));
294 if (_PrimInsert(value, size())) {
295 return std::make_pair(find(_view.key(value)),
true);
298 return std::make_pair(end(),
false);
302 return std::make_pair(i,
false);
306 return std::make_pair(iterator(),
false);
310 iterator insert(iterator pos,
const mapped_type& value)
312 return insert(value).first;
315 template <
class InputIterator>
316 void insert(InputIterator first, InputIterator last)
318 if (_Validate(CanInsert)) {
320 for (; first != last; ++first) {
321 _PrimInsert(*first, size());
326 void erase(iterator pos)
331 size_type erase(
const key_type& key)
333 return _Erase(key) ? 1 : 0;
336 void erase(iterator first, iterator last)
338 if (_Validate(CanErase)) {
340 while (first != last) {
341 const key_type& key = first->first;
350 _Copy(mapped_vector_type());
353 iterator find(
const key_type& key)
355 return _Validate() ? iterator(
this, _view.find(key)) : iterator();
358 const_iterator find(
const key_type& key)
const
360 return _Validate() ? const_iterator(
this, _view.find(key)) :
364 size_type count(
const key_type& key)
const
366 return _Validate() ? _view.count(key) : 0;
369 bool operator==(
const This& other)
const
371 return _view == other._view;
374 bool operator!=(
const This& other)
const
376 return !(*
this == other);
381 explicit operator bool()
const
383 return _view.IsValid();
387 const std::string& _GetType()
const
392 int _GetPermission()
const
399 return _Validate() ? this : NULL;
402 const This* _GetThis()
const
404 return _Validate() ? this : NULL;
407 bool _Validate()
const
409 if (_view.IsValid()) {
418 bool _Validate(
int permission)
423 if ((_permission & permission) == permission) {
426 const char* op =
"edit";
427 if (~_permission & permission & CanSet) {
430 else if (~_permission & permission & CanInsert) {
433 else if (~_permission & permission & CanErase) {
440 bool _Copy(
const mapped_vector_type& values)
442 return _Validate(CanSet) ? _PrimCopy(values) : false;
445 bool _Insert(
const mapped_type& value,
size_t index)
447 return _Validate(CanInsert) ? _PrimInsert(value, index) : false;
450 bool _Erase(
const key_type& key)
452 return _Validate(CanErase) ? _PrimErase(key) : false;
455 bool _PrimCopy(
const mapped_vector_type& values)
457 typedef std::vector<typename ChildPolicy::ValueType>
460 ChildrenValueVector v;
461 for (
size_t i = 0; i < values.size(); ++i)
462 v.push_back(Adapter::Convert(values[i]));
464 return _view.GetChildren().Copy(v, _type);
467 bool _PrimInsert(
const mapped_type& value,
size_t index)
469 return _view.GetChildren().Insert(
470 Adapter::Convert(value), index, _type);
473 bool _PrimErase(
const key_type& key)
475 return _view.GetChildren().Erase(key, _type);
483 template <
class V>
friend class SdfChildrenProxy;
484 template <
class V>
friend class SdfPyChildrenProxy;
488template <
typename _View>
489struct Tf_ShouldIterateOverCopy<SdfChildrenProxy<_View> > : std::true_type
494template <
class _View>
495struct Vt_DefaultValueFactory<SdfChildrenProxy<_View> > {
496 static Vt_DefaultValueHolder Invoke() =
delete;
499PXR_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.