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> 41 PXR_NAMESPACE_OPEN_SCOPE
56 template <
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);
300 value_vector_type result;
302 _listEditor->ApplyEditsToList(&result);
307 void Add(
const value_type& value)
310 if (!_listEditor->IsOrderedOnly()) {
311 if (_listEditor->IsExplicit()) {
312 _AddOrReplace(SdfListOpTypeExplicit, value);
316 _AddOrReplace(SdfListOpTypeAdded, value);
322 void Prepend(
const value_type& value)
325 if (!_listEditor->IsOrderedOnly()) {
326 if (_listEditor->IsExplicit()) {
327 _Prepend(SdfListOpTypeExplicit, value);
331 _Prepend(SdfListOpTypePrepended, value);
337 void Append(
const value_type& value)
340 if (!_listEditor->IsOrderedOnly()) {
341 if (_listEditor->IsExplicit()) {
342 _Append(SdfListOpTypeExplicit, value);
346 _Append(SdfListOpTypeAppended, value);
352 void Remove(
const value_type& value)
355 if (_listEditor->IsExplicit()) {
358 else if (!_listEditor->IsOrderedOnly()) {
362 _AddIfMissing(SdfListOpTypeDeleted, value);
367 void Erase(
const value_type& value)
370 if (!_listEditor->IsOrderedOnly()) {
371 if (_listEditor->IsExplicit()) {
386 explicit operator bool()
const 388 return _listEditor && _listEditor->IsValid();
405 bool _Validate()
const 418 void _AddIfMissing(SdfListOpType op,
const value_type& value)
420 ListProxy proxy(_listEditor, op);
421 size_t index = proxy.Find(value);
422 if (index ==
size_t(-1)) {
423 proxy.push_back(value);
427 void _AddOrReplace(SdfListOpType op,
const value_type& value)
429 ListProxy proxy(_listEditor, op);
430 size_t index = proxy.Find(value);
431 if (index ==
size_t(-1)) {
432 proxy.push_back(value);
434 else if (value != static_cast<value_type>(proxy[index])) {
435 proxy[index] = value;
439 void _Prepend(SdfListOpType op,
const value_type& value)
441 ListProxy proxy(_listEditor, op);
442 size_t index = proxy.Find(value);
444 if (index !=
size_t(-1)) {
447 proxy.insert(proxy.begin(), value);
451 void _Append(SdfListOpType op,
const value_type& value)
453 ListProxy proxy(_listEditor, op);
454 size_t index = proxy.Find(value);
455 if (proxy.empty() || (index != proxy.size()-1)) {
456 if (index !=
size_t(-1)) {
459 proxy.push_back(value);
464 std::shared_ptr<Sdf_ListEditor<TypePolicy> > _listEditor;
466 friend class Sdf_ListEditorProxyAccess;
467 template <
class T>
friend class SdfPyWrapListEditorProxy;
473 static Vt_DefaultValueHolder Invoke() =
delete;
476 PXR_NAMESPACE_CLOSE_SCOPE
478 #endif // PXR_USD_SDF_LIST_EDITOR_PROXY_H void RemoveItemEdits(const value_type &item)
Remove all occurrences of the given item, regardless of whether the item is explicit,...
bool ClearEdits()
Removes all keys and changes the editor to have list operations.
bool HasKeys() const
Returns true if the editor has an explicit list (even if it's empty) or it has any added,...
ListProxy GetOrderedItems() const
Returns the items reordered by this list editor.
void ApplyEditsToList(value_vector_type *vec, CB callback) const
Apply the edits to vec.
Represents a set of list editing operations.
value_vector_type GetAddedOrExplicitItems() const
Returns the added or explicitly set items.
ListProxy GetAddedItems() const
Returns the items added by this list editor.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
SdfListEditorProxy(const std::shared_ptr< Sdf_ListEditor< TypePolicy > > &listEditor)
Creates a new proxy object backed by the supplied list editor.
bool IsExplicit() const
Returns true if the editor has an explicit list, false if it has list operations.
bool IsOrderedOnly() const
Returns true if the editor is not explicit and allows ordering only.
bool IsExpired() const
Returns true if the list editor is expired.
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.
Represents a single list of list editing operations.
void ReplaceItemEdits(const value_type &oldItem, const value_type &newItem)
Replace all occurrences of the given item, regardless of whether the item is explicit,...
ListProxy GetDeletedItems() const
Returns the items deleted by this list editor.
bool CopyItems(const This &other)
Copies the keys from other.
void ApplyEditsToList(value_vector_type *vec) const
Apply the edits to vec.
bool ClearEditsAndMakeExplicit()
Removes all keys and changes the editor to be explicit.
ListProxy GetExplicitItems() const
Returns the explicitly set items.
void ModifyItemEdits(CB callback)
callback is called for every key.
ListProxy GetAppendedItems() const
Returns the items appended by this list editor.
SdfListEditorProxy()
Creates a default proxy object.
ListProxy GetPrependedItems() const
Returns the items prepended by this list editor.