Loading...
Searching...
No Matches
listOp.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_SDF_LIST_OP_H
8#define PXR_USD_SDF_LIST_OP_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/api.h"
12#include "pxr/base/tf/token.h"
13#include "pxr/base/tf/hash.h"
14#include "pxr/base/vt/traits.h"
15
16#include <functional>
17#include <iosfwd>
18#include <list>
19#include <map>
20#include <optional>
21#include <string>
22#include <vector>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
30enum SdfListOpType {
31 SdfListOpTypeExplicit,
32 SdfListOpTypeAdded,
33 SdfListOpTypeDeleted,
34 SdfListOpTypeOrdered,
35 SdfListOpTypePrepended,
36 SdfListOpTypeAppended
37};
38
44template <class T>
45struct Sdf_ListOpTraits
46{
47 typedef std::less<T> ItemComparator;
48};
49
68
69template <typename T>
70class SdfListOp {
71public:
72 typedef T ItemType;
73 typedef std::vector<ItemType> ItemVector;
74 typedef ItemType value_type;
75 typedef ItemVector value_vector_type;
76
78 SDF_API
80 const ItemVector& explicitItems = ItemVector());
81
84 SDF_API
86 const ItemVector& prependedItems = ItemVector(),
87 const ItemVector& appendedItems = ItemVector(),
88 const ItemVector& deletedItems = ItemVector());
89
91 SDF_API SdfListOp();
92
93 SDF_API void Swap(SdfListOp<T>& rhs);
94
98 bool HasKeys() const
99 {
100 if (IsExplicit()) {
101 return true;
102 }
103 if (_addedItems.size() != 0 ||
104 _prependedItems.size() != 0 ||
105 _appendedItems.size() != 0 ||
106 _deletedItems.size() != 0) {
107 return true;
108 }
109 return _orderedItems.size() != 0;
110 }
111
113 SDF_API bool HasItem(const T& item) const;
114
116 bool IsExplicit() const
117 {
118 return _isExplicit;
119 }
120
122 const ItemVector& GetExplicitItems() const
123 {
124 return _explicitItems;
125 }
126
128 const ItemVector& GetPrependedItems() const
129 {
130 return _prependedItems;
131 }
132
134 const ItemVector& GetAppendedItems() const
135 {
136 return _appendedItems;
137 }
138
140 const ItemVector& GetDeletedItems() const
141 {
142 return _deletedItems;
143 }
144
146 SDF_API const ItemVector& GetItems(SdfListOpType type) const;
147
154 SDF_API ItemVector GetAppliedItems() const;
155
160 SDF_API bool SetExplicitItems(const ItemVector &items, std::string* errMsg = nullptr);
161
166 SDF_API bool SetPrependedItems(const ItemVector &items, std::string* errMsg = nullptr);
167
172 SDF_API bool SetAppendedItems(const ItemVector &items, std::string* errMsg = nullptr);
173
178 SDF_API bool SetDeletedItems(const ItemVector &items, std::string* errMsg = nullptr);
179
184 SDF_API bool SetItems(const ItemVector &items, SdfListOpType type,
185 std::string* errMsg = nullptr);
186
188 SDF_API void Clear();
189
191 SDF_API void ClearAndMakeExplicit();
192
194 typedef std::function<
195 std::optional<ItemType>(SdfListOpType, const ItemType&)
197
203 SDF_API
204 void ApplyOperations(ItemVector* vec,
205 const ApplyCallback& cb = ApplyCallback()) const;
206
217 SDF_API
218 std::optional<SdfListOp<T>>
219 ApplyOperations(const SdfListOp<T> &inner) const;
220
222 typedef std::function<
223 std::optional<ItemType>(const ItemType&)
225
236 SDF_API
237 bool ModifyOperations(const ModifyCallback& callback);
238
241 SDF_API
242 bool ModifyOperations(const ModifyCallback& callback,
243 bool unusedRemoveDuplicates);
244
248 SDF_API
249 bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n,
250 const ItemVector& newItems);
251
254 SDF_API
255 void ComposeOperations(const SdfListOp<T>& stronger, SdfListOpType op);
256
259 const ItemVector& GetAddedItems() const
260 {
261 return _addedItems;
262 }
263
266 const ItemVector& GetOrderedItems() const
267 {
268 return _orderedItems;
269 }
272 SDF_API void SetAddedItems(const ItemVector &items);
273
276 SDF_API void SetOrderedItems(const ItemVector &items);
277
278 friend inline size_t hash_value(const SdfListOp &op) {
279 return TfHash::Combine(
280 op._isExplicit,
281 op._explicitItems,
282 op._addedItems,
283 op._prependedItems,
284 op._appendedItems,
285 op._deletedItems,
286 op._orderedItems
287 );
288 }
289
290 bool operator==(const SdfListOp<T> &rhs) const {
291 return _isExplicit == rhs._isExplicit &&
292 _explicitItems == rhs._explicitItems &&
293 _addedItems == rhs._addedItems &&
294 _prependedItems == rhs._prependedItems &&
295 _appendedItems == rhs._appendedItems &&
296 _deletedItems == rhs._deletedItems &&
297 _orderedItems == rhs._orderedItems;
298 };
299
300 bool operator!=(const SdfListOp<T> &rhs) const {
301 return !(*this == rhs);
302 };
303
304private:
305 void _SetExplicit(bool isExplicit);
306
307 typedef typename Sdf_ListOpTraits<T>::ItemComparator _ItemComparator;
308 typedef std::list<ItemType> _ApplyList;
309 typedef std::map<ItemType, typename _ApplyList::iterator, _ItemComparator>
310 _ApplyMap;
311
312 void _PrependKeys(const ApplyCallback& cb,
313 _ApplyList* result, _ApplyMap* search) const;
314 void _AppendKeys(const ApplyCallback& cb,
315 _ApplyList* result, _ApplyMap* search) const;
316 void _DeleteKeys(const ApplyCallback& cb,
317 _ApplyList* result, _ApplyMap* search) const;
318
321 void _AddKeys(SdfListOpType, const ApplyCallback& cb,
322 _ApplyList* result, _ApplyMap* search) const;
323
326 void _ReorderKeys(const ApplyCallback& cb,
327 _ApplyList* result, _ApplyMap* search) const;
328 static void _ReorderKeysHelper(ItemVector order, const ApplyCallback& cb,
329 _ApplyList *result, _ApplyMap *search);
330 template <class ItemType>
331 friend void SdfApplyListOrdering(std::vector<ItemType> *v,
332 const std::vector<ItemType> &order);
333 bool _MakeUnique(std::vector<T>& items, bool reverse=false,
334 std::string* errMsg = nullptr);
335
336private:
337 bool _isExplicit;
338 ItemVector _explicitItems;
339 ItemVector _addedItems;
340 ItemVector _prependedItems;
341 ItemVector _appendedItems;
342 ItemVector _deletedItems;
343 ItemVector _orderedItems;
344};
345
346// SdfListOps can VtValue-compose.
347template <class T>
348struct VtValueTypeCanCompose<SdfListOp<T>> : std::true_type {};
349
350// ADL swap.
351template <class T>
352void swap(SdfListOp<T> &x, SdfListOp<T> &y)
353{
354 x.Swap(y);
355}
356
357// Helper function for applying an ordering operation described by \p orderVector
358// to vector \p v.
359template <class ItemType>
360SDF_API
361void SdfApplyListOrdering(std::vector<ItemType>* v,
362 const std::vector<ItemType>& order);
363
364// Ostream output methods for list values (useful for debugging and required
365// for storing a list value in a VtValue).
366template <typename T>
367SDF_API
368std::ostream & operator<<( std::ostream &, const SdfListOp<T> & );
369
370// Concrete, instantiated listop types.
371typedef class SdfListOp<int> SdfIntListOp;
372typedef class SdfListOp<unsigned int> SdfUIntListOp;
373typedef class SdfListOp<int64_t> SdfInt64ListOp;
374typedef class SdfListOp<uint64_t> SdfUInt64ListOp;
375typedef class SdfListOp<TfToken> SdfTokenListOp;
376typedef class SdfListOp<std::string> SdfStringListOp;
377typedef class SdfListOp<class SdfPath> SdfPathListOp;
378typedef class SdfListOp<class SdfReference> SdfReferenceListOp;
379typedef class SdfListOp<class SdfPayload> SdfPayloadListOp;
380typedef class SdfListOp<class SdfUnregisteredValue> SdfUnregisteredValueListOp;
381
382PXR_NAMESPACE_CLOSE_SCOPE
383
384#endif // PXR_USD_SDF_LIST_OP_H
SdfListOp is a value type representing an operation that edits a list.
Definition: listOp.h:70
SDF_API bool SetDeletedItems(const ItemVector &items, std::string *errMsg=nullptr)
Sets the deleted items.
SDF_API bool HasItem(const T &item) const
Returns true if the given item is in any of the item lists.
static SDF_API SdfListOp CreateExplicit(const ItemVector &explicitItems=ItemVector())
Create a ListOp in explicit mode with the given explicitItems.
SDF_API std::optional< SdfListOp< T > > ApplyOperations(const SdfListOp< T > &inner) const
Applies edit operations to the given ListOp.
SDF_API bool ModifyOperations(const ModifyCallback &callback, bool unusedRemoveDuplicates)
SDF_API void SetOrderedItems(const ItemVector &items)
std::function< std::optional< ItemType >(const ItemType &) > ModifyCallback
Callback type for ModifyOperations.
Definition: listOp.h:224
SDF_API bool ModifyOperations(const ModifyCallback &callback)
Modifies operations specified in this object.
SDF_API bool SetItems(const ItemVector &items, SdfListOpType type, std::string *errMsg=nullptr)
Sets the item vector for the given operation type.
SDF_API void SetAddedItems(const ItemVector &items)
SDF_API bool SetAppendedItems(const ItemVector &items, std::string *errMsg=nullptr)
Sets the appended items.
const ItemVector & GetOrderedItems() const
Definition: listOp.h:266
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,...
const ItemVector & GetDeletedItems() const
Returns the deleted items.
Definition: listOp.h:140
SDF_API const ItemVector & GetItems(SdfListOpType type) const
Return the item vector identified by type.
bool IsExplicit() const
Returns true if the list is explicit.
Definition: listOp.h:116
SDF_API void Clear()
Removes all items and changes the list to be non-explicit.
const ItemVector & GetPrependedItems() const
Returns the explicit items.
Definition: listOp.h:128
const ItemVector & GetExplicitItems() const
Returns the explicit items.
Definition: listOp.h:122
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...
SDF_API SdfListOp()
Create an empty ListOp in non-explicit mode.
SDF_API void ApplyOperations(ItemVector *vec, const ApplyCallback &cb=ApplyCallback()) const
Applies edit operations to the given ItemVector.
SDF_API void ClearAndMakeExplicit()
Removes all items and changes the list to be explicit.
SDF_API bool SetExplicitItems(const ItemVector &items, std::string *errMsg=nullptr)
Sets the explicit items.
SDF_API void ComposeOperations(const SdfListOp< T > &stronger, SdfListOpType op)
Composes a stronger SdfListOp's opinions for a given operation list over this one.
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:98
const ItemVector & GetAppendedItems() const
Returns the explicit items.
Definition: listOp.h:134
SDF_API ItemVector GetAppliedItems() const
Returns the effective list of items represented by the operations in this list op.
std::function< std::optional< ItemType >(SdfListOpType, const ItemType &) > ApplyCallback
Callback type for ApplyOperations.
Definition: listOp.h:196
SDF_API bool SetPrependedItems(const ItemVector &items, std::string *errMsg=nullptr)
Sets the prepended items.
const ItemVector & GetAddedItems() const
Definition: listOp.h:259
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
STL namespace.
A trait indicating whether VtValue compose-over functionality can be registered for a type.
Definition: traits.h:137
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...