All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
listEditor.h
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 <boost/noncopyable.hpp>
37 #include <boost/optional.hpp>
38 
39 #include <functional>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 SDF_DECLARE_HANDLES(SdfLayer);
44 SDF_DECLARE_HANDLES(SdfSpec);
45 
51 template <class TypePolicy>
52 class Sdf_ListEditor
53  : public boost::noncopyable
54 {
55 private:
56  typedef Sdf_ListEditor<TypePolicy> This;
57 
58 public:
59  typedef typename TypePolicy::value_type value_type;
60  typedef std::vector<value_type> value_vector_type;
61 
62  SdfLayerHandle GetLayer() const
63  {
64  return _owner ? _owner->GetLayer() : SdfLayerHandle();
65  }
66 
67  SdfPath GetPath() const
68  {
69  return _owner ? _owner->GetPath() : SdfPath();
70  }
71 
72  bool IsValid() const
73  {
74  return !IsExpired();
75  }
76 
77  bool IsExpired() const
78  {
79  return !_owner;
80  }
81 
82  bool HasKeys() const
83  {
84  if (IsExplicit()) {
85  return true;
86  }
87  else if (IsOrderedOnly()) {
88  return !_GetOperations(SdfListOpTypeOrdered).empty();
89  }
90  else {
91  return (!_GetOperations(SdfListOpTypeAdded).empty() ||
92  !_GetOperations(SdfListOpTypePrepended).empty() ||
93  !_GetOperations(SdfListOpTypeAppended).empty() ||
94  !_GetOperations(SdfListOpTypeDeleted).empty() ||
95  !_GetOperations(SdfListOpTypeOrdered).empty());
96  }
97  }
98 
99  virtual bool IsExplicit() const = 0;
100  virtual bool IsOrderedOnly() const = 0;
101 
102  virtual SdfAllowed PermissionToEdit(SdfListOpType op) const
103  {
104  if (!_owner) {
105  return SdfAllowed("List editor is expired");
106  }
107 
108  if (!_owner->PermissionToEdit()) {
109  return SdfAllowed("Permission denied");
110  }
111 
112  return true;
113  }
114 
115  virtual bool CopyEdits(const Sdf_ListEditor& rhs) = 0;
116  virtual bool ClearEdits() = 0;
117  virtual bool ClearEditsAndMakeExplicit() = 0;
118 
119  typedef std::function<
120  boost::optional<value_type>(const value_type&)
121  >
122  ModifyCallback;
123 
128  virtual void ModifyItemEdits(const ModifyCallback& cb) = 0;
129 
130  typedef std::function<
131  boost::optional<value_type>(SdfListOpType, const value_type&)
132  >
133  ApplyCallback;
134 
142  virtual void ApplyEditsToList(
143  value_vector_type* vec,
144  const ApplyCallback& cb = ApplyCallback()) = 0;
145 
147  size_t GetSize(SdfListOpType op) const
148  {
149  return _GetOperations(op).size();
150  }
151 
153  value_type Get(SdfListOpType op, size_t i) const
154  {
155  return _GetOperations(op)[i];
156  }
157 
159  value_vector_type GetVector(SdfListOpType op) const
160  {
161  return _GetOperations(op);
162  }
163 
166  size_t Count(SdfListOpType op, const value_type& val) const
167  {
168  const value_vector_type& ops = _GetOperations(op);
169  return std::count(ops.begin(), ops.end(), _typePolicy.Canonicalize(val));
170  }
171 
174  size_t Find(SdfListOpType op, const value_type& val) const
175  {
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);
181  }
182 
183  return size_t(-1);
184  }
185 
188  virtual bool ReplaceEdits(
189  SdfListOpType op, size_t index, size_t n,
190  const value_vector_type& elems) = 0;
191 
193  virtual void ApplyList(SdfListOpType op, const Sdf_ListEditor& rhs) = 0;
194 
195 protected:
196  Sdf_ListEditor()
197  {
198  }
199 
200  Sdf_ListEditor(const SdfSpecHandle& owner, const TfToken& field,
201  const TypePolicy& typePolicy)
202  : _owner(owner),
203  _field(field),
204  _typePolicy(typePolicy)
205  {
206  }
207 
208  virtual ~Sdf_ListEditor() = default;
209 
210  const SdfSpecHandle& _GetOwner() const
211  {
212  return _owner;
213  }
214 
215  const TfToken& _GetField() const
216  {
217  return _field;
218  }
219 
220  const TypePolicy& _GetTypePolicy() const
221  {
222  return _typePolicy;
223  }
224 
225  virtual bool _ValidateEdit(SdfListOpType op,
226  const value_vector_type& oldValues,
227  const value_vector_type& newValues) const
228  {
229  // Disallow duplicate items from being stored in the new list
230  // editor values. This is O(n^2), but we expect the number of elements
231  // stored to be small enough that this won't matter.
232  //
233  // XXX:
234  // We assume that duplicate data items are never allowed to be
235  // authored. For full generality, this information ought to come from
236  // the layer schema.
237 
238  // We also assume that the `oldValues` are already valid and do not
239  // contain duplicates. With this assumption, we can accelerate the
240  // common case of appending new items at the end and skip over a common
241  // prefix of oldValues and newValues. Then we can only check for dupes
242  // in the tail of newValues.
243 
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;
251  }
252 
253  for (auto i = newValuesTail; i != newEnd; ++i) {
254  // Have to check unmatched new items for dupes.
255  for (auto j = newValues.begin(); j != i; ++j) {
256  if (*i == *j) {
257  TF_CODING_ERROR("Duplicate item '%s' not allowed for "
258  "field '%s' on <%s>",
259  TfStringify(*i).c_str(),
260  _field.GetText(),
261  this->GetPath().GetText());
262  return false;
263  }
264  }
265  }
266 
267  // Ensure that all new values are valid for this field.
268  const SdfSchema::FieldDefinition* fieldDef =
269  _owner->GetSchema().GetFieldDefinition(_field);
270  if (!fieldDef) {
271  TF_CODING_ERROR("No field definition for field '%s'",
272  _field.GetText());
273  }
274  else {
275  for (auto i = newValuesTail; i != newEnd; ++i) {
276  if (SdfAllowed isValid = fieldDef->IsValidListValue(*i)) { }
277  else {
278  TF_CODING_ERROR("%s", isValid.GetWhyNot().c_str());
279  return false;
280  }
281  }
282  }
283 
284  return true;
285  }
286 
287  virtual void _OnEdit(SdfListOpType op,
288  const value_vector_type& oldValues,
289  const value_vector_type& newValues) const
290  {
291  }
292 
293  virtual const value_vector_type& _GetOperations(SdfListOpType op) const = 0;
294 
295 private:
296  SdfSpecHandle _owner;
297  TfToken _field;
298  TypePolicy _typePolicy;
299 
300 };
301 
302 template <class TypePolicy>
303 std::ostream&
304 operator<<(std::ostream& s, const Sdf_ListEditor<TypePolicy>& x)
305 {
306  struct Util {
307  typedef typename Sdf_ListEditor<TypePolicy>::value_vector_type
308  value_vector_type;
309 
310  static void _Write(std::ostream& s, const value_vector_type& v)
311  {
312  s << '[';
313  for (size_t i = 0, n = v.size(); i < n; ++i) {
314  if (i != 0) {
315  s << ", ";
316  }
317  s << v[i];
318  }
319  s << ']';
320  }
321  };
322 
323  if (!x.IsValid()) {
324  return s;
325  }
326  else if (x.IsExplicit()) {
327  Util::_Write(s, x.GetVector(SdfListOpTypeExplicit));
328  return s;
329  }
330  else {
331  s << "{ ";
332  if (!x.IsOrderedOnly()) {
333  s << "'added': ";
334  Util::_Write(s, x.GetVector(SdfListOpTypeAdded));
335  s << "'prepended': ";
336  Util::_Write(s, x.GetVector(SdfListOpTypePrepended));
337  s << "'appended': ";
338  Util::_Write(s, x.GetVector(SdfListOpTypeAppended));
339  s << ", 'deleted': ";
340  Util::_Write(s, x.GetVector(SdfListOpTypeDeleted));
341  s << ", ";
342  }
343  s << "'ordered': ";
344  Util::_Write(s, x.GetVector(SdfListOpTypeOrdered));
345  return s << " }";
346  }
347 }
348 
349 PXR_NAMESPACE_CLOSE_SCOPE
350 
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...
Definition: layer.h:96
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:85
Base class for all Sdf spec classes.
Definition: spec.h:51
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Class defining various attributes for a field.
Definition: schema.h:72
Indicates if an operation is allowed and, if not, why not.
Definition: allowed.h:47
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
std::enable_if<!std::is_enum< T >::value, std::string >::type TfStringify(const T &v)
Convert an arbitrary type into a string.
Definition: stringUtils.h:527
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...