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" 36 #include <boost/iterator/iterator_facade.hpp> 37 #include <boost/iterator/reverse_iterator.hpp> 38 #include <boost/operators.hpp> 43 PXR_NAMESPACE_OPEN_SCOPE
45 template <
class _View>
46 class SdfChildrenProxy : boost::equality_comparable<SdfChildrenProxy<_View> > {
49 typedef typename View::Adapter Adapter;
50 typedef typename View::ChildPolicy ChildPolicy;
51 typedef typename View::key_type key_type;
52 typedef typename View::value_type mapped_type;
53 typedef std::vector<mapped_type> mapped_vector_type;
54 typedef std::pair<const key_type, mapped_type> value_type;
55 typedef std::map<key_type, mapped_type> map_type;
56 typedef typename View::size_type size_type;
57 typedef SdfChildrenProxy<View> This;
60 typedef typename View::const_iterator _inner_iterator;
64 _ValueProxy() : _owner(NULL) { }
65 _ValueProxy(This* owner, _inner_iterator i) : _owner(owner), _pos(i)
70 operator mapped_type()
const 76 _ValueProxy& operator=(
const U& x)
78 _owner->_Set(*_pos, x);
82 bool operator==(
const mapped_type& other)
const 84 return *_pos == other;
94 explicit _PairProxy(This* owner, _inner_iterator i) :
95 first(owner->_view.key(i)), second(owner, i) { }
100 operator value_type()
const 102 return value_type(first, second);
105 friend class _PairProxy;
109 static value_type Dereference(
const This* owner, _inner_iterator i)
111 return value_type(owner->_view.key(i), *i);
114 static _PairProxy Dereference(This* owner, _inner_iterator i)
116 return _PairProxy(owner, i);
120 template <
class _Owner,
class _Iter,
class _Value>
122 public boost::iterator_facade<
123 _Iterator<_Owner, _Iter, _Value>,
125 std::bidirectional_iterator_tag,
129 _Iterator(_Owner owner, _inner_iterator i) : _owner(owner), _pos(i) { }
130 template <
class O2,
class I2,
class V2>
131 _Iterator(
const _Iterator<O2, I2, V2>& other) :
132 _owner(other._owner), _pos(other._pos) { }
135 friend class boost::iterator_core_access;
137 _Value dereference()
const 139 return _Traits::Dereference(_owner, _pos);
142 template <
class O2,
class I2,
class V2>
143 bool equal(
const _Iterator<O2, I2, V2>& other)
const 145 return _pos == other._pos;
158 _inner_iterator _pos;
160 template <
class O2,
class I2,
class V2>
161 friend class _Iterator;
165 typedef _ValueProxy reference;
166 typedef _Iterator<This*, _inner_iterator, _PairProxy> iterator;
167 typedef boost::reverse_iterator<iterator> reverse_iterator;
168 typedef _Iterator<const This*, _inner_iterator, value_type> const_iterator;
169 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
171 static const int CanSet = 1;
172 static const int CanInsert = 2;
173 static const int CanErase = 4;
175 SdfChildrenProxy(
const View& view,
const std::string& type,
176 int permission = CanSet | CanInsert | CanErase) :
177 _view(view), _type(type), _permission(permission)
183 SdfChildrenProxy(
const SdfChildrenProxy<U>& other) :
184 _view(other._view), _type(other._type), _permission(other._permission)
189 This& operator=(
const This& other)
191 if (other._Validate()) {
192 _Copy(other._view.values());
198 This& operator=(
const SdfChildrenProxy<U>& other)
200 if (other._Validate()) {
201 _Copy(other._view.values());
206 This& operator=(
const mapped_vector_type& values)
212 operator mapped_vector_type()
const 214 return _Validate() ? _view.values() : mapped_vector_type();
217 map_type items()
const 219 return _Validate() ? _view.template items_as<map_type>() :map_type();
224 return iterator(_GetThis(), _view.begin());
228 return iterator(_GetThis(), _view.end());
230 const_iterator begin()
const 232 return const_iterator(_GetThis(), _view.begin());
234 const_iterator end()
const 236 return const_iterator(_GetThis(), _view.end());
239 reverse_iterator rbegin()
241 return reverse_iterator(end());
243 reverse_iterator rend()
245 return reverse_iterator(begin());
247 const_reverse_iterator rbegin()
const 249 return reverse_iterator(end());
251 const_reverse_iterator rend()
const 253 return reverse_iterator(begin());
256 size_type size()
const 258 return _Validate() ? _view.size() : 0;
261 size_type max_size()
const 263 return _view.max_size();
268 return _Validate() ? _view.empty() :
true;
271 std::pair<iterator, bool> insert(
const mapped_type& value)
273 if (_Validate(CanInsert)) {
274 iterator i = find(_view.key(value));
276 if (_PrimInsert(value, size())) {
277 return std::make_pair(find(_view.key(value)),
true);
280 return std::make_pair(end(),
false);
284 return std::make_pair(i,
false);
288 return std::make_pair(iterator(),
false);
292 iterator insert(iterator pos,
const mapped_type& value)
294 return insert(value).first;
297 template <
class InputIterator>
298 void insert(InputIterator first, InputIterator last)
300 if (_Validate(CanInsert)) {
302 for (; first != last; ++first) {
303 _PrimInsert(*first, size());
308 void erase(iterator pos)
313 size_type erase(
const key_type& key)
315 return _Erase(key) ? 1 : 0;
318 void erase(iterator first, iterator last)
320 if (_Validate(CanErase)) {
322 while (first != last) {
323 const key_type& key = first->first;
332 _Copy(mapped_vector_type());
335 iterator find(
const key_type& key)
337 return _Validate() ? iterator(
this, _view.find(key)) : iterator();
340 const_iterator find(
const key_type& key)
const 342 return _Validate() ? const_iterator(
this, _view.find(key)) :
346 size_type count(
const key_type& key)
const 348 return _Validate() ? _view.count(key) : 0;
353 return _view == other._view;
358 explicit operator bool()
const 360 return _view.IsValid();
364 const std::string& _GetType()
const 369 int _GetPermission()
const 376 return _Validate() ? this : NULL;
379 const This* _GetThis()
const 381 return _Validate() ? this : NULL;
384 bool _Validate()
const 386 if (_view.IsValid()) {
395 bool _Validate(
int permission)
400 if ((_permission & permission) == permission) {
403 const char* op =
"edit";
404 if (~_permission & permission & CanSet) {
407 else if (~_permission & permission & CanInsert) {
410 else if (~_permission & permission & CanErase) {
417 bool _Copy(
const mapped_vector_type& values)
419 return _Validate(CanSet) ? _PrimCopy(values) : false;
422 bool _Insert(
const mapped_type& value,
size_t index)
424 return _Validate(CanInsert) ? _PrimInsert(value, index) : false;
427 bool _Erase(
const key_type& key)
429 return _Validate(CanErase) ? _PrimErase(key) : false;
432 bool _PrimCopy(
const mapped_vector_type& values)
434 typedef std::vector<typename ChildPolicy::ValueType>
437 ChildrenValueVector v;
438 for (
size_t i = 0; i < values.size(); ++i)
439 v.push_back(Adapter::Convert(values[i]));
441 return _view.GetChildren().Copy(v, _type);
444 bool _PrimInsert(
const mapped_type& value,
size_t index)
446 return _view.GetChildren().Insert(
447 Adapter::Convert(value), index, _type);
450 bool _PrimErase(
const key_type& key)
452 return _view.GetChildren().Erase(key, _type);
460 template <
class V>
friend class SdfChildrenProxy;
461 template <
class V>
friend class SdfPyChildrenProxy;
465 template <
typename _View>
466 struct Tf_ShouldIterateOverCopy<SdfChildrenProxy<_View> > : boost::true_type
471 template <
class _View>
472 struct Vt_DefaultValueFactory<SdfChildrenProxy<_View> > {
473 static Vt_DefaultValueHolder Invoke() =
delete;
476 PXR_NAMESPACE_CLOSE_SCOPE
478 #endif // PXR_USD_SDF_CHILDREN_PROXY_H A simple iterator adapter for STL containers.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Low-level utilities for informing users of various internal and external diagnostic conditions.
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.