forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listProxy.h
694 lines (588 loc) · 19.6 KB
/
listProxy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_SDF_LIST_PROXY_H
#define PXR_USD_SDF_LIST_PROXY_H
/// \file sdf/listProxy.h
#include "pxr/pxr.h"
#include "pxr/usd/sdf/allowed.h"
#include "pxr/usd/sdf/listEditor.h"
#include "pxr/usd/sdf/listOp.h"
#include "pxr/usd/sdf/path.h"
#include "pxr/base/tf/diagnostic.h"
#include "pxr/base/tf/errorMark.h"
#include "pxr/base/tf/iterator.h"
#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/reverse_iterator.hpp>
#include <boost/optional.hpp>
#include <memory>
#include <type_traits>
PXR_NAMESPACE_OPEN_SCOPE
/// \class SdfListProxy
///
/// Represents a single list of list editing operations.
///
/// An SdfListProxy represents a single list of list editing operations, making
/// it look like an STL vector (modeling a random access container and back
/// insertion sequence).
///
template <class _TypePolicy>
class SdfListProxy {
public:
typedef _TypePolicy TypePolicy;
typedef SdfListProxy<TypePolicy> This;
typedef typename TypePolicy::value_type value_type;
typedef std::vector<value_type> value_vector_type;
private:
// Proxies an item in a list editor list.
class _ItemProxy {
public:
explicit _ItemProxy(This* owner, size_t index) :
_owner(owner), _index(index)
{
// Do nothing
}
_ItemProxy& operator=(const _ItemProxy& x) {
_owner->_Edit(_index, 1, value_vector_type(1, x));
return *this;
}
_ItemProxy& operator=(const value_type& x) {
_owner->_Edit(_index, 1, value_vector_type(1, x));
return *this;
}
operator value_type() const {
return _owner->_Get(_index);
}
// Operators rely on implicit conversion to value_type
// for comparing two _ItemProxy instances
bool operator==(const value_type& x) const {
return _owner->_Get(_index) == x;
}
bool operator!=(const value_type& x) const {
return !(*this == x);
}
bool operator<(const value_type& x) const {
return _owner->_Get(_index) < x;
}
bool operator>(const value_type& x) const {
return x < value_type(*this);
}
bool operator>=(const value_type& x) const {
return !(*this < x);
}
bool operator<=(const value_type& x) const {
return !(x < value_type(*this));
}
private:
This* _owner;
size_t _index;
};
friend class _ItemProxy;
class _GetHelper {
public:
typedef _ItemProxy result_type;
result_type operator()(This* owner, size_t index) const {
return _ItemProxy(owner, index);
}
};
class _ConstGetHelper {
public:
typedef value_type result_type;
result_type operator()(const This* owner, size_t index) const {
return owner->_Get(index);
}
};
friend class _GetHelper;
friend class _ConstGetHelper;
template <class Owner, class GetItem>
class _Iterator :
public boost::iterator_facade<
_Iterator<Owner, GetItem>,
std::remove_cv_t<
std::remove_reference_t<
typename GetItem::result_type
>
>,
std::random_access_iterator_tag,
typename GetItem::result_type> {
public:
typedef _Iterator<Owner, GetItem> This;
typedef
boost::iterator_facade<
_Iterator<Owner, GetItem>,
std::remove_cv_t<
std::remove_reference_t<
typename GetItem::result_type
>
>,
std::random_access_iterator_tag,
typename GetItem::result_type> Parent;
typedef typename Parent::reference reference;
typedef typename Parent::difference_type difference_type;
_Iterator() : _owner(NULL), _index(0)
{
// Do nothing
}
_Iterator(Owner owner, size_t index) : _owner(owner), _index(index)
{
// Do nothing
}
private:
friend class boost::iterator_core_access;
reference dereference() const {
return _getItem(_owner, _index);
}
bool equal(const This& other) const {
if (_owner != other._owner) {
TF_CODING_ERROR("Comparing SdfListProxy iterators from "
"different proxies!");
return false;
}
return _index == other._index;
}
void increment() {
++_index;
}
void decrement() {
--_index;
}
void advance(difference_type n) {
_index += n;
}
difference_type distance_to(const This& other) const {
return other._index - _index;
}
private:
GetItem _getItem;
Owner _owner;
size_t _index;
};
public:
typedef _ItemProxy reference;
typedef _Iterator<This*, _GetHelper> iterator;
typedef _Iterator<const This*, _ConstGetHelper> const_iterator;
typedef boost::reverse_iterator<iterator> reverse_iterator;
typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
/// Creates a default list proxy object for list operation vector specified
/// \p op. This object evaluates to false in a boolean context and all
/// operations on this object have no effect.
SdfListProxy(SdfListOpType op) :
_op(op)
{
}
/// Create a new proxy wrapping the list operation vector specified by
/// \p op in the underlying \p listEditor.
SdfListProxy(const std::shared_ptr<Sdf_ListEditor<TypePolicy> >& editor,
SdfListOpType op) :
_listEditor(editor),
_op(op)
{
}
/// Return an iterator to the start of the sequence.
iterator begin() {
return iterator(_GetThis(), 0);
}
/// Return an iterator to the end of the sequence.
iterator end() {
return iterator(_GetThis(), _GetSize());
}
/// Return a reverse iterator to the last item of the sequence.
reverse_iterator rbegin() {
return reverse_iterator(end());
}
/// Return a reverse iterator past the start item of the sequence.
reverse_iterator rend() {
return reverse_iterator(begin());
}
/// Return a const iterator to the start of the sequence.
const_iterator begin() const {
return const_iterator(_GetThis(), 0);
}
/// Return a const iterator to the end of the sequence.
const_iterator end() const {
return const_iterator(_GetThis(), _GetSize());
}
/// Return a const reverse iterator to the last item of the sequence.
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
/// Return a const reverse iterator past the start item of the
/// sequence.
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
/// Return the size of the sequence.
size_t size() const {
return _Validate() ? _GetSize() : 0;
}
/// Return true if size() == 0.
bool empty() const {
return size() == 0;
}
/// Return a \p reference to the item at index \p n.
reference operator[](size_t n) {
return reference(_GetThis(), n);
}
/// Return a copy of the item at index \p n.
value_type operator[](size_t n) const {
return _Get(n);
}
/// Return a \p reference to the item at the front of the sequence.
reference front() {
return reference(_GetThis(), 0);
}
/// Return a \p reference to the item at the back of the sequence.
reference back() {
return reference(_GetThis(), _GetSize() - 1);
}
/// Return a copy of the item at the front of the sequence.
value_type front() const {
return _Get(0);
}
/// Return a copy of the item at the back of the sequence.
value_type back() const {
return _Get(_GetSize() - 1);
}
/// Append \p elem to this sequence.
void push_back(const value_type& elem) {
_Edit(_GetSize(), 0, value_vector_type(1, elem));
}
/// Remove the last element from this sequence.
void pop_back() {
_Edit(_GetSize() - 1, 1, value_vector_type());
}
/// Insert \p x into this sequence at position \p pos.
iterator insert(iterator pos, const value_type& x) {
_Edit(pos - iterator(this, 0), 0, value_vector_type(1, x));
return pos;
}
/// Insert copies of the elements in [\p f, \p l) into this sequence
/// starting at position \p pos.
template <class InputIterator>
void insert(iterator pos, InputIterator f, InputIterator l) {
_Edit(pos - iterator(this, 0), 0, value_vector_type(f, l));
}
/// Erase the element at \p pos.
void erase(iterator pos) {
_Edit(pos - iterator(this, 0), 1, value_vector_type());
}
/// Erase all the elements in the range [\p f, \p l).
void erase(iterator f, iterator l) {
_Edit(f - iterator(this, 0), l - f, value_vector_type());
}
/// Clear the contents of the sequence.
void clear() {
_Edit(0, _GetSize(), value_vector_type());
}
/// Resize the contents of the sequence.
///
/// Inserts or erases copies of \p t at the end
/// such that the size becomes \p n.
void resize(size_t n, const value_type& t = value_type()) {
size_t s = _GetSize();
if (n > s) {
_Edit(s, 0, value_vector_type(n - s, t));
}
else if (n < s) {
_Edit(n, s - n, value_vector_type());
}
}
/// Produce a copy of the contents of this sequence into a vector.
operator value_vector_type() const {
return _listEditor ? _listEditor->GetVector(_op) : value_vector_type();
}
/// Replace all elements in this sequence with the elements in
/// the \p other sequence.
template <class T2>
This& operator=(const SdfListProxy<T2>& other) {
_Edit(0, _GetSize(), static_cast<value_vector_type>(other));
return *this;
}
/// Replace all elements in this sequence with the given vector.
This& operator=(const value_vector_type& other) {
_Edit(0, _GetSize(), other);
return *this;
}
/// Replace all elements in this sequence with the given vector.
template <class Y>
This& operator=(const std::vector<Y>& v) {
_Edit(0, _GetSize(), value_vector_type(v.begin(), v.end()));
return *this;
}
/// Equality comparison.
template <class T2>
bool operator==(const SdfListProxy<T2>& y) const {
return value_vector_type(*this) == value_vector_type(y);
}
/// Inequality comparison.
template <class T2>
bool operator!=(const SdfListProxy<T2>& y) const {
return !(*this == y);
}
/// Less-than comparison.
template <class T2>
bool operator<(const SdfListProxy<T2>& y) const {
return value_vector_type(*this) < value_vector_type(y);
}
/// Less-than-or-equal comparison.
template <class T2>
bool operator<=(const SdfListProxy<T2>& y) const {
return value_vector_type(*this) <= value_vector_type(y);
}
/// Greater-than comparison.
template <class T2>
bool operator>(const SdfListProxy<T2>& y) const {
return !(*this <= y);
}
/// Greater-than-or-equal comparison.
template <class T2>
bool operator>=(const SdfListProxy<T2>& y) const {
return !(*this < y);
}
/// Equality comparison.
bool operator==(const value_vector_type& y) const {
return value_vector_type(*this) == y;
}
/// Equality comparision
friend bool operator==(const value_vector_type& x, const SdfListProxy& y) {
return y == x;
}
/// Inequality comparison.
bool operator!=(const value_vector_type& y) const {
return !(*this == y);
}
/// Inequality comparision
friend bool operator!=(const value_vector_type& x, const SdfListProxy& y) {
return y != x;
}
/// Less-than comparison.
bool operator<(const value_vector_type& y) const {
return value_vector_type(*this) < y;
}
/// Less-than comparison
friend bool operator<(const value_vector_type& x, const SdfListProxy& y) {
return x < value_vector_type(y);
}
/// Greater-than comparison.
bool operator>(const value_vector_type& y) const {
return value_vector_type(*this) > y;
}
/// Greater-than comparison.
friend bool operator>(const value_vector_type& x, const SdfListProxy& y) {
return x > value_vector_type(y);
}
/// Less-than or equal to comparison.
bool operator<=(const value_vector_type& y) const {
return !(*this > y);
}
/// Less-than or equal to comparison.
friend bool operator<=(const value_vector_type& x, const SdfListProxy& y) {
return x <= value_vector_type(y);
}
/// Greater-than or equal to comparison.
bool operator>=(const value_vector_type& y) const {
return !(*this < y);
}
/// Greater-than or equal to comparison.
friend bool operator>=(const value_vector_type& x, const SdfListProxy& y) {
return x >= value_vector_type(y);
}
/// Explicit bool conversion operator. The list proxy object converts to
/// \c true if the list editor is valid, \c false otherwise.
explicit operator bool() const
{
return _listEditor && _listEditor->IsValid() && _IsRelevant();
}
// Extensions
/// Returns the layer that this list editor belongs to.
SdfLayerHandle GetLayer() const
{
return _listEditor ? _listEditor->GetLayer() : SdfLayerHandle();
}
/// Returns the path to this list editor's value.
SdfPath GetPath() const
{
return _listEditor ? _listEditor->GetPath() : SdfPath();
}
/// Returns true if the list editor is expired.
bool IsExpired() const
{
return _listEditor && _listEditor->IsExpired();
}
size_t Count(const value_type& value) const
{
return (_Validate() ? _listEditor->Count(_op, value) : 0);
}
size_t Find(const value_type& value) const
{
return (_Validate() ? _listEditor->Find(_op, value) : size_t(-1));
}
void Insert(int index, const value_type& value)
{
if (index == -1) {
index = static_cast<int>(_GetSize());
}
_Edit(index, 0, value_vector_type(1, value));
}
void Remove(const value_type& value)
{
size_t index = Find(value);
if (index != size_t(-1)) {
Erase(index);
}
else {
// Allow policy to raise an error even though we're not
// doing anything.
_Edit(_GetSize(), 0, value_vector_type());
}
}
void Replace(const value_type& oldValue, const value_type& newValue)
{
size_t index = Find(oldValue);
if (index != size_t(-1)) {
_Edit(index, 1, value_vector_type(1, newValue));
}
else {
// Allow policy to raise an error even though we're not
// doing anything.
_Edit(_GetSize(), 0, value_vector_type());
}
}
void Erase(size_t index)
{
_Edit(index, 1, value_vector_type());
}
/// Applies the edits in the given list to this one.
void ApplyList(const SdfListProxy &list)
{
if (_Validate() && list._Validate()) {
_listEditor->ApplyList(_op, *list._listEditor);
}
}
/// Apply the edits in this list to the given \p vec.
void ApplyEditsToList(value_vector_type* vec)
{
if (_Validate()) {
_listEditor->ApplyEditsToList(vec);
}
}
/// Modify all edits in this list.
///
/// \p callback must be a callable that accepts an argument of type
/// value_type and returns a boost::optional<value_type>.
///
/// \p callback is called with every item in the list. If an invalid
/// boost::optional is returned, the item is removed. Otherwise it's
/// replaced with the returned item. If a returned item matches an
/// item that was previously returned, the returned item will be
/// removed.
template <class CB>
void ModifyItemEdits(CB callback)
{
if (_Validate()) {
_listEditor->ModifyItemEdits(std::forward<CB>(callback));
}
}
private:
bool _Validate()
{
if (!_listEditor) {
return false;
}
if (IsExpired()) {
TF_CODING_ERROR("Accessing expired list editor");
return false;
}
return true;
}
bool _Validate() const
{
if (!_listEditor) {
return false;
}
if (IsExpired()) {
TF_CODING_ERROR("Accessing expired list editor");
return false;
}
return true;
}
This* _GetThis()
{
return _Validate() ? this : NULL;
}
const This* _GetThis() const
{
return _Validate() ? this : NULL;
}
bool _IsRelevant() const
{
if (_listEditor->IsExplicit()) {
return _op == SdfListOpTypeExplicit;
}
else if (_listEditor->IsOrderedOnly()) {
return _op == SdfListOpTypeOrdered;
}
else {
return _op != SdfListOpTypeExplicit;
}
}
size_t _GetSize() const
{
return _listEditor ? _listEditor->GetSize(_op) : 0;
}
value_type _Get(size_t n) const
{
return _Validate() ? _listEditor->Get(_op, n) : value_type();
}
void _Edit(size_t index, size_t n, const value_vector_type& elems)
{
if (_Validate()) {
// Allow policy to raise an error even if we're not
// doing anything.
if (n == 0 && elems.empty()) {
SdfAllowed canEdit = _listEditor->PermissionToEdit(_op);
if (!canEdit) {
TF_CODING_ERROR("Editing list: %s",
canEdit.GetWhyNot().c_str());
}
return;
}
bool valid =
_listEditor->ReplaceEdits(_op, index, n, elems);
if (!valid) {
TF_CODING_ERROR("Inserting invalid value into list editor");
}
}
}
private:
std::shared_ptr<Sdf_ListEditor<TypePolicy> > _listEditor;
SdfListOpType _op;
template <class> friend class SdfPyWrapListProxy;
};
// Allow TfIteration over list proxies.
template <typename T>
struct Tf_ShouldIterateOverCopy<SdfListProxy<T> > : std::true_type
{
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_SDF_LIST_PROXY_H