listOp.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_OP_H
25 #define PXR_USD_SDF_LIST_OP_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/sdf/api.h"
29 #include "pxr/base/tf/token.h"
30 
31 #include <boost/functional/hash.hpp>
32 #include <boost/optional/optional_fwd.hpp>
33 
34 #include <functional>
35 #include <iosfwd>
36 #include <list>
37 #include <map>
38 #include <string>
39 #include <vector>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
47 enum SdfListOpType {
48  SdfListOpTypeExplicit,
49  SdfListOpTypeAdded,
50  SdfListOpTypeDeleted,
51  SdfListOpTypeOrdered,
52  SdfListOpTypePrepended,
53  SdfListOpTypeAppended
54 };
55 
61 template <class T>
62 struct Sdf_ListOpTraits
63 {
64  typedef std::less<T> ItemComparator;
65 };
66 
74 template <typename T>
75 class SdfListOp {
76 public:
77  typedef T ItemType;
78  typedef std::vector<ItemType> ItemVector;
79  typedef ItemType value_type;
80  typedef ItemVector value_vector_type;
81 
83  SDF_API
85  const ItemVector& explicitItems = ItemVector());
86 
89  SDF_API
90  static SdfListOp Create(
91  const ItemVector& prependedItems = ItemVector(),
92  const ItemVector& appendedItems = ItemVector(),
93  const ItemVector& deletedItems = ItemVector());
94 
96  SDF_API SdfListOp();
97 
98  SDF_API void Swap(SdfListOp<T>& rhs);
99 
103  bool HasKeys() const
104  {
105  if (IsExplicit()) {
106  return true;
107  }
108  if (_addedItems.size() != 0 ||
109  _prependedItems.size() != 0 ||
110  _appendedItems.size() != 0 ||
111  _deletedItems.size() != 0) {
112  return true;
113  }
114  return _orderedItems.size() != 0;
115  }
116 
118  SDF_API bool HasItem(const T& item) const;
119 
121  bool IsExplicit() const
122  {
123  return _isExplicit;
124  }
125 
127  const ItemVector& GetExplicitItems() const
128  {
129  return _explicitItems;
130  }
131 
133  const ItemVector& GetAddedItems() const
134  {
135  return _addedItems;
136  }
137 
139  const ItemVector& GetPrependedItems() const
140  {
141  return _prependedItems;
142  }
143 
145  const ItemVector& GetAppendedItems() const
146  {
147  return _appendedItems;
148  }
149 
151  const ItemVector& GetDeletedItems() const
152  {
153  return _deletedItems;
154  }
155 
157  const ItemVector& GetOrderedItems() const
158  {
159  return _orderedItems;
160  }
161 
163  SDF_API const ItemVector& GetItems(SdfListOpType type) const;
164 
165  SDF_API void SetExplicitItems(const ItemVector &items);
166  SDF_API void SetAddedItems(const ItemVector &items);
167  SDF_API void SetPrependedItems(const ItemVector &items);
168  SDF_API void SetAppendedItems(const ItemVector &items);
169  SDF_API void SetDeletedItems(const ItemVector &items);
170  SDF_API void SetOrderedItems(const ItemVector &items);
171 
173  SDF_API void SetItems(const ItemVector &items, SdfListOpType type);
174 
176  SDF_API void Clear();
177 
179  SDF_API void ClearAndMakeExplicit();
180 
182  typedef std::function<
183  boost::optional<ItemType>(SdfListOpType, const ItemType&)
185 
191  SDF_API
192  void ApplyOperations(ItemVector* vec,
193  const ApplyCallback& cb = ApplyCallback()) const;
194 
205  SDF_API
206  boost::optional<SdfListOp<T>>
207  ApplyOperations(const SdfListOp<T> &inner) const;
208 
210  typedef std::function<
211  boost::optional<ItemType>(const ItemType&)
213 
224  SDF_API
225  bool ModifyOperations(const ModifyCallback& callback,
226  bool removeDuplicates = false);
227 
231  SDF_API
232  bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n,
233  const ItemVector& newItems);
234 
237  SDF_API
238  void ComposeOperations(const SdfListOp<T>& stronger, SdfListOpType op);
239 
240  friend inline size_t hash_value(const SdfListOp &op) {
241  size_t h = 0;
242  boost::hash_combine(h, op._isExplicit);
243  boost::hash_combine(h, op._explicitItems);
244  boost::hash_combine(h, op._addedItems);
245  boost::hash_combine(h, op._prependedItems);
246  boost::hash_combine(h, op._appendedItems);
247  boost::hash_combine(h, op._deletedItems);
248  boost::hash_combine(h, op._orderedItems);
249  return h;
250  }
251 
252  bool operator==(const SdfListOp<T> &rhs) const {
253  return _isExplicit == rhs._isExplicit &&
254  _explicitItems == rhs._explicitItems &&
255  _addedItems == rhs._addedItems &&
256  _prependedItems == rhs._prependedItems &&
257  _appendedItems == rhs._appendedItems &&
258  _deletedItems == rhs._deletedItems &&
259  _orderedItems == rhs._orderedItems;
260  };
261 
262  bool operator!=(const SdfListOp<T> &rhs) const {
263  return !(*this == rhs);
264  };
265 
266 private:
267  void _SetExplicit(bool isExplicit);
268 
269  typedef typename Sdf_ListOpTraits<T>::ItemComparator _ItemComparator;
270  typedef std::list<ItemType> _ApplyList;
271  typedef std::map<ItemType, typename _ApplyList::iterator, _ItemComparator>
272  _ApplyMap;
273 
274  void _AddKeys(SdfListOpType, const ApplyCallback& cb,
275  _ApplyList* result, _ApplyMap* search) const;
276  void _PrependKeys(SdfListOpType, const ApplyCallback& cb,
277  _ApplyList* result, _ApplyMap* search) const;
278  void _AppendKeys(SdfListOpType, const ApplyCallback& cb,
279  _ApplyList* result, _ApplyMap* search) const;
280  void _DeleteKeys(SdfListOpType, const ApplyCallback& cb,
281  _ApplyList* result, _ApplyMap* search) const;
282  void _ReorderKeys(SdfListOpType, const ApplyCallback& cb,
283  _ApplyList* result, _ApplyMap* search) const;
284 
285 private:
286  bool _isExplicit;
287  ItemVector _explicitItems;
288  ItemVector _addedItems;
289  ItemVector _prependedItems;
290  ItemVector _appendedItems;
291  ItemVector _deletedItems;
292  ItemVector _orderedItems;
293 };
294 
295 // ADL swap.
296 template <class T>
297 void swap(SdfListOp<T> &x, SdfListOp<T> &y)
298 {
299  x.Swap(y);
300 }
301 
302 // Helper function for applying an ordering operation described by \p orderVector
303 // to vector \p v.
304 template <class ItemType>
305 SDF_API
306 void SdfApplyListOrdering(std::vector<ItemType>* v,
307  const std::vector<ItemType>& order);
308 
309 // Ostream output methods for list values (useful for debugging and required
310 // for storing a list value in a VtValue).
311 template <typename T>
312 SDF_API
313 std::ostream & operator<<( std::ostream &, const SdfListOp<T> & );
314 
315 // Concrete, instantiated listop types.
316 typedef class SdfListOp<int> SdfIntListOp;
317 typedef class SdfListOp<unsigned int> SdfUIntListOp;
318 typedef class SdfListOp<int64_t> SdfInt64ListOp;
319 typedef class SdfListOp<uint64_t> SdfUInt64ListOp;
320 typedef class SdfListOp<TfToken> SdfTokenListOp;
321 typedef class SdfListOp<std::string> SdfStringListOp;
322 typedef class SdfListOp<class SdfPath> SdfPathListOp;
323 typedef class SdfListOp<class SdfReference> SdfReferenceListOp;
324 typedef class SdfListOp<class SdfPayload> SdfPayloadListOp;
325 typedef class SdfListOp<class SdfUnregisteredValue> SdfUnregisteredValueListOp;
326 
327 PXR_NAMESPACE_CLOSE_SCOPE
328 
329 #endif // PXR_USD_SDF_LIST_OP_H
void swap(ArAssetInfo &lhs, ArAssetInfo &rhs)
Definition: assetInfo.h:74
const ItemVector & GetPrependedItems() const
Returns the explicit items.
Definition: listOp.h:139
std::function< boost::optional< ItemType >SdfListOpType, const ItemType &) > ApplyCallback
Callback type for ApplyOperations.
Definition: listOp.h:184
SDF_API const ItemVector & GetItems(SdfListOpType type) const
Return the item vector identified by type.
SDF_API SdfListOp()
Create an empty ListOp in non-explicit mode.
SDF_API void SetItems(const ItemVector &items, SdfListOpType type)
Sets the item vector for the given operation type.
bool HasKeys() const
Returns true if the editor has an explicit list (even if it's empty) or it has any added,...
Definition: listOp.h:103
const ItemVector & GetOrderedItems() const
Returns the ordered items.
Definition: listOp.h:157
SDF_API void ClearAndMakeExplicit()
Removes all items and changes the list to be explicit.
SDF_API void Clear()
Removes all items and changes the list to be non-explicit.
const ItemVector & GetAppendedItems() const
Returns the explicit items.
Definition: listOp.h:145
std::function< boost::optional< ItemType >const ItemType &) > ModifyCallback
Callback type for ModifyOperations.
Definition: listOp.h:212
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
SDF_API bool HasItem(const T &item) const
Returns true if the given item is in any of the item lists.
Value type representing a list-edit operation.
Definition: listOp.h:75
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
static SDF_API SdfListOp CreateExplicit(const ItemVector &explicitItems=ItemVector())
Create a ListOp in explicit mode with the given explicitItems.
SDF_API bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n, const ItemVector &newItems)
Replaces the items in the specified operation vector in the range (index, index + n] with the given n...
const ItemVector & GetDeletedItems() const
Returns the deleted items.
Definition: listOp.h:151
static SDF_API SdfListOp Create(const ItemVector &prependedItems=ItemVector(), const ItemVector &appendedItems=ItemVector(), const ItemVector &deletedItems=ItemVector())
Create a ListOp in non-explicit mode with the given prependedItems, appendedItems,...
SDF_API void ComposeOperations(const SdfListOp< T > &stronger, SdfListOpType op)
Composes a stronger SdfListOp's opinions for a given operation list over this one.
SDF_API bool ModifyOperations(const ModifyCallback &callback, bool removeDuplicates=false)
Modifies operations specified in this object.
const ItemVector & GetExplicitItems() const
Returns the explicit items.
Definition: listOp.h:127
const ItemVector & GetAddedItems() const
Returns the explicit items.
Definition: listOp.h:133
bool IsExplicit() const
Returns true if the list is explicit.
Definition: listOp.h:121
SDF_API void ApplyOperations(ItemVector *vec, const ApplyCallback &cb=ApplyCallback()) const
Applies edit operations to the given ItemVector.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...