HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
listEditor.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_SDF_LIST_EDITOR_H
25 #define PXR_USD_SDF_LIST_EDITOR_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/token.h"
29 #include "pxr/usd/sdf/allowed.h"
31 #include "pxr/usd/sdf/listOp.h"
32 #include "pxr/usd/sdf/path.h"
33 #include "pxr/usd/sdf/schema.h"
34 #include "pxr/usd/sdf/spec.h"
35 
36 #include <hboost/optional.hpp>
37 
38 #include <functional>
39 
41 
44 
45 /// \class Sdf_ListEditor
46 ///
47 /// Base class for list editor implementations in which list editing operations
48 /// are stored in data field(s) associated with an owning spec.
49 ///
50 template <class TypePolicy>
52 {
53  Sdf_ListEditor(const Sdf_ListEditor&) = delete;
54  Sdf_ListEditor& operator=(const Sdf_ListEditor&) = delete;
55 private:
57 
58 public:
60  typedef std::vector<value_type> value_vector_type;
61 
62  virtual ~Sdf_ListEditor() = default;
63 
64  SdfLayerHandle GetLayer() const
65  {
66  return _owner ? _owner->GetLayer() : SdfLayerHandle();
67  }
68 
69  SdfPath GetPath() const
70  {
71  return _owner ? _owner->GetPath() : SdfPath();
72  }
73 
74  bool IsValid() const
75  {
76  return !IsExpired();
77  }
78 
79  bool IsExpired() const
80  {
81  return !_owner;
82  }
83 
84  bool HasKeys() const
85  {
86  if (IsExplicit()) {
87  return true;
88  }
89  else if (IsOrderedOnly()) {
90  return !_GetOperations(SdfListOpTypeOrdered).empty();
91  }
92  else {
93  return (!_GetOperations(SdfListOpTypeAdded).empty() ||
98  }
99  }
100 
101  virtual bool IsExplicit() const = 0;
102  virtual bool IsOrderedOnly() const = 0;
103 
105  {
106  if (!_owner) {
107  return SdfAllowed("List editor is expired");
108  }
109 
110  if (!_owner->PermissionToEdit()) {
111  return SdfAllowed("Permission denied");
112  }
113 
114  return true;
115  }
116 
117  virtual bool CopyEdits(const Sdf_ListEditor& rhs) = 0;
118  virtual bool ClearEdits() = 0;
119  virtual bool ClearEditsAndMakeExplicit() = 0;
120 
121  typedef std::function<
122  hboost::optional<value_type>(const value_type&)
123  >
125 
126  /// Modifies the operations stored in all operation lists.
127  /// \p callback is called for every key. If the returned key is
128  /// invalid then the key is removed, otherwise it's replaced with the
129  /// returned key. If the returned key matches a key that was previously
130  /// returned for the list being processed, the returned key will be
131  /// removed.
132  virtual void ModifyItemEdits(const ModifyCallback& cb) = 0;
133 
134  typedef std::function<
135  hboost::optional<value_type>(SdfListOpType, const value_type&)
136  >
138 
139  /// Apply the list operations represented by this interface to the given
140  /// vector of values \p vec. If \p callback is valid then it's called
141  /// for every key in the editor before applying it to \p vec. If the
142  /// returned key is empty then the key will not be applied. Otherwise
143  /// the returned key is applied, allowing callbacks to perform key
144  /// translation. Note that this means list editors can't meaningfully
145  /// hold the empty key.
146  virtual void ApplyEditsToList(
147  value_vector_type* vec,
148  const ApplyCallback& cb = ApplyCallback()) = 0;
149 
150  /// Returns the number of elements in the specified list of operations.
151  size_t GetSize(SdfListOpType op) const
152  {
153  return _GetOperations(op).size();
154  }
155 
156  /// Returns the \p i'th value in the specified list of operations.
157  value_type Get(SdfListOpType op, size_t i) const
158  {
159  return _GetOperations(op)[i];
160  }
161 
162  /// Returns the specified list of operations.
164  {
165  return _GetOperations(op);
166  }
167 
168  /// Returns the number of occurrences of \p val in the specified list of
169  /// operations.
170  size_t Count(SdfListOpType op, const value_type& val) const
171  {
172  const value_vector_type& ops = _GetOperations(op);
173  return std::count(ops.begin(), ops.end(), _typePolicy.Canonicalize(val));
174  }
175 
176  /// Returns the index of \p val in the specified list of operations, -1
177  /// if \p val is not found.
178  size_t Find(SdfListOpType op, const value_type& val) const
179  {
180  const value_vector_type& vec = _GetOperations(op);
181  typename value_vector_type::const_iterator findIt =
182  std::find(vec.begin(), vec.end(), _typePolicy.Canonicalize(val));
183  if (findIt != vec.end()) {
184  return std::distance(vec.begin(), findIt);
185  }
186 
187  return size_t(-1);
188  }
189 
190  /// Replaces the operations in the specified list of operations in range
191  /// [index, index + n) with the given \p elems.
192  virtual bool ReplaceEdits(
193  SdfListOpType op, size_t index, size_t n,
194  const value_vector_type& elems) = 0;
195 
196  /// Applies a \p rhs opinions about a given operation list to this one.
197  virtual void ApplyList(SdfListOpType op, const Sdf_ListEditor& rhs) = 0;
198 
199 protected:
201  {
202  }
203 
204  Sdf_ListEditor(const SdfSpecHandle& owner, const TfToken& field,
205  const TypePolicy& typePolicy)
206  : _owner(owner),
207  _field(field),
208  _typePolicy(typePolicy)
209  {
210  }
211 
212  const SdfSpecHandle& _GetOwner() const
213  {
214  return _owner;
215  }
216 
217  const TfToken& _GetField() const
218  {
219  return _field;
220  }
221 
222  const TypePolicy& _GetTypePolicy() const
223  {
224  return _typePolicy;
225  }
226 
227  virtual bool _ValidateEdit(SdfListOpType op,
228  const value_vector_type& oldValues,
229  const value_vector_type& newValues) const
230  {
231  // Disallow duplicate items from being stored in the new list
232  // editor values. This is O(n^2), but we expect the number of elements
233  // stored to be small enough that this won't matter.
234  //
235  // XXX:
236  // We assume that duplicate data items are never allowed to be
237  // authored. For full generality, this information ought to come from
238  // the layer schema.
239 
240  // We also assume that the `oldValues` are already valid and do not
241  // contain duplicates. With this assumption, we can accelerate the
242  // common case of appending new items at the end and skip over a common
243  // prefix of oldValues and newValues. Then we can only check for dupes
244  // in the tail of newValues.
245 
246  typename value_vector_type::const_iterator
247  oldValuesTail = oldValues.begin(),
248  newValuesTail = newValues.begin();
249  auto oldEnd = oldValues.end(), newEnd = newValues.end();
250  while (oldValuesTail != oldEnd && newValuesTail != newEnd &&
251  *oldValuesTail == *newValuesTail) {
252  ++oldValuesTail, ++newValuesTail;
253  }
254 
255  for (auto i = newValuesTail; i != newEnd; ++i) {
256  // Have to check unmatched new items for dupes.
257  for (auto j = newValues.begin(); j != i; ++j) {
258  if (*i == *j) {
259  TF_CODING_ERROR("Duplicate item '%s' not allowed for "
260  "field '%s' on <%s>",
261  TfStringify(*i).c_str(),
262  _field.GetText(),
263  this->GetPath().GetText());
264  return false;
265  }
266  }
267  }
268 
269  // Ensure that all new values are valid for this field.
270  const SdfSchema::FieldDefinition* fieldDef =
271  _owner->GetSchema().GetFieldDefinition(_field);
272  if (!fieldDef) {
273  TF_CODING_ERROR("No field definition for field '%s'",
274  _field.GetText());
275  }
276  else {
277  for (auto i = newValuesTail; i != newEnd; ++i) {
278  if (SdfAllowed isValid = fieldDef->IsValidListValue(*i)) { }
279  else {
280  TF_CODING_ERROR("%s", isValid.GetWhyNot().c_str());
281  return false;
282  }
283  }
284  }
285 
286  return true;
287  }
288 
289  virtual void _OnEdit(SdfListOpType op,
290  const value_vector_type& oldValues,
291  const value_vector_type& newValues) const
292  {
293  }
294 
295  virtual const value_vector_type& _GetOperations(SdfListOpType op) const = 0;
296 
297 private:
298  SdfSpecHandle _owner;
299  TfToken _field;
300  TypePolicy _typePolicy;
301 
302 };
303 
304 template <class TypePolicy>
305 std::ostream&
306 operator<<(std::ostream& s, const Sdf_ListEditor<TypePolicy>& x)
307 {
308  struct Util {
310  value_vector_type;
311 
312  static void _Write(std::ostream& s, const value_vector_type& v)
313  {
314  s << '[';
315  for (size_t i = 0, n = v.size(); i < n; ++i) {
316  if (i != 0) {
317  s << ", ";
318  }
319  s << v[i];
320  }
321  s << ']';
322  }
323  };
324 
325  if (!x.IsValid()) {
326  return s;
327  }
328  else if (x.IsExplicit()) {
329  Util::_Write(s, x.GetVector(SdfListOpTypeExplicit));
330  return s;
331  }
332  else {
333  s << "{ ";
334  if (!x.IsOrderedOnly()) {
335  s << "'added': ";
336  Util::_Write(s, x.GetVector(SdfListOpTypeAdded));
337  s << "'prepended': ";
338  Util::_Write(s, x.GetVector(SdfListOpTypePrepended));
339  s << "'appended': ";
340  Util::_Write(s, x.GetVector(SdfListOpTypeAppended));
341  s << ", 'deleted': ";
342  Util::_Write(s, x.GetVector(SdfListOpTypeDeleted));
343  s << ", ";
344  }
345  s << "'ordered': ";
346  Util::_Write(s, x.GetVector(SdfListOpTypeOrdered));
347  return s << " }";
348  }
349 }
350 
352 
353 #endif // PXR_USD_SDF_LIST_EDITOR_H
virtual const value_vector_type & _GetOperations(SdfListOpType op) const =0
SDF_API const char * GetText() const
Definition: layer.h:94
size_t Find(SdfListOpType op, const value_type &val) const
Definition: listEditor.h:178
virtual bool IsOrderedOnly() const =0
bool HasKeys() const
Definition: listEditor.h:84
const GLdouble * v
Definition: glcorearb.h:837
#define TF_CODING_ERROR
virtual void _OnEdit(SdfListOpType op, const value_vector_type &oldValues, const value_vector_type &newValues) const
Definition: listEditor.h:289
Definition: spec.h:49
GLdouble s
Definition: glad.h:3009
virtual bool ClearEdits()=0
uint64 value_type
Definition: GA_PrimCompat.h:29
TypePolicy::value_type value_type
Definition: listEditor.h:59
const TypePolicy & _GetTypePolicy() const
Definition: listEditor.h:222
value_type Get(SdfListOpType op, size_t i) const
Returns the i'th value in the specified list of operations.
Definition: listEditor.h:157
virtual SdfAllowed PermissionToEdit(SdfListOpType op) const
Definition: listEditor.h:104
const SdfSpecHandle & _GetOwner() const
Definition: listEditor.h:212
GLdouble n
Definition: glcorearb.h:2008
Definition: token.h:87
bool IsValid() const
Definition: listEditor.h:74
virtual bool ReplaceEdits(SdfListOpType op, size_t index, size_t n, const value_vector_type &elems)=0
virtual bool IsExplicit() const =0
SdfListOpType
Definition: listOp.h:47
Sdf_ListEditor(const SdfSpecHandle &owner, const TfToken &field, const TypePolicy &typePolicy)
Definition: listEditor.h:204
SdfLayerHandle GetLayer() const
Definition: listEditor.h:64
virtual void ModifyItemEdits(const ModifyCallback &cb)=0
virtual void ApplyList(SdfListOpType op, const Sdf_ListEditor &rhs)=0
Applies a rhs opinions about a given operation list to this one.
const TfToken & _GetField() const
Definition: listEditor.h:217
Definition: path.h:291
GLint GLenum GLint x
Definition: glcorearb.h:409
std::vector< value_type > value_vector_type
Definition: listEditor.h:60
char const * GetText() const
Definition: token.h:196
std::function< hboost::optional< value_type >const value_type &) > ModifyCallback
Definition: listEditor.h:124
GLint j
Definition: glad.h:2733
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
size_t Count(SdfListOpType op, const value_type &val) const
Definition: listEditor.h:170
GLuint index
Definition: glcorearb.h:786
SdfAllowed IsValidListValue(const T &value) const
Definition: schema.h:103
virtual bool ClearEditsAndMakeExplicit()=0
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool IsExpired() const
Definition: listEditor.h:79
virtual ~Sdf_ListEditor()=default
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfLayer)
std::enable_if<!std::is_enum< T >::value, std::string >::type TfStringify(const T &v)
Definition: stringUtils.h:541
SdfPath GetPath() const
Definition: listEditor.h:69
SIM_API const UT_StringHolder distance
virtual void ApplyEditsToList(value_vector_type *vec, const ApplyCallback &cb=ApplyCallback())=0
size_t GetSize(SdfListOpType op) const
Returns the number of elements in the specified list of operations.
Definition: listEditor.h:151
std::function< hboost::optional< value_type >SdfListOpType, const value_type &) > ApplyCallback
Definition: listEditor.h:137
GLint GLsizei count
Definition: glcorearb.h:405
value_vector_type GetVector(SdfListOpType op) const
Returns the specified list of operations.
Definition: listEditor.h:163
virtual bool _ValidateEdit(SdfListOpType op, const value_vector_type &oldValues, const value_vector_type &newValues) const
Definition: listEditor.h:227
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
virtual bool CopyEdits(const Sdf_ListEditor &rhs)=0