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 SDF_LIST_EDITOR_H
25 #define SDF_LIST_EDITOR_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/token.h"
29 #include "pxr/usd/sdf/allowed.h"
30 #include "pxr/usd/sdf/declareHandles.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  for (size_t i = 0; i < newValues.size(); ++i) {
238  for (size_t j = i + 1; j < newValues.size(); ++j) {
239  if (newValues[i] == newValues[j]) {
240  TF_CODING_ERROR("Duplicate item '%s' not allowed for "
241  "field '%s' on <%s>",
242  TfStringify(newValues[i]).c_str(),
243  _field.GetText(),
244  this->GetPath().GetText());
245  return false;
246  }
247  }
248  }
249 
250  // Ensure that all new values are valid for this field.
251  const SdfSchema::FieldDefinition* fieldDef =
252  _owner->GetSchema().GetFieldDefinition(_field);
253  if (!fieldDef) {
254  TF_CODING_ERROR("No field definition for field '%s'",
255  _field.GetText());
256  }
257  else {
258  TF_FOR_ALL(v, newValues) {
259  if (SdfAllowed isValid = fieldDef->IsValidListValue(*v)) { }
260  else {
261  TF_CODING_ERROR("%s", isValid.GetWhyNot().c_str());
262  return false;
263  }
264  }
265  }
266 
267  return true;
268  }
269 
270  virtual void _OnEdit(SdfListOpType op,
271  const value_vector_type& oldValues,
272  const value_vector_type& newValues) const
273  {
274  }
275 
276  virtual const value_vector_type& _GetOperations(SdfListOpType op) const = 0;
277 
278 private:
279  SdfSpecHandle _owner;
280  TfToken _field;
281  TypePolicy _typePolicy;
282 
283 };
284 
285 template <class TypePolicy>
286 std::ostream&
287 operator<<(std::ostream& s, const Sdf_ListEditor<TypePolicy>& x)
288 {
289  struct Util {
290  typedef typename Sdf_ListEditor<TypePolicy>::value_vector_type
291  value_vector_type;
292 
293  static void _Write(std::ostream& s, const value_vector_type& v)
294  {
295  s << '[';
296  for (size_t i = 0, n = v.size(); i < n; ++i) {
297  if (i != 0) {
298  s << ", ";
299  }
300  s << v[i];
301  }
302  s << ']';
303  }
304  };
305 
306  if (!x.IsValid()) {
307  return s;
308  }
309  else if (x.IsExplicit()) {
310  Util::_Write(s, x.GetVector(SdfListOpTypeExplicit));
311  return s;
312  }
313  else {
314  s << "{ ";
315  if (!x.IsOrderedOnly()) {
316  s << "'added': ";
317  Util::_Write(s, x.GetVector(SdfListOpTypeAdded));
318  s << "'prepended': ";
319  Util::_Write(s, x.GetVector(SdfListOpTypePrepended));
320  s << "'appended': ";
321  Util::_Write(s, x.GetVector(SdfListOpTypeAppended));
322  s << ", 'deleted': ";
323  Util::_Write(s, x.GetVector(SdfListOpTypeDeleted));
324  s << ", ";
325  }
326  s << "'ordered': ";
327  Util::_Write(s, x.GetVector(SdfListOpTypeOrdered));
328  return s << " }";
329  }
330 }
331 
332 PXR_NAMESPACE_CLOSE_SCOPE
333 
334 #endif // 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:93
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:87
Base class for all Sdf spec classes.
Definition: spec.h:51
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
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
#define TF_FOR_ALL(iter, c)
Macro for iterating over a container.
Definition: iterator.h:390
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
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:523