24#ifndef PXR_USD_SDF_LIST_EDITOR_PROXY_H
25#define PXR_USD_SDF_LIST_EDITOR_PROXY_H
30#include "pxr/usd/sdf/listEditor.h"
32#include "pxr/usd/sdf/changeBlock.h"
34#include "pxr/base/vt/value.h"
40PXR_NAMESPACE_OPEN_SCOPE
55template <
class _TypePolicy>
58 typedef _TypePolicy TypePolicy;
61 typedef typename TypePolicy::value_type value_type;
62 typedef std::vector<value_type> value_vector_type;
65 typedef std::function<std::optional<value_type>
66 (SdfListOpType,
const value_type&)> ApplyCallback;
69 typedef std::function<std::optional<value_type>
70 (
const value_type&)> ModifyCallback;
80 const std::shared_ptr<Sdf_ListEditor<TypePolicy> >& listEditor)
81 : _listEditor(listEditor)
92 return _listEditor->IsExpired();
99 return _Validate() ? _listEditor->IsExplicit() :
true;
106 return _Validate() ? _listEditor->IsOrderedOnly() :
false;
114 return _Validate() ? _listEditor->HasKeys() :
true;
121 _listEditor->ApplyEditsToList(vec, ApplyCallback());
134 _listEditor->ApplyEditsToList(vec, ApplyCallback(callback));
147 return _Validate() && other._Validate() ?
148 _listEditor->CopyEdits(*other._listEditor) :
false;
158 return _Validate() ? _listEditor->ClearEdits() :
false;
168 return _Validate() ? _listEditor->ClearEditsAndMakeExplicit() :
false;
178 _listEditor->ModifyItemEdits(ModifyCallback(callback));
186 bool onlyAddOrExplicit =
false)
const
192 if (i !=
size_t(-1)) {
197 if (i !=
size_t(-1)) {
202 if (i !=
size_t(-1)) {
207 if (i !=
size_t(-1)) {
211 if (!onlyAddOrExplicit) {
213 if (i !=
size_t(-1)) {
218 if (i !=
size_t(-1)) {
263 return ListProxy(_listEditor, SdfListOpTypeExplicit);
269 return ListProxy(_listEditor, SdfListOpTypeAdded);
275 return ListProxy(_listEditor, SdfListOpTypePrepended);
281 return ListProxy(_listEditor, SdfListOpTypeAppended);
287 return ListProxy(_listEditor, SdfListOpTypeDeleted);
293 return ListProxy(_listEditor, SdfListOpTypeOrdered);
311 value_vector_type result;
313 _listEditor->ApplyEditsToList(&result);
318 void Add(
const value_type& value)
321 if (!_listEditor->IsOrderedOnly()) {
322 if (_listEditor->IsExplicit()) {
323 _AddOrReplace(SdfListOpTypeExplicit, value);
327 _AddOrReplace(SdfListOpTypeAdded, value);
333 void Prepend(
const value_type& value)
336 if (!_listEditor->IsOrderedOnly()) {
337 if (_listEditor->IsExplicit()) {
338 _Prepend(SdfListOpTypeExplicit, value);
342 _Prepend(SdfListOpTypePrepended, value);
348 void Append(
const value_type& value)
351 if (!_listEditor->IsOrderedOnly()) {
352 if (_listEditor->IsExplicit()) {
353 _Append(SdfListOpTypeExplicit, value);
357 _Append(SdfListOpTypeAppended, value);
363 void Remove(
const value_type& value)
366 if (_listEditor->IsExplicit()) {
369 else if (!_listEditor->IsOrderedOnly()) {
373 _AddIfMissing(SdfListOpTypeDeleted, value);
378 void Erase(
const value_type& value)
381 if (!_listEditor->IsOrderedOnly()) {
382 if (_listEditor->IsExplicit()) {
397 explicit operator bool()
const
399 return _listEditor && _listEditor->IsValid();
416 bool _Validate()
const
429 void _AddIfMissing(SdfListOpType op,
const value_type& value)
431 ListProxy proxy(_listEditor, op);
432 size_t index = proxy.Find(value);
433 if (index ==
size_t(-1)) {
434 proxy.push_back(value);
438 void _AddOrReplace(SdfListOpType op,
const value_type& value)
440 ListProxy proxy(_listEditor, op);
441 size_t index = proxy.Find(value);
442 if (index ==
size_t(-1)) {
443 proxy.push_back(value);
445 else if (value !=
static_cast<value_type
>(proxy[index])) {
446 proxy[index] = value;
450 void _Prepend(SdfListOpType op,
const value_type& value)
452 ListProxy proxy(_listEditor, op);
453 size_t index = proxy.Find(value);
455 if (index !=
size_t(-1)) {
458 proxy.insert(proxy.begin(), value);
462 void _Append(SdfListOpType op,
const value_type& value)
464 ListProxy proxy(_listEditor, op);
465 size_t index = proxy.Find(value);
466 if (proxy.empty() || (index != proxy.size()-1)) {
467 if (index !=
size_t(-1)) {
470 proxy.push_back(value);
475 std::shared_ptr<Sdf_ListEditor<TypePolicy> > _listEditor;
477 friend class Sdf_ListEditorProxyAccess;
478 template <
class T>
friend class SdfPyWrapListEditorProxy;
484 static Vt_DefaultValueHolder Invoke() =
delete;
487PXR_NAMESPACE_CLOSE_SCOPE
Represents a set of list editing operations.
bool CopyItems(const This &other)
Copies the keys from other.
ListProxy GetPrependedItems() const
Returns the items prepended by this list editor.
ListProxy GetAppendedItems() const
Returns the items appended by this list editor.
ListProxy GetAddedItems() const
Returns the items added by this list editor.
void ApplyEditsToList(value_vector_type *vec) const
Apply the edits to vec.
bool ClearEdits()
Removes all keys and changes the editor to have list operations.
bool ContainsItemEdit(const value_type &item, bool onlyAddOrExplicit=false) const
Check if the given item is explicit, added, prepended, appended, deleted, or ordered by this editor.
void RemoveItemEdits(const value_type &item)
Remove all occurrences of the given item, regardless of whether the item is explicit,...
bool IsExplicit() const
Returns true if the editor has an explicit list, false if it has list operations.
ListProxy GetOrderedItems() const
Returns the items reordered by this list editor.
void ModifyItemEdits(CB callback)
callback is called for every key.
void ReplaceItemEdits(const value_type &oldItem, const value_type &newItem)
Replace all occurrences of the given item, regardless of whether the item is explicit,...
bool ClearEditsAndMakeExplicit()
Removes all keys and changes the editor to be explicit.
void ApplyEditsToList(value_vector_type *vec, CB callback) const
Apply the edits to vec.
bool IsOrderedOnly() const
Returns true if the editor is not explicit and allows ordering only.
SdfListEditorProxy()
Creates a default proxy object.
bool IsExpired() const
Returns true if the list editor is expired.
bool HasKeys() const
Returns true if the editor has an explicit list (even if it's empty) or it has any added,...
value_vector_type GetAddedOrExplicitItems() const
Deprecated. Please use GetAppliedItems.
value_vector_type GetAppliedItems() const
Returns the effective list of items represented by the operations in this list op.
ListProxy GetExplicitItems() const
Returns the explicitly set items.
ListProxy GetDeletedItems() const
Returns the items deleted by this list editor.
SdfListEditorProxy(const std::shared_ptr< Sdf_ListEditor< TypePolicy > > &listEditor)
Creates a new proxy object backed by the supplied list editor.
Represents a single list of list editing operations.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.