Loading...
Searching...
No Matches
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#include "pxr/base/tf/hash.h"
31
32#include <functional>
33#include <iosfwd>
34#include <list>
35#include <map>
36#include <optional>
37#include <string>
38#include <vector>
39
40PXR_NAMESPACE_OPEN_SCOPE
41
46enum SdfListOpType {
47 SdfListOpTypeExplicit,
48 SdfListOpTypeAdded,
49 SdfListOpTypeDeleted,
50 SdfListOpTypeOrdered,
51 SdfListOpTypePrepended,
52 SdfListOpTypeAppended
53};
54
60template <class T>
61struct Sdf_ListOpTraits
62{
63 typedef std::less<T> ItemComparator;
64};
65
73template <typename T>
74class SdfListOp {
75public:
76 typedef T ItemType;
77 typedef std::vector<ItemType> ItemVector;
78 typedef ItemType value_type;
79 typedef ItemVector value_vector_type;
80
82 SDF_API
84 const ItemVector& explicitItems = ItemVector());
85
88 SDF_API
90 const ItemVector& prependedItems = ItemVector(),
91 const ItemVector& appendedItems = ItemVector(),
92 const ItemVector& deletedItems = ItemVector());
93
95 SDF_API SdfListOp();
96
97 SDF_API void Swap(SdfListOp<T>& rhs);
98
102 bool HasKeys() const
103 {
104 if (IsExplicit()) {
105 return true;
106 }
107 if (_addedItems.size() != 0 ||
108 _prependedItems.size() != 0 ||
109 _appendedItems.size() != 0 ||
110 _deletedItems.size() != 0) {
111 return true;
112 }
113 return _orderedItems.size() != 0;
114 }
115
117 SDF_API bool HasItem(const T& item) const;
118
120 bool IsExplicit() const
121 {
122 return _isExplicit;
123 }
124
126 const ItemVector& GetExplicitItems() const
127 {
128 return _explicitItems;
129 }
130
132 const ItemVector& GetAddedItems() const
133 {
134 return _addedItems;
135 }
136
138 const ItemVector& GetPrependedItems() const
139 {
140 return _prependedItems;
141 }
142
144 const ItemVector& GetAppendedItems() const
145 {
146 return _appendedItems;
147 }
148
150 const ItemVector& GetDeletedItems() const
151 {
152 return _deletedItems;
153 }
154
156 const ItemVector& GetOrderedItems() const
157 {
158 return _orderedItems;
159 }
160
162 SDF_API const ItemVector& GetItems(SdfListOpType type) const;
163
170 SDF_API ItemVector GetAppliedItems() const;
171
172 SDF_API void SetExplicitItems(const ItemVector &items);
173 SDF_API void SetAddedItems(const ItemVector &items);
174 SDF_API void SetPrependedItems(const ItemVector &items);
175 SDF_API void SetAppendedItems(const ItemVector &items);
176 SDF_API void SetDeletedItems(const ItemVector &items);
177 SDF_API void SetOrderedItems(const ItemVector &items);
178
180 SDF_API void SetItems(const ItemVector &items, SdfListOpType type);
181
183 SDF_API void Clear();
184
186 SDF_API void ClearAndMakeExplicit();
187
189 typedef std::function<
190 std::optional<ItemType>(SdfListOpType, const ItemType&)
192
198 SDF_API
199 void ApplyOperations(ItemVector* vec,
200 const ApplyCallback& cb = ApplyCallback()) const;
201
212 SDF_API
213 std::optional<SdfListOp<T>>
214 ApplyOperations(const SdfListOp<T> &inner) const;
215
217 typedef std::function<
218 std::optional<ItemType>(const ItemType&)
220
231 SDF_API
232 bool ModifyOperations(const ModifyCallback& callback,
233 bool removeDuplicates = false);
234
238 SDF_API
239 bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n,
240 const ItemVector& newItems);
241
244 SDF_API
245 void ComposeOperations(const SdfListOp<T>& stronger, SdfListOpType op);
246
247 friend inline size_t hash_value(const SdfListOp &op) {
248 return TfHash::Combine(
249 op._isExplicit,
250 op._explicitItems,
251 op._addedItems,
252 op._prependedItems,
253 op._appendedItems,
254 op._deletedItems,
255 op._orderedItems
256 );
257 }
258
259 bool operator==(const SdfListOp<T> &rhs) const {
260 return _isExplicit == rhs._isExplicit &&
261 _explicitItems == rhs._explicitItems &&
262 _addedItems == rhs._addedItems &&
263 _prependedItems == rhs._prependedItems &&
264 _appendedItems == rhs._appendedItems &&
265 _deletedItems == rhs._deletedItems &&
266 _orderedItems == rhs._orderedItems;
267 };
268
269 bool operator!=(const SdfListOp<T> &rhs) const {
270 return !(*this == rhs);
271 };
272
273private:
274 void _SetExplicit(bool isExplicit);
275
276 typedef typename Sdf_ListOpTraits<T>::ItemComparator _ItemComparator;
277 typedef std::list<ItemType> _ApplyList;
278 typedef std::map<ItemType, typename _ApplyList::iterator, _ItemComparator>
279 _ApplyMap;
280
281 void _AddKeys(SdfListOpType, const ApplyCallback& cb,
282 _ApplyList* result, _ApplyMap* search) const;
283 void _PrependKeys(SdfListOpType, const ApplyCallback& cb,
284 _ApplyList* result, _ApplyMap* search) const;
285 void _AppendKeys(SdfListOpType, const ApplyCallback& cb,
286 _ApplyList* result, _ApplyMap* search) const;
287 void _DeleteKeys(SdfListOpType, const ApplyCallback& cb,
288 _ApplyList* result, _ApplyMap* search) const;
289 void _ReorderKeys(SdfListOpType, const ApplyCallback& cb,
290 _ApplyList* result, _ApplyMap* search) const;
291
292private:
293 bool _isExplicit;
294 ItemVector _explicitItems;
295 ItemVector _addedItems;
296 ItemVector _prependedItems;
297 ItemVector _appendedItems;
298 ItemVector _deletedItems;
299 ItemVector _orderedItems;
300};
301
302// ADL swap.
303template <class T>
304void swap(SdfListOp<T> &x, SdfListOp<T> &y)
305{
306 x.Swap(y);
307}
308
309// Helper function for applying an ordering operation described by \p orderVector
310// to vector \p v.
311template <class ItemType>
312SDF_API
313void SdfApplyListOrdering(std::vector<ItemType>* v,
314 const std::vector<ItemType>& order);
315
316// Ostream output methods for list values (useful for debugging and required
317// for storing a list value in a VtValue).
318template <typename T>
319SDF_API
320std::ostream & operator<<( std::ostream &, const SdfListOp<T> & );
321
322// Concrete, instantiated listop types.
323typedef class SdfListOp<int> SdfIntListOp;
324typedef class SdfListOp<unsigned int> SdfUIntListOp;
325typedef class SdfListOp<int64_t> SdfInt64ListOp;
326typedef class SdfListOp<uint64_t> SdfUInt64ListOp;
327typedef class SdfListOp<TfToken> SdfTokenListOp;
328typedef class SdfListOp<std::string> SdfStringListOp;
329typedef class SdfListOp<class SdfPath> SdfPathListOp;
330typedef class SdfListOp<class SdfReference> SdfReferenceListOp;
331typedef class SdfListOp<class SdfPayload> SdfPayloadListOp;
332typedef class SdfListOp<class SdfUnregisteredValue> SdfUnregisteredValueListOp;
333
334PXR_NAMESPACE_CLOSE_SCOPE
335
336#endif // PXR_USD_SDF_LIST_OP_H
Value type representing a list-edit operation.
Definition: listOp.h:74
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:219
const ItemVector & GetOrderedItems() const
Returns the ordered items.
Definition: listOp.h:156
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:150
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:120
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:138
const ItemVector & GetExplicitItems() const
Returns the explicit items.
Definition: listOp.h:126
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:102
const ItemVector & GetAppendedItems() const
Returns the explicit items.
Definition: listOp.h:144
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:191
const ItemVector & GetAddedItems() const
Returns the explicit items.
Definition: listOp.h:132
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:492
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
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...