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"
36#include <boost/optional.hpp>
41PXR_NAMESPACE_OPEN_SCOPE
56template <
class _TypePolicy>
59 typedef _TypePolicy TypePolicy;
62 typedef typename TypePolicy::value_type value_type;
63 typedef std::vector<value_type> value_vector_type;
66 typedef std::function<boost::optional<value_type>
67 (SdfListOpType,
const value_type&)> ApplyCallback;
70 typedef std::function<boost::optional<value_type>
71 (
const value_type&)> ModifyCallback;
81 const std::shared_ptr<Sdf_ListEditor<TypePolicy> >& listEditor)
82 : _listEditor(listEditor)
93 return _listEditor->IsExpired();
100 return _Validate() ? _listEditor->IsExplicit() :
true;
107 return _Validate() ? _listEditor->IsOrderedOnly() :
false;
115 return _Validate() ? _listEditor->HasKeys() :
true;
122 _listEditor->ApplyEditsToList(vec, ApplyCallback());
135 _listEditor->ApplyEditsToList(vec, ApplyCallback(callback));
148 return _Validate() && other._Validate() ?
149 _listEditor->CopyEdits(*other._listEditor) :
false;
159 return _Validate() ? _listEditor->ClearEdits() :
false;
169 return _Validate() ? _listEditor->ClearEditsAndMakeExplicit() :
false;
179 _listEditor->ModifyItemEdits(ModifyCallback(callback));
187 bool onlyAddOrExplicit =
false)
const
193 if (i !=
size_t(-1)) {
198 if (i !=
size_t(-1)) {
203 if (i !=
size_t(-1)) {
208 if (i !=
size_t(-1)) {
212 if (!onlyAddOrExplicit) {
214 if (i !=
size_t(-1)) {
219 if (i !=
size_t(-1)) {
264 return ListProxy(_listEditor, SdfListOpTypeExplicit);
270 return ListProxy(_listEditor, SdfListOpTypeAdded);
276 return ListProxy(_listEditor, SdfListOpTypePrepended);
282 return ListProxy(_listEditor, SdfListOpTypeAppended);
288 return ListProxy(_listEditor, SdfListOpTypeDeleted);
294 return ListProxy(_listEditor, SdfListOpTypeOrdered);
312 value_vector_type result;
314 _listEditor->ApplyEditsToList(&result);
319 void Add(
const value_type& value)
322 if (!_listEditor->IsOrderedOnly()) {
323 if (_listEditor->IsExplicit()) {
324 _AddOrReplace(SdfListOpTypeExplicit, value);
328 _AddOrReplace(SdfListOpTypeAdded, value);
334 void Prepend(
const value_type& value)
337 if (!_listEditor->IsOrderedOnly()) {
338 if (_listEditor->IsExplicit()) {
339 _Prepend(SdfListOpTypeExplicit, value);
343 _Prepend(SdfListOpTypePrepended, value);
349 void Append(
const value_type& value)
352 if (!_listEditor->IsOrderedOnly()) {
353 if (_listEditor->IsExplicit()) {
354 _Append(SdfListOpTypeExplicit, value);
358 _Append(SdfListOpTypeAppended, value);
364 void Remove(
const value_type& value)
367 if (_listEditor->IsExplicit()) {
370 else if (!_listEditor->IsOrderedOnly()) {
374 _AddIfMissing(SdfListOpTypeDeleted, value);
379 void Erase(
const value_type& value)
382 if (!_listEditor->IsOrderedOnly()) {
383 if (_listEditor->IsExplicit()) {
398 explicit operator bool()
const
400 return _listEditor && _listEditor->IsValid();
417 bool _Validate()
const
430 void _AddIfMissing(SdfListOpType op,
const value_type& value)
432 ListProxy proxy(_listEditor, op);
433 size_t index = proxy.Find(value);
434 if (index ==
size_t(-1)) {
435 proxy.push_back(value);
439 void _AddOrReplace(SdfListOpType op,
const value_type& value)
441 ListProxy proxy(_listEditor, op);
442 size_t index = proxy.Find(value);
443 if (index ==
size_t(-1)) {
444 proxy.push_back(value);
446 else if (value !=
static_cast<value_type
>(proxy[index])) {
447 proxy[index] = value;
451 void _Prepend(SdfListOpType op,
const value_type& value)
453 ListProxy proxy(_listEditor, op);
454 size_t index = proxy.Find(value);
456 if (index !=
size_t(-1)) {
459 proxy.insert(proxy.begin(), value);
463 void _Append(SdfListOpType op,
const value_type& value)
465 ListProxy proxy(_listEditor, op);
466 size_t index = proxy.Find(value);
467 if (proxy.empty() || (index != proxy.size()-1)) {
468 if (index !=
size_t(-1)) {
471 proxy.push_back(value);
476 std::shared_ptr<Sdf_ListEditor<TypePolicy> > _listEditor;
478 friend class Sdf_ListEditorProxyAccess;
479 template <
class T>
friend class SdfPyWrapListEditorProxy;
485 static Vt_DefaultValueHolder Invoke() =
delete;
488PXR_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.