24 #ifndef PXR_USD_SDF_LIST_PROXY_H 25 #define PXR_USD_SDF_LIST_PROXY_H 31 #include "pxr/usd/sdf/listEditor.h" 32 #include "pxr/usd/sdf/listOp.h" 33 #include "pxr/usd/sdf/path.h" 38 #include <boost/iterator/iterator_facade.hpp> 39 #include <boost/iterator/reverse_iterator.hpp> 40 #include <boost/operators.hpp> 41 #include <boost/optional.hpp> 42 #include <boost/type_traits/is_base_of.hpp> 43 #include <boost/type_traits/remove_cv.hpp> 44 #include <boost/type_traits/remove_reference.hpp> 48 PXR_NAMESPACE_OPEN_SCOPE
58 template <
class _TypePolicy>
60 boost::totally_ordered<SdfListProxy<_TypePolicy>,
61 std::vector<typename _TypePolicy::value_type> > {
63 typedef _TypePolicy TypePolicy;
65 typedef typename TypePolicy::value_type value_type;
66 typedef std::vector<value_type> value_vector_type;
70 class _ItemProxy : boost::totally_ordered<_ItemProxy> {
72 explicit _ItemProxy(
This* owner,
size_t index) :
73 _owner(owner), _index(index)
78 _ItemProxy&
operator=(
const _ItemProxy& x) {
79 _owner->_Edit(_index, 1, value_vector_type(1, x));
83 _ItemProxy&
operator=(
const value_type& x) {
84 _owner->_Edit(_index, 1, value_vector_type(1, x));
88 operator value_type()
const {
89 return _owner->_Get(_index);
93 return _owner->_Get(_index) == x;
96 bool operator<(
const value_type& x)
const {
97 return _owner->_Get(_index) < x;
104 friend class _ItemProxy;
108 typedef _ItemProxy result_type;
110 result_type operator()(
This* owner,
size_t index)
const {
111 return _ItemProxy(owner, index);
114 class _ConstGetHelper {
116 typedef value_type result_type;
118 result_type operator()(
const This* owner,
size_t index)
const {
119 return owner->_Get(index);
122 friend class _GetHelper;
123 friend class _ConstGetHelper;
125 template <
class Owner,
class GetItem>
127 public boost::iterator_facade<
128 _Iterator<Owner, GetItem>,
129 typename boost::remove_cv<
130 typename boost::remove_reference<
131 typename GetItem::result_type
134 std::random_access_iterator_tag,
135 typename GetItem::result_type> {
137 typedef _Iterator<Owner, GetItem>
This;
139 boost::iterator_facade<
140 _Iterator<Owner, GetItem>,
141 typename boost::remove_cv<
142 typename boost::remove_reference<
143 typename GetItem::result_type
146 std::random_access_iterator_tag,
147 typename GetItem::result_type> Parent;
148 typedef typename Parent::reference reference;
149 typedef typename Parent::difference_type difference_type;
151 _Iterator() : _owner(NULL), _index(0)
156 _Iterator(Owner owner,
size_t index) : _owner(owner), _index(index)
162 friend class boost::iterator_core_access;
164 reference dereference()
const {
165 return _getItem(_owner, _index);
168 bool equal(
const This& other)
const {
169 if (_owner != other._owner) {
171 "different proxies!");
174 return _index == other._index;
185 void advance(difference_type n) {
189 difference_type distance_to(
const This& other)
const {
190 return other._index - _index;
200 typedef _ItemProxy reference;
201 typedef _Iterator<This*, _GetHelper> iterator;
202 typedef _Iterator<const This*, _ConstGetHelper> const_iterator;
203 typedef boost::reverse_iterator<iterator> reverse_iterator;
204 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
216 SdfListProxy(
const std::shared_ptr<Sdf_ListEditor<TypePolicy> >& editor,
225 return iterator(_GetThis(), 0);
229 return iterator(_GetThis(), _GetSize());
234 return reverse_iterator(
end());
238 return reverse_iterator(
begin());
243 return const_iterator(_GetThis(), 0);
246 const_iterator
end()
const {
247 return const_iterator(_GetThis(), _GetSize());
252 return const_reverse_iterator(
end());
256 const_reverse_iterator
rend()
const {
257 return const_reverse_iterator(
begin());
262 return _Validate() ? _GetSize() : 0;
272 return reference(_GetThis(), n);
282 return reference(_GetThis(), 0);
287 return reference(_GetThis(), _GetSize() - 1);
297 return _Get(_GetSize() - 1);
302 _Edit(_GetSize(), 0, value_vector_type(1, elem));
307 _Edit(_GetSize() - 1, 1, value_vector_type());
311 iterator
insert(iterator pos,
const value_type& x) {
312 _Edit(pos - iterator(
this, 0), 0, value_vector_type(1, x));
318 template <
class InputIterator>
319 void insert(iterator pos, InputIterator f, InputIterator l) {
320 _Edit(pos - iterator(
this, 0), 0, value_vector_type(f, l));
325 _Edit(pos - iterator(
this, 0), 1, value_vector_type());
329 void erase(iterator f, iterator l) {
330 _Edit(f - iterator(
this, 0), l - f, value_vector_type());
335 _Edit(0, _GetSize(), value_vector_type());
342 void resize(
size_t n,
const value_type& t = value_type()) {
343 size_t s = _GetSize();
345 _Edit(s, 0, value_vector_type(n - s, t));
348 _Edit(n, s - n, value_vector_type());
353 operator value_vector_type()
const {
354 return _listEditor ? _listEditor->GetVector(_op) : value_vector_type();
361 _Edit(0, _GetSize(), static_cast<value_vector_type>(other));
367 _Edit(0, _GetSize(), other);
374 _Edit(0, _GetSize(), value_vector_type(v.begin(), v.end()));
381 return value_vector_type(*
this) == value_vector_type(y);
387 return !(*
this == y);
393 return value_vector_type(*
this) < value_vector_type(y);
399 return value_vector_type(*
this) <= value_vector_type(y);
405 return !(*
this <= y);
416 return value_vector_type(*
this) == y;
421 return value_vector_type(*
this) < y;
426 return value_vector_type(*
this) > y;
431 explicit operator bool()
const 433 return _listEditor && _listEditor->IsValid() && _IsRelevant();
441 return _listEditor ? _listEditor->GetLayer() : SdfLayerHandle();
447 return _listEditor ? _listEditor->GetPath() :
SdfPath();
453 return _listEditor && _listEditor->IsExpired();
456 size_t Count(
const value_type& value)
const 458 return (_Validate() ? _listEditor->Count(_op, value) : 0);
461 size_t Find(
const value_type& value)
const 463 return (_Validate() ? _listEditor->Find(_op, value) : size_t(-1));
466 void Insert(
int index,
const value_type& value)
469 index = static_cast<int>(_GetSize());
471 _Edit(index, 0, value_vector_type(1, value));
474 void Remove(
const value_type& value)
476 size_t index = Find(value);
477 if (index !=
size_t(-1)) {
483 _Edit(_GetSize(), 0, value_vector_type());
487 void Replace(
const value_type& oldValue,
const value_type& newValue)
489 size_t index = Find(oldValue);
490 if (index !=
size_t(-1)) {
491 _Edit(index, 1, value_vector_type(1, newValue));
496 _Edit(_GetSize(), 0, value_vector_type());
500 void Erase(
size_t index)
502 _Edit(index, 1, value_vector_type());
508 if (_Validate() && list._Validate()) {
509 _listEditor->ApplyList(_op, *list._listEditor);
517 _listEditor->ApplyEditsToList(vec);
535 _listEditor->ModifyItemEdits(std::forward<CB>(callback));
553 bool _Validate()
const 568 return _Validate() ? this : NULL;
571 const This* _GetThis()
const 573 return _Validate() ? this : NULL;
576 bool _IsRelevant()
const 578 if (_listEditor->IsExplicit()) {
579 return _op == SdfListOpTypeExplicit;
581 else if (_listEditor->IsOrderedOnly()) {
582 return _op == SdfListOpTypeOrdered;
585 return _op != SdfListOpTypeExplicit;
589 size_t _GetSize()
const 591 return _listEditor ? _listEditor->GetSize(_op) : 0;
594 value_type _Get(
size_t n)
const 596 return _Validate() ? _listEditor->Get(_op, n) : value_type();
599 void _Edit(
size_t index,
size_t n,
const value_vector_type& elems)
604 if (n == 0 && elems.empty()) {
605 SdfAllowed canEdit = _listEditor->PermissionToEdit(_op);
614 _listEditor->ReplaceEdits(_op, index, n, elems);
622 std::shared_ptr<Sdf_ListEditor<TypePolicy> > _listEditor;
625 template <
class>
friend class SdfPyWrapListProxy;
629 template <
typename T>
630 struct Tf_ShouldIterateOverCopy<
SdfListProxy<T> > : boost::true_type
634 PXR_NAMESPACE_CLOSE_SCOPE
636 #endif // PXR_USD_SDF_LIST_PROXY_H reference operator[](size_t n)
Return a reference to the item at index n.
const_reverse_iterator rend() const
Return a const reverse iterator past the start item of the sequence.
void clear()
Clear the contents of the sequence.
SdfPath GetPath() const
Returns the path to this list editor's value.
This & operator=(const value_vector_type &other)
Replace all elements in this sequence with the given vector.
void resize(size_t n, const value_type &t=value_type())
Resize the contents of the sequence.
bool empty() const
Return true if size() == 0.
reference back()
Return a reference to the item at the back of the sequence.
This & operator=(const SdfListProxy< T2 > &other)
Replace all elements in this sequence with the elements in the other sequence.
void push_back(const value_type &elem)
Append elem to this sequence.
value_type back() const
Return a copy of the item at the back of the sequence.
A simple iterator adapter for STL containers.
reverse_iterator rbegin()
Return a reverse iterator to the last item of the sequence.
bool operator==(const SdfListProxy< T2 > &y) const
Equality comparison.
This & operator=(const std::vector< Y > &v)
Replace all elements in this sequence with the given vector.
reference front()
Return a reference to the item at the front of the sequence.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
bool operator<(const value_vector_type &y) const
Less-than comparison.
value_type operator[](size_t n) const
Return a copy of the item at index n.
bool IsExpired() const
Returns true if the list editor is expired.
Low-level utilities for informing users of various internal and external diagnostic conditions.
SDF_API const std::string & GetWhyNot() const
Returns the reason why the operation is not allowed.
bool operator>=(const SdfListProxy< T2 > &y) const
Greater-than-or-equal comparison.
SdfLayerHandle GetLayer() const
Returns the layer that this list editor belongs to.
value_type front() const
Return a copy of the item at the front of the sequence.
bool operator>(const value_vector_type &y) const
Greater-than comparison.
void erase(iterator pos)
Erase the element at pos.
void pop_back()
Remove the last element from this sequence.
iterator begin()
Return an iterator to the start of the sequence.
SdfListProxy(const std::shared_ptr< Sdf_ListEditor< TypePolicy > > &editor, SdfListOpType op)
Create a new proxy wrapping the list operation vector specified by op in the underlying listEditor.
void ModifyItemEdits(CB callback)
Modify all edits in this list.
Indicates if an operation is allowed and, if not, why not.
Represents a single list of list editing operations.
void ApplyList(const SdfListProxy &list)
Applies the edits in the given list to this one.
bool operator<(const SdfListProxy< T2 > &y) const
Less-than comparison.
void insert(iterator pos, InputIterator f, InputIterator l)
Insert copies of the elements in [f, l) into this sequence starting at position pos.
iterator end()
Return an iterator to the end of the sequence.
A path value used to locate objects in layers or scenegraphs.
size_t size() const
Return the size of the sequence.
bool operator>(const SdfListProxy< T2 > &y) const
Greater-than comparison.
iterator insert(iterator pos, const value_type &x)
Insert x into this sequence at position pos.
bool operator<=(const SdfListProxy< T2 > &y) const
Less-than-or-equal comparison.
const_iterator begin() const
Return a const iterator to the start of the sequence.
const_iterator end() const
Return a const iterator to the end of the sequence.
bool operator!=(const SdfListProxy< T2 > &y) const
Inequality comparison.
void erase(iterator f, iterator l)
Erase all the elements in the range [f, l).
void ApplyEditsToList(value_vector_type *vec)
Apply the edits in this list to the given vec.
bool operator==(const value_vector_type &y) const
Equality comparison.
reverse_iterator rend()
Return a reverse iterator past the start item of the sequence.
const_reverse_iterator rbegin() const
Return a const reverse iterator to the last item of the sequence.
SdfListProxy(SdfListOpType op)
Creates a default list proxy object for list operation vector specified op.