24#ifndef PXR_USD_SDF_LIST_EDITOR_H
25#define PXR_USD_SDF_LIST_EDITOR_H
31#include "pxr/usd/sdf/listOp.h"
32#include "pxr/usd/sdf/path.h"
33#include "pxr/usd/sdf/schema.h"
39PXR_NAMESPACE_OPEN_SCOPE
49template <
class TypePolicy>
52 Sdf_ListEditor(
const Sdf_ListEditor&) =
delete;
53 Sdf_ListEditor& operator=(
const Sdf_ListEditor&) =
delete;
55 typedef Sdf_ListEditor<TypePolicy> This;
58 typedef typename TypePolicy::value_type value_type;
59 typedef std::vector<value_type> value_vector_type;
61 virtual ~Sdf_ListEditor() =
default;
63 SdfLayerHandle GetLayer()
const
65 return _owner ? _owner->GetLayer() : SdfLayerHandle();
70 return _owner ? _owner->GetPath() :
SdfPath();
78 bool IsExpired()
const
88 else if (IsOrderedOnly()) {
89 return !_GetOperations(SdfListOpTypeOrdered).empty();
92 return (!_GetOperations(SdfListOpTypeAdded).empty() ||
93 !_GetOperations(SdfListOpTypePrepended).empty() ||
94 !_GetOperations(SdfListOpTypeAppended).empty() ||
95 !_GetOperations(SdfListOpTypeDeleted).empty() ||
96 !_GetOperations(SdfListOpTypeOrdered).empty());
100 virtual bool IsExplicit()
const = 0;
101 virtual bool IsOrderedOnly()
const = 0;
103 virtual SdfAllowed PermissionToEdit(SdfListOpType op)
const
109 if (!_owner->PermissionToEdit()) {
116 virtual bool CopyEdits(
const Sdf_ListEditor& rhs) = 0;
117 virtual bool ClearEdits() = 0;
118 virtual bool ClearEditsAndMakeExplicit() = 0;
120 typedef std::function<
121 std::optional<value_type>(
const value_type&)
131 virtual void ModifyItemEdits(
const ModifyCallback& cb) = 0;
133 typedef std::function<
134 std::optional<value_type>(SdfListOpType,
const value_type&)
145 virtual void ApplyEditsToList(
146 value_vector_type* vec,
147 const ApplyCallback& cb = ApplyCallback()) = 0;
150 size_t GetSize(SdfListOpType op)
const
152 return _GetOperations(op).size();
156 value_type Get(SdfListOpType op,
size_t i)
const
158 return _GetOperations(op)[i];
162 value_vector_type GetVector(SdfListOpType op)
const
164 return _GetOperations(op);
169 size_t Count(SdfListOpType op,
const value_type& val)
const
171 const value_vector_type& ops = _GetOperations(op);
172 return std::count(ops.begin(), ops.end(), _typePolicy.Canonicalize(val));
177 size_t Find(SdfListOpType op,
const value_type& val)
const
179 const value_vector_type& vec = _GetOperations(op);
180 typename value_vector_type::const_iterator findIt =
181 std::find(vec.begin(), vec.end(), _typePolicy.Canonicalize(val));
182 if (findIt != vec.end()) {
183 return std::distance(vec.begin(), findIt);
191 virtual bool ReplaceEdits(
192 SdfListOpType op,
size_t index,
size_t n,
193 const value_vector_type& elems) = 0;
196 virtual void ApplyList(SdfListOpType op,
const Sdf_ListEditor& rhs) = 0;
203 Sdf_ListEditor(
const SdfSpecHandle& owner,
const TfToken& field,
204 const TypePolicy& typePolicy)
207 _typePolicy(typePolicy)
211 const SdfSpecHandle& _GetOwner()
const
216 const TfToken& _GetField()
const
221 const TypePolicy& _GetTypePolicy()
const
226 virtual bool _ValidateEdit(SdfListOpType op,
227 const value_vector_type& oldValues,
228 const value_vector_type& newValues)
const
245 typename value_vector_type::const_iterator
246 oldValuesTail = oldValues.begin(),
247 newValuesTail = newValues.begin();
248 auto oldEnd = oldValues.end(), newEnd = newValues.end();
249 while (oldValuesTail != oldEnd && newValuesTail != newEnd &&
250 *oldValuesTail == *newValuesTail) {
251 ++oldValuesTail, ++newValuesTail;
254 for (
auto i = newValuesTail; i != newEnd; ++i) {
256 for (
auto j = newValues.begin(); j != i; ++j) {
259 "field '%s' on <%s>",
262 this->GetPath().GetText());
270 _owner->GetSchema().GetFieldDefinition(_field);
276 for (
auto i = newValuesTail; i != newEnd; ++i) {
277 if (
SdfAllowed isValid = fieldDef->IsValidListValue(*i)) { }
288 virtual void _OnEdit(SdfListOpType op,
289 const value_vector_type& oldValues,
290 const value_vector_type& newValues)
const
294 virtual const value_vector_type& _GetOperations(SdfListOpType op)
const = 0;
297 SdfSpecHandle _owner;
299 TypePolicy _typePolicy;
303template <
class TypePolicy>
305operator<<(std::ostream& s,
const Sdf_ListEditor<TypePolicy>& x)
308 typedef typename Sdf_ListEditor<TypePolicy>::value_vector_type
311 static void _Write(std::ostream& s,
const value_vector_type& v)
314 for (
size_t i = 0, n = v.size(); i < n; ++i) {
327 else if (x.IsExplicit()) {
328 Util::_Write(s, x.GetVector(SdfListOpTypeExplicit));
333 if (!x.IsOrderedOnly()) {
335 Util::_Write(s, x.GetVector(SdfListOpTypeAdded));
336 s <<
"'prepended': ";
337 Util::_Write(s, x.GetVector(SdfListOpTypePrepended));
339 Util::_Write(s, x.GetVector(SdfListOpTypeAppended));
340 s <<
", 'deleted': ";
341 Util::_Write(s, x.GetVector(SdfListOpTypeDeleted));
345 Util::_Write(s, x.GetVector(SdfListOpTypeOrdered));
350PXR_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...