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"
36 #include <boost/noncopyable.hpp>
37 #include <boost/optional.hpp>
41 PXR_NAMESPACE_OPEN_SCOPE
51 template <
class TypePolicy>
53 :
public boost::noncopyable
56 typedef Sdf_ListEditor<TypePolicy> This;
59 typedef typename TypePolicy::value_type value_type;
60 typedef std::vector<value_type> value_vector_type;
62 SdfLayerHandle GetLayer()
const
64 return _owner ? _owner->GetLayer() : SdfLayerHandle();
69 return _owner ? _owner->GetPath() :
SdfPath();
77 bool IsExpired()
const
87 else if (IsOrderedOnly()) {
88 return !_GetOperations(SdfListOpTypeOrdered).empty();
91 return (!_GetOperations(SdfListOpTypeAdded).empty() ||
92 !_GetOperations(SdfListOpTypePrepended).empty() ||
93 !_GetOperations(SdfListOpTypeAppended).empty() ||
94 !_GetOperations(SdfListOpTypeDeleted).empty() ||
95 !_GetOperations(SdfListOpTypeOrdered).empty());
99 virtual bool IsExplicit()
const = 0;
100 virtual bool IsOrderedOnly()
const = 0;
102 virtual SdfAllowed PermissionToEdit(SdfListOpType op)
const
108 if (!_owner->PermissionToEdit()) {
115 virtual bool CopyEdits(
const Sdf_ListEditor& rhs) = 0;
116 virtual bool ClearEdits() = 0;
117 virtual bool ClearEditsAndMakeExplicit() = 0;
119 typedef std::function<
120 boost::optional<value_type>(
const value_type&)
128 virtual void ModifyItemEdits(
const ModifyCallback& cb) = 0;
130 typedef std::function<
131 boost::optional<value_type>(SdfListOpType,
const value_type&)
142 virtual void ApplyEditsToList(
143 value_vector_type* vec,
144 const ApplyCallback& cb = ApplyCallback()) = 0;
147 size_t GetSize(SdfListOpType op)
const
149 return _GetOperations(op).size();
153 value_type Get(SdfListOpType op,
size_t i)
const
155 return _GetOperations(op)[i];
159 value_vector_type GetVector(SdfListOpType op)
const
161 return _GetOperations(op);
166 size_t Count(SdfListOpType op,
const value_type& val)
const
168 const value_vector_type& ops = _GetOperations(op);
169 return std::count(ops.begin(), ops.end(), _typePolicy.Canonicalize(val));
174 size_t Find(SdfListOpType op,
const value_type& val)
const
176 const value_vector_type& vec = _GetOperations(op);
177 typename value_vector_type::const_iterator findIt =
178 std::find(vec.begin(), vec.end(), _typePolicy.Canonicalize(val));
179 if (findIt != vec.end()) {
180 return std::distance(vec.begin(), findIt);
188 virtual bool ReplaceEdits(
189 SdfListOpType op,
size_t index,
size_t n,
190 const value_vector_type& elems) = 0;
193 virtual void ApplyList(SdfListOpType op,
const Sdf_ListEditor& rhs) = 0;
200 Sdf_ListEditor(
const SdfSpecHandle& owner,
const TfToken& field,
201 const TypePolicy& typePolicy)
204 _typePolicy(typePolicy)
208 virtual ~Sdf_ListEditor() =
default;
210 const SdfSpecHandle& _GetOwner()
const
215 const TfToken& _GetField()
const
220 const TypePolicy& _GetTypePolicy()
const
225 virtual bool _ValidateEdit(SdfListOpType op,
226 const value_vector_type& oldValues,
227 const value_vector_type& newValues)
const
244 typename value_vector_type::const_iterator
245 oldValuesTail = oldValues.begin(),
246 newValuesTail = newValues.begin();
247 auto oldEnd = oldValues.end(), newEnd = newValues.end();
248 while (oldValuesTail != oldEnd && newValuesTail != newEnd &&
249 *oldValuesTail == *newValuesTail) {
250 ++oldValuesTail, ++newValuesTail;
253 for (
auto i = newValuesTail; i != newEnd; ++i) {
255 for (
auto j = newValues.begin(); j != i; ++j) {
258 "field '%s' on <%s>",
261 this->GetPath().GetText());
269 _owner->GetSchema().GetFieldDefinition(_field);
275 for (
auto i = newValuesTail; i != newEnd; ++i) {
276 if (
SdfAllowed isValid = fieldDef->IsValidListValue(*i)) { }
287 virtual void _OnEdit(SdfListOpType op,
288 const value_vector_type& oldValues,
289 const value_vector_type& newValues)
const
293 virtual const value_vector_type& _GetOperations(SdfListOpType op)
const = 0;
296 SdfSpecHandle _owner;
298 TypePolicy _typePolicy;
302 template <
class TypePolicy>
304 operator<<(std::ostream& s, const Sdf_ListEditor<TypePolicy>& x)
307 typedef typename Sdf_ListEditor<TypePolicy>::value_vector_type
310 static void _Write(std::ostream& s,
const value_vector_type& v)
313 for (
size_t i = 0, n = v.size(); i < n; ++i) {
326 else if (x.IsExplicit()) {
327 Util::_Write(s, x.GetVector(SdfListOpTypeExplicit));
332 if (!x.IsOrderedOnly()) {
334 Util::_Write(s, x.GetVector(SdfListOpTypeAdded));
335 s <<
"'prepended': ";
336 Util::_Write(s, x.GetVector(SdfListOpTypePrepended));
338 Util::_Write(s, x.GetVector(SdfListOpTypeAppended));
339 s <<
", 'deleted': ";
340 Util::_Write(s, x.GetVector(SdfListOpTypeDeleted));
344 Util::_Write(s, x.GetVector(SdfListOpTypeOrdered));
349 PXR_NAMESPACE_CLOSE_SCOPE
351 #endif // PXR_USD_SDF_LIST_EDITOR_H
A unit of scene description that you combine with other units of scene description to form a shot...
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Base class for all Sdf spec classes.
Token for efficient comparison, assignment, and hashing of known strings.
Class defining various attributes for a field.
Indicates if an operation is allowed and, if not, why not.
A path value used to locate objects in layers or scenegraphs.
std::enable_if<!std::is_enum< T >::value, std::string >::type 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...