7#ifndef PXR_USD_SDF_LIST_EDITOR_H
8#define PXR_USD_SDF_LIST_EDITOR_H
14#include "pxr/usd/sdf/listOp.h"
15#include "pxr/usd/sdf/path.h"
16#include "pxr/usd/sdf/schema.h"
22PXR_NAMESPACE_OPEN_SCOPE
32template <
class TypePolicy>
35 Sdf_ListEditor(
const Sdf_ListEditor&) =
delete;
36 Sdf_ListEditor& operator=(
const Sdf_ListEditor&) =
delete;
38 typedef Sdf_ListEditor<TypePolicy> This;
41 typedef typename TypePolicy::value_type value_type;
42 typedef std::vector<value_type> value_vector_type;
44 virtual ~Sdf_ListEditor() =
default;
46 SdfLayerHandle GetLayer()
const
48 return _owner ? _owner->GetLayer() : SdfLayerHandle();
53 return _owner ? _owner->GetPath() :
SdfPath();
61 bool IsExpired()
const
71 else if (IsOrderedOnly()) {
72 return !_GetOperations(SdfListOpTypeOrdered).empty();
75 return (!_GetOperations(SdfListOpTypeAdded).empty() ||
76 !_GetOperations(SdfListOpTypePrepended).empty() ||
77 !_GetOperations(SdfListOpTypeAppended).empty() ||
78 !_GetOperations(SdfListOpTypeDeleted).empty() ||
79 !_GetOperations(SdfListOpTypeOrdered).empty());
83 virtual bool IsExplicit()
const = 0;
84 virtual bool IsOrderedOnly()
const = 0;
86 virtual SdfAllowed PermissionToEdit(SdfListOpType op)
const
92 if (!_owner->PermissionToEdit()) {
99 virtual bool CopyEdits(
const Sdf_ListEditor& rhs) = 0;
100 virtual bool ClearEdits() = 0;
101 virtual bool ClearEditsAndMakeExplicit() = 0;
103 typedef std::function<
104 std::optional<value_type>(
const value_type&)
114 virtual void ModifyItemEdits(
const ModifyCallback& cb) = 0;
116 typedef std::function<
117 std::optional<value_type>(SdfListOpType,
const value_type&)
128 virtual void ApplyEditsToList(
129 value_vector_type* vec,
130 const ApplyCallback& cb = ApplyCallback()) = 0;
133 size_t GetSize(SdfListOpType op)
const
135 return _GetOperations(op).size();
139 value_type Get(SdfListOpType op,
size_t i)
const
141 return _GetOperations(op)[i];
145 value_vector_type GetVector(SdfListOpType op)
const
147 return _GetOperations(op);
152 size_t Count(SdfListOpType op,
const value_type& val)
const
154 const value_vector_type& ops = _GetOperations(op);
155 return std::count(ops.begin(), ops.end(), _typePolicy.Canonicalize(val));
160 size_t Find(SdfListOpType op,
const value_type& val)
const
162 const value_vector_type& vec = _GetOperations(op);
163 typename value_vector_type::const_iterator findIt =
164 std::find(vec.begin(), vec.end(), _typePolicy.Canonicalize(val));
165 if (findIt != vec.end()) {
166 return std::distance(vec.begin(), findIt);
174 virtual bool ReplaceEdits(
175 SdfListOpType op,
size_t index,
size_t n,
176 const value_vector_type& elems) = 0;
179 virtual void ApplyList(SdfListOpType op,
const Sdf_ListEditor& rhs) = 0;
186 Sdf_ListEditor(
const SdfSpecHandle& owner,
const TfToken& field,
187 const TypePolicy& typePolicy)
190 _typePolicy(typePolicy)
194 const SdfSpecHandle& _GetOwner()
const
199 const TfToken& _GetField()
const
204 const TypePolicy& _GetTypePolicy()
const
209 virtual bool _ValidateEdit(SdfListOpType op,
210 const value_vector_type& oldValues,
211 const value_vector_type& newValues)
const
228 typename value_vector_type::const_iterator
229 oldValuesTail = oldValues.begin(),
230 newValuesTail = newValues.begin();
231 auto oldEnd = oldValues.end(), newEnd = newValues.end();
232 while (oldValuesTail != oldEnd && newValuesTail != newEnd &&
233 *oldValuesTail == *newValuesTail) {
234 ++oldValuesTail, ++newValuesTail;
237 for (
auto i = newValuesTail; i != newEnd; ++i) {
239 for (
auto j = newValues.begin(); j != i; ++j) {
242 "field '%s' on <%s>",
245 this->GetPath().GetText());
253 _owner->GetSchema().GetFieldDefinition(_field);
259 for (
auto i = newValuesTail; i != newEnd; ++i) {
260 if (
SdfAllowed isValid = fieldDef->IsValidListValue(*i)) { }
271 virtual void _OnEdit(SdfListOpType op,
272 const value_vector_type& oldValues,
273 const value_vector_type& newValues)
const
277 virtual const value_vector_type& _GetOperations(SdfListOpType op)
const = 0;
280 SdfSpecHandle _owner;
282 TypePolicy _typePolicy;
286template <
class TypePolicy>
288operator<<(std::ostream& s,
const Sdf_ListEditor<TypePolicy>& x)
291 typedef typename Sdf_ListEditor<TypePolicy>::value_vector_type
294 static void _Write(std::ostream& s,
const value_vector_type& v)
297 for (
size_t i = 0, n = v.size(); i < n; ++i) {
310 else if (x.IsExplicit()) {
311 Util::_Write(s, x.GetVector(SdfListOpTypeExplicit));
316 if (!x.IsOrderedOnly()) {
318 Util::_Write(s, x.GetVector(SdfListOpTypeAdded));
319 s <<
"'prepended': ";
320 Util::_Write(s, x.GetVector(SdfListOpTypePrepended));
322 Util::_Write(s, x.GetVector(SdfListOpTypeAppended));
323 s <<
", 'deleted': ";
324 Util::_Write(s, x.GetVector(SdfListOpTypeDeleted));
328 Util::_Write(s, x.GetVector(SdfListOpTypeOrdered));
333PXR_NAMESPACE_CLOSE_SCOPE
Indicates if an operation is allowed and, if not, why not.
A scene description container that can combine with other such containers to form simple component as...
A path value used to locate objects in layers or scenegraphs.
Class defining various attributes for a field.
Base class for all Sdf spec classes.
Token for efficient comparison, assignment, and hashing of known strings.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
std::string TfStringify(const T &v)
Convert an arbitrary type into a string.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...