7#ifndef PXR_USD_SDF_LIST_EDITOR_PROXY_H
8#define PXR_USD_SDF_LIST_EDITOR_PROXY_H
13#include "pxr/usd/sdf/listEditor.h"
15#include "pxr/usd/sdf/changeBlock.h"
17#include "pxr/base/vt/value.h"
23PXR_NAMESPACE_OPEN_SCOPE
38template <
class _TypePolicy>
41 typedef _TypePolicy TypePolicy;
44 typedef typename TypePolicy::value_type value_type;
45 typedef std::vector<value_type> value_vector_type;
48 typedef std::function<std::optional<value_type>
49 (SdfListOpType,
const value_type&)> ApplyCallback;
52 typedef std::function<std::optional<value_type>
53 (
const value_type&)> ModifyCallback;
63 const std::shared_ptr<Sdf_ListEditor<TypePolicy> >& listEditor)
64 : _listEditor(listEditor)
75 return _listEditor->IsExpired();
82 return _Validate() ? _listEditor->IsExplicit() :
true;
89 return _Validate() ? _listEditor->IsOrderedOnly() :
false;
97 return _Validate() ? _listEditor->HasKeys() :
true;
104 _listEditor->ApplyEditsToList(vec, ApplyCallback());
117 _listEditor->ApplyEditsToList(vec, ApplyCallback(callback));
130 return _Validate() && other._Validate() ?
131 _listEditor->CopyEdits(*other._listEditor) :
false;
141 return _Validate() ? _listEditor->ClearEdits() :
false;
151 return _Validate() ? _listEditor->ClearEditsAndMakeExplicit() :
false;
161 _listEditor->ModifyItemEdits(ModifyCallback(callback));
169 bool onlyAddOrExplicit =
false)
const
175 if (i !=
size_t(-1)) {
180 if (i !=
size_t(-1)) {
185 if (i !=
size_t(-1)) {
190 if (i !=
size_t(-1)) {
194 if (!onlyAddOrExplicit) {
196 if (i !=
size_t(-1)) {
201 if (i !=
size_t(-1)) {
246 return ListProxy(_listEditor, SdfListOpTypeExplicit);
252 return ListProxy(_listEditor, SdfListOpTypeAdded);
258 return ListProxy(_listEditor, SdfListOpTypePrepended);
264 return ListProxy(_listEditor, SdfListOpTypeAppended);
270 return ListProxy(_listEditor, SdfListOpTypeDeleted);
276 return ListProxy(_listEditor, SdfListOpTypeOrdered);
294 value_vector_type result;
296 _listEditor->ApplyEditsToList(&result);
301 void Add(
const value_type& value)
304 if (!_listEditor->IsOrderedOnly()) {
305 if (_listEditor->IsExplicit()) {
306 _AddOrReplace(SdfListOpTypeExplicit, value);
310 _AddOrReplace(SdfListOpTypeAdded, value);
316 void Prepend(
const value_type& value)
319 if (!_listEditor->IsOrderedOnly()) {
320 if (_listEditor->IsExplicit()) {
321 _Prepend(SdfListOpTypeExplicit, value);
325 _Prepend(SdfListOpTypePrepended, value);
331 void Append(
const value_type& value)
334 if (!_listEditor->IsOrderedOnly()) {
335 if (_listEditor->IsExplicit()) {
336 _Append(SdfListOpTypeExplicit, value);
340 _Append(SdfListOpTypeAppended, value);
346 void Remove(
const value_type& value)
349 if (_listEditor->IsExplicit()) {
352 else if (!_listEditor->IsOrderedOnly()) {
356 _AddIfMissing(SdfListOpTypeDeleted, value);
361 void Erase(
const value_type& value)
364 if (!_listEditor->IsOrderedOnly()) {
365 if (_listEditor->IsExplicit()) {
380 explicit operator bool()
const
382 return _listEditor && _listEditor->IsValid();
399 bool _Validate()
const
412 void _AddIfMissing(SdfListOpType op,
const value_type& value)
414 ListProxy proxy(_listEditor, op);
415 size_t index = proxy.Find(value);
416 if (index ==
size_t(-1)) {
417 proxy.push_back(value);
421 void _AddOrReplace(SdfListOpType op,
const value_type& value)
423 ListProxy proxy(_listEditor, op);
424 size_t index = proxy.Find(value);
425 if (index ==
size_t(-1)) {
426 proxy.push_back(value);
428 else if (value !=
static_cast<value_type
>(proxy[index])) {
429 proxy[index] = value;
433 void _Prepend(SdfListOpType op,
const value_type& value)
435 ListProxy proxy(_listEditor, op);
436 size_t index = proxy.Find(value);
438 if (index !=
size_t(-1)) {
441 proxy.insert(proxy.begin(), value);
445 void _Append(SdfListOpType op,
const value_type& value)
447 ListProxy proxy(_listEditor, op);
448 size_t index = proxy.Find(value);
449 if (proxy.empty() || (index != proxy.size()-1)) {
450 if (index !=
size_t(-1)) {
453 proxy.push_back(value);
458 std::shared_ptr<Sdf_ListEditor<TypePolicy> > _listEditor;
460 friend class Sdf_ListEditorProxyAccess;
461 template <
class T>
friend class SdfPyWrapListEditorProxy;
467 static Vt_DefaultValueHolder Invoke() =
delete;
470PXR_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.
size_t Find(const value_type &value) const
Returns the index of value in the list of operations.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.