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
15#include <functional>
16#include <iosfwd>
17#include <list>
18#include <map>
19#include <optional>
20#include <string>
21#include <vector>
22
23PXR_NAMESPACE_OPEN_SCOPE
24
29enum SdfListOpType {
30 SdfListOpTypeExplicit,
31 SdfListOpTypeAdded,
32 SdfListOpTypeDeleted,
33 SdfListOpTypeOrdered,
34 SdfListOpTypePrepended,
35 SdfListOpTypeAppended
36};
37
43template <class T>
44struct Sdf_ListOpTraits
45{
46 typedef std::less<T> ItemComparator;
47};
48
67
68template <typename T>
69class SdfListOp {
70public:
71 typedef T ItemType;
72 typedef std::vector<ItemType> ItemVector;
73 typedef ItemType value_type;
74 typedef ItemVector value_vector_type;
75
77 SDF_API
79 const ItemVector& explicitItems = ItemVector());
80
83 SDF_API
85 const ItemVector& prependedItems = ItemVector(),
86 const ItemVector& appendedItems = ItemVector(),
87 const ItemVector& deletedItems = ItemVector());
88
90 SDF_API SdfListOp();
91
92 SDF_API void Swap(SdfListOp<T>& rhs);
93
97 bool HasKeys() const
98 {
99 if (IsExplicit()) {
100 return true;
101 }
102 if (_addedItems.size() != 0 ||
103 _prependedItems.size() != 0 ||
104 _appendedItems.size() != 0 ||
105 _deletedItems.size() != 0) {
106 return true;
107 }
108 return _orderedItems.size() != 0;
109 }
110
112 SDF_API bool HasItem(const T& item) const;
113
115 bool IsExplicit() const
116 {
117 return _isExplicit;
118 }
119
121 const ItemVector& GetExplicitItems() const
122 {
123 return _explicitItems;
124 }
125
127 const ItemVector& GetPrependedItems() const
128 {
129 return _prependedItems;
130 }
131
133 const ItemVector& GetAppendedItems() const
134 {
135 return _appendedItems;
136 }
137
139 const ItemVector& GetDeletedItems() const
140 {
141 return _deletedItems;
142 }
143
145 SDF_API const ItemVector& GetItems(SdfListOpType type) const;
146
153 SDF_API ItemVector GetAppliedItems() const;
154
159 SDF_API bool SetExplicitItems(const ItemVector &items, std::string* errMsg = nullptr);
160
165 SDF_API bool SetPrependedItems(const ItemVector &items, std::string* errMsg = nullptr);
166
171 SDF_API bool SetAppendedItems(const ItemVector &items, std::string* errMsg = nullptr);
172
177 SDF_API bool SetDeletedItems(const ItemVector &items, std::string* errMsg = nullptr);
178
183 SDF_API bool SetItems(const ItemVector &items, SdfListOpType type,
184 std::string* errMsg = nullptr);
185
187 SDF_API void Clear();
188
190 SDF_API void ClearAndMakeExplicit();
191
193 typedef std::function<
194 std::optional<ItemType>(SdfListOpType, const ItemType&)
196
202 SDF_API
203 void ApplyOperations(ItemVector* vec,
204 const ApplyCallback& cb = ApplyCallback()) const;
205
216 SDF_API
217 std::optional<SdfListOp<T>>
218 ApplyOperations(const SdfListOp<T> &inner) const;
219
221 typedef std::function<
222 std::optional<ItemType>(const ItemType&)
224
235 SDF_API
236 bool ModifyOperations(const ModifyCallback& callback);
237
240 SDF_API
241 bool ModifyOperations(const ModifyCallback& callback,
242 bool unusedRemoveDuplicates);
243
247 SDF_API
248 bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n,
249 const ItemVector& newItems);
250
253 SDF_API
254 void ComposeOperations(const SdfListOp<T>& stronger, SdfListOpType op);
255
258 const ItemVector& GetAddedItems() const
259 {
260 return _addedItems;
261 }
262
265 const ItemVector& GetOrderedItems() const
266 {
267 return _orderedItems;
268 }
271 SDF_API void SetAddedItems(const ItemVector &items);
272
275 SDF_API void SetOrderedItems(const ItemVector &items);
276
277 friend inline size_t hash_value(const SdfListOp &op) {
278 return TfHash::Combine(
279 op._isExplicit,
280 op._explicitItems,
281 op._addedItems,
282 op._prependedItems,
283 op._appendedItems,
284 op._deletedItems,
285 op._orderedItems
286 );
287 }
288
289 bool operator==(const SdfListOp<T> &rhs) const {
290 return _isExplicit == rhs._isExplicit &&
291 _explicitItems == rhs._explicitItems &&
292 _addedItems == rhs._addedItems &&
293 _prependedItems == rhs._prependedItems &&
294 _appendedItems == rhs._appendedItems &&
295 _deletedItems == rhs._deletedItems &&
296 _orderedItems == rhs._orderedItems;
297 };
298
299 bool operator!=(const SdfListOp<T> &rhs) const {
300 return !(*this == rhs);
301 };
302
303private:
304 void _SetExplicit(bool isExplicit);
305
306 typedef typename Sdf_ListOpTraits<T>::ItemComparator _ItemComparator;
307 typedef std::list<ItemType> _ApplyList;
308 typedef std::map<ItemType, typename _ApplyList::iterator, _ItemComparator>
309 _ApplyMap;
310
311 void _PrependKeys(const ApplyCallback& cb,
312 _ApplyList* result, _ApplyMap* search) const;
313 void _AppendKeys(const ApplyCallback& cb,
314 _ApplyList* result, _ApplyMap* search) const;
315 void _DeleteKeys(const ApplyCallback& cb,
316 _ApplyList* result, _ApplyMap* search) const;
317
320 void _AddKeys(SdfListOpType, const ApplyCallback& cb,
321 _ApplyList* result, _ApplyMap* search) const;
322
325 void _ReorderKeys(const ApplyCallback& cb,
326 _ApplyList* result, _ApplyMap* search) const;
327 static void _ReorderKeysHelper(ItemVector order, const ApplyCallback& cb,
328 _ApplyList *result, _ApplyMap *search);
329 template <class ItemType>
330 friend void SdfApplyListOrdering(std::vector<ItemType> *v,
331 const std::vector<ItemType> &order);
332 bool _MakeUnique(std::vector<T>& items, bool reverse=false,
333 std::string* errMsg = nullptr);
334
335private:
336 bool _isExplicit;
337 ItemVector _explicitItems;
338 ItemVector _addedItems;
339 ItemVector _prependedItems;
340 ItemVector _appendedItems;
341 ItemVector _deletedItems;
342 ItemVector _orderedItems;
343};
344
345// ADL swap.
346template <class T>
347void swap(SdfListOp<T> &x, SdfListOp<T> &y)
348{
349 x.Swap(y);
350}
351
352// Helper function for applying an ordering operation described by \p orderVector
353// to vector \p v.
354template <class ItemType>
355SDF_API
356void SdfApplyListOrdering(std::vector<ItemType>* v,
357 const std::vector<ItemType>& order);
358
359// Ostream output methods for list values (useful for debugging and required
360// for storing a list value in a VtValue).
361template <typename T>
362SDF_API
363std::ostream & operator<<( std::ostream &, const SdfListOp<T> & );
364
365// Concrete, instantiated listop types.
366typedef class SdfListOp<int> SdfIntListOp;
367typedef class SdfListOp<unsigned int> SdfUIntListOp;
368typedef class SdfListOp<int64_t> SdfInt64ListOp;
369typedef class SdfListOp<uint64_t> SdfUInt64ListOp;
370typedef class SdfListOp<TfToken> SdfTokenListOp;
371typedef class SdfListOp<std::string> SdfStringListOp;
372typedef class SdfListOp<class SdfPath> SdfPathListOp;
373typedef class SdfListOp<class SdfReference> SdfReferenceListOp;
374typedef class SdfListOp<class SdfPayload> SdfPayloadListOp;
375typedef class SdfListOp<class SdfUnregisteredValue> SdfUnregisteredValueListOp;
376
377PXR_NAMESPACE_CLOSE_SCOPE
378
379#endif // PXR_USD_SDF_LIST_OP_H
SdfListOp is a value type representing an operation that edits a list.
Definition: listOp.h:69
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:223
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:265
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:139
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:115
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:127
const ItemVector & GetExplicitItems() const
Returns the explicit items.
Definition: listOp.h:121
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:97
const ItemVector & GetAppendedItems() const
Returns the explicit items.
Definition: listOp.h:133
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:195
SDF_API bool SetPrependedItems(const ItemVector &items, std::string *errMsg=nullptr)
Sets the prepended items.
const ItemVector & GetAddedItems() const
Definition: listOp.h:258
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.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...