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
56template <typename T>
57class SdfListOp {
58public:
59 typedef T ItemType;
60 typedef std::vector<ItemType> ItemVector;
61 typedef ItemType value_type;
62 typedef ItemVector value_vector_type;
63
65 SDF_API
67 const ItemVector& explicitItems = ItemVector());
68
71 SDF_API
73 const ItemVector& prependedItems = ItemVector(),
74 const ItemVector& appendedItems = ItemVector(),
75 const ItemVector& deletedItems = ItemVector());
76
78 SDF_API SdfListOp();
79
80 SDF_API void Swap(SdfListOp<T>& rhs);
81
85 bool HasKeys() const
86 {
87 if (IsExplicit()) {
88 return true;
89 }
90 if (_addedItems.size() != 0 ||
91 _prependedItems.size() != 0 ||
92 _appendedItems.size() != 0 ||
93 _deletedItems.size() != 0) {
94 return true;
95 }
96 return _orderedItems.size() != 0;
97 }
98
100 SDF_API bool HasItem(const T& item) const;
101
103 bool IsExplicit() const
104 {
105 return _isExplicit;
106 }
107
109 const ItemVector& GetExplicitItems() const
110 {
111 return _explicitItems;
112 }
113
115 const ItemVector& GetAddedItems() const
116 {
117 return _addedItems;
118 }
119
121 const ItemVector& GetPrependedItems() const
122 {
123 return _prependedItems;
124 }
125
127 const ItemVector& GetAppendedItems() const
128 {
129 return _appendedItems;
130 }
131
133 const ItemVector& GetDeletedItems() const
134 {
135 return _deletedItems;
136 }
137
139 const ItemVector& GetOrderedItems() const
140 {
141 return _orderedItems;
142 }
143
145 SDF_API const ItemVector& GetItems(SdfListOpType type) const;
146
153 SDF_API ItemVector GetAppliedItems() const;
154
155 SDF_API void SetExplicitItems(const ItemVector &items);
156 SDF_API void SetAddedItems(const ItemVector &items);
157 SDF_API void SetPrependedItems(const ItemVector &items);
158 SDF_API void SetAppendedItems(const ItemVector &items);
159 SDF_API void SetDeletedItems(const ItemVector &items);
160 SDF_API void SetOrderedItems(const ItemVector &items);
161
163 SDF_API void SetItems(const ItemVector &items, SdfListOpType type);
164
166 SDF_API void Clear();
167
169 SDF_API void ClearAndMakeExplicit();
170
172 typedef std::function<
173 std::optional<ItemType>(SdfListOpType, const ItemType&)
175
181 SDF_API
182 void ApplyOperations(ItemVector* vec,
183 const ApplyCallback& cb = ApplyCallback()) const;
184
195 SDF_API
196 std::optional<SdfListOp<T>>
197 ApplyOperations(const SdfListOp<T> &inner) const;
198
200 typedef std::function<
201 std::optional<ItemType>(const ItemType&)
203
214 SDF_API
215 bool ModifyOperations(const ModifyCallback& callback,
216 bool removeDuplicates = false);
217
221 SDF_API
222 bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n,
223 const ItemVector& newItems);
224
227 SDF_API
228 void ComposeOperations(const SdfListOp<T>& stronger, SdfListOpType op);
229
230 friend inline size_t hash_value(const SdfListOp &op) {
231 return TfHash::Combine(
232 op._isExplicit,
233 op._explicitItems,
234 op._addedItems,
235 op._prependedItems,
236 op._appendedItems,
237 op._deletedItems,
238 op._orderedItems
239 );
240 }
241
242 bool operator==(const SdfListOp<T> &rhs) const {
243 return _isExplicit == rhs._isExplicit &&
244 _explicitItems == rhs._explicitItems &&
245 _addedItems == rhs._addedItems &&
246 _prependedItems == rhs._prependedItems &&
247 _appendedItems == rhs._appendedItems &&
248 _deletedItems == rhs._deletedItems &&
249 _orderedItems == rhs._orderedItems;
250 };
251
252 bool operator!=(const SdfListOp<T> &rhs) const {
253 return !(*this == rhs);
254 };
255
256private:
257 void _SetExplicit(bool isExplicit);
258
259 typedef typename Sdf_ListOpTraits<T>::ItemComparator _ItemComparator;
260 typedef std::list<ItemType> _ApplyList;
261 typedef std::map<ItemType, typename _ApplyList::iterator, _ItemComparator>
262 _ApplyMap;
263
264 void _AddKeys(SdfListOpType, const ApplyCallback& cb,
265 _ApplyList* result, _ApplyMap* search) const;
266 void _PrependKeys(SdfListOpType, const ApplyCallback& cb,
267 _ApplyList* result, _ApplyMap* search) const;
268 void _AppendKeys(SdfListOpType, const ApplyCallback& cb,
269 _ApplyList* result, _ApplyMap* search) const;
270 void _DeleteKeys(SdfListOpType, const ApplyCallback& cb,
271 _ApplyList* result, _ApplyMap* search) const;
272 void _ReorderKeys(SdfListOpType, const ApplyCallback& cb,
273 _ApplyList* result, _ApplyMap* search) const;
274
275private:
276 bool _isExplicit;
277 ItemVector _explicitItems;
278 ItemVector _addedItems;
279 ItemVector _prependedItems;
280 ItemVector _appendedItems;
281 ItemVector _deletedItems;
282 ItemVector _orderedItems;
283};
284
285// ADL swap.
286template <class T>
287void swap(SdfListOp<T> &x, SdfListOp<T> &y)
288{
289 x.Swap(y);
290}
291
292// Helper function for applying an ordering operation described by \p orderVector
293// to vector \p v.
294template <class ItemType>
295SDF_API
296void SdfApplyListOrdering(std::vector<ItemType>* v,
297 const std::vector<ItemType>& order);
298
299// Ostream output methods for list values (useful for debugging and required
300// for storing a list value in a VtValue).
301template <typename T>
302SDF_API
303std::ostream & operator<<( std::ostream &, const SdfListOp<T> & );
304
305// Concrete, instantiated listop types.
306typedef class SdfListOp<int> SdfIntListOp;
307typedef class SdfListOp<unsigned int> SdfUIntListOp;
308typedef class SdfListOp<int64_t> SdfInt64ListOp;
309typedef class SdfListOp<uint64_t> SdfUInt64ListOp;
310typedef class SdfListOp<TfToken> SdfTokenListOp;
311typedef class SdfListOp<std::string> SdfStringListOp;
312typedef class SdfListOp<class SdfPath> SdfPathListOp;
313typedef class SdfListOp<class SdfReference> SdfReferenceListOp;
314typedef class SdfListOp<class SdfPayload> SdfPayloadListOp;
315typedef class SdfListOp<class SdfUnregisteredValue> SdfUnregisteredValueListOp;
316
317PXR_NAMESPACE_CLOSE_SCOPE
318
319#endif // PXR_USD_SDF_LIST_OP_H
Value type representing a list-edit operation.
Definition: listOp.h:57
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.
std::function< std::optional< ItemType >(const ItemType &) > ModifyCallback
Callback type for ModifyOperations.
Definition: listOp.h:202
const ItemVector & GetOrderedItems() const
Returns the ordered items.
Definition: listOp.h:139
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:133
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:103
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:121
const ItemVector & GetExplicitItems() const
Returns the explicit items.
Definition: listOp.h:109
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 SetItems(const ItemVector &items, SdfListOpType type)
Sets the item vector for the given operation type.
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 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:85
const ItemVector & GetAppendedItems() const
Returns the explicit items.
Definition: listOp.h:127
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:174
const ItemVector & GetAddedItems() const
Returns the explicit items.
Definition: listOp.h:115
SDF_API bool ModifyOperations(const ModifyCallback &callback, bool removeDuplicates=false)
Modifies operations specified in this object.
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:475
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...