HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
listOp.h
Go to the documentation of this file.
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 <hboost/optional/optional_fwd.hpp>
33 
34 #include <functional>
35 #include <iosfwd>
36 #include <list>
37 #include <map>
38 #include <string>
39 #include <vector>
40 
42 
43 /// \enum SdfListOpType
44 ///
45 /// Enum for specifying one of the list editing operation types.
46 ///
54 };
55 
56 /// \struct Sdf_ListOpTraits
57 ///
58 /// Trait classes for specializing behaviors of SdfListOp for a given item
59 /// type.
60 ///
61 template <class T>
63 {
64  typedef std::less<T> ItemComparator;
65 };
66 
67 /// \class SdfListOp
68 ///
69 /// Value type representing a list-edit operation.
70 ///
71 /// SdfListOp is a value type representing an operation that edits a list.
72 /// It may add or remove items, reorder them, or replace the list entirely.
73 ///
74 template <typename T>
75 class SdfListOp {
76 public:
77  typedef T ItemType;
78  typedef std::vector<ItemType> ItemVector;
81 
82  /// Create a ListOp in explicit mode with the given \p explicitItems.
83  SDF_API
85  const ItemVector& explicitItems = ItemVector());
86 
87  /// Create a ListOp in non-explicit mode with the given
88  /// \p prependedItems, \p appendedItems, and \p deletedItems
89  SDF_API
90  static SdfListOp Create(
91  const ItemVector& prependedItems = ItemVector(),
92  const ItemVector& appendedItems = ItemVector(),
93  const ItemVector& deletedItems = ItemVector());
94 
95  /// Create an empty ListOp in non-explicit mode.
97 
98  SDF_API void Swap(SdfListOp<T>& rhs);
99 
100  /// Returns \c true if the editor has an explicit list (even if it's
101  /// empty) or it has any added, prepended, appended, deleted,
102  /// or ordered keys.
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 
117  /// Returns \c true if the given item is in any of the item lists.
118  SDF_API bool HasItem(const T& item) const;
119 
120  /// Returns \c true if the list is explicit.
121  bool IsExplicit() const
122  {
123  return _isExplicit;
124  }
125 
126  /// Returns the explicit items.
128  {
129  return _explicitItems;
130  }
131 
132  /// Returns the explicit items.
133  const ItemVector& GetAddedItems() const
134  {
135  return _addedItems;
136  }
137 
138  /// Returns the explicit items.
140  {
141  return _prependedItems;
142  }
143 
144  /// Returns the explicit items.
146  {
147  return _appendedItems;
148  }
149 
150  /// Returns the deleted items.
152  {
153  return _deletedItems;
154  }
155 
156  /// Returns the ordered items.
158  {
159  return _orderedItems;
160  }
161 
162  /// Return the item vector identified by \p type.
164 
165  /// Returns the effective list of items represented by the operations in
166  /// this list op. This function should be used to determine the final list
167  /// of items added instead of looking at the individual explicit, prepended,
168  /// and appended item lists.
169  ///
170  /// This is equivalent to calling ApplyOperations on an empty item vector.
172 
173  SDF_API void SetExplicitItems(const ItemVector &items);
174  SDF_API void SetAddedItems(const ItemVector &items);
175  SDF_API void SetPrependedItems(const ItemVector &items);
176  SDF_API void SetAppendedItems(const ItemVector &items);
177  SDF_API void SetDeletedItems(const ItemVector &items);
178  SDF_API void SetOrderedItems(const ItemVector &items);
179 
180  /// Sets the item vector for the given operation \p type.
181  SDF_API void SetItems(const ItemVector &items, SdfListOpType type);
182 
183  /// Removes all items and changes the list to be non-explicit.
184  SDF_API void Clear();
185 
186  /// Removes all items and changes the list to be explicit.
188 
189  /// Callback type for ApplyOperations.
190  typedef std::function<
191  hboost::optional<ItemType>(SdfListOpType, const ItemType&)
193 
194  /// Applies edit operations to the given ItemVector.
195  /// If supplied, \p cb will be called on each item in the operation vectors
196  /// before they are applied to \p vec. Consumers can use this to transform
197  /// the items stored in the operation vectors to match what's stored in
198  /// \p vec.
199  SDF_API
200  void ApplyOperations(ItemVector* vec,
201  const ApplyCallback& cb = ApplyCallback()) const;
202 
203  /// Applies edit operations to the given ListOp.
204  ///
205  /// The result is a ListOp that, when applied to a list, has the same
206  /// effect as applying \p inner and then \p this in sequence.
207  ///
208  /// The result will be empty if the result is not well defined.
209  /// The result is well-defined when \p inner and \p this do not
210  /// use the 'ordered' or 'added' item lists. In other words, only
211  /// the explicit, prepended, appended, and deleted portions of
212  /// SdfListOp are closed under composition with ApplyOperations().
213  SDF_API
214  hboost::optional<SdfListOp<T>>
215  ApplyOperations(const SdfListOp<T> &inner) const;
216 
217  /// Callback type for ModifyOperations.
218  typedef std::function<
219  hboost::optional<ItemType>(const ItemType&)
221 
222  /// Modifies operations specified in this object.
223  /// \p callback is called for every item in all operation vectors. If the
224  /// returned key is empty then the key is removed, otherwise it's replaced
225  /// with the returned key.
226  ///
227  /// If \p removeDuplicates is \c true and \p callback returns a key that was
228  /// previously returned for the current operation vector being processed,
229  /// the returned key will be removed.
230  ///
231  /// Returns true if a change was made, false otherwise.
232  SDF_API
233  bool ModifyOperations(const ModifyCallback& callback,
234  bool removeDuplicates = false);
235 
236  /// Replaces the items in the specified operation vector in the range
237  /// (index, index + n] with the given \p newItems. If \p newItems is empty
238  /// the items in the range will simply be removed.
239  SDF_API
240  bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n,
241  const ItemVector& newItems);
242 
243  /// Composes a stronger SdfListOp's opinions for a given operation list
244  /// over this one.
245  SDF_API
246  void ComposeOperations(const SdfListOp<T>& stronger, SdfListOpType op);
247 
248  friend inline size_t hash_value(const SdfListOp &op) {
249  return TfHash::Combine(
250  op._isExplicit,
251  op._explicitItems,
252  op._addedItems,
253  op._prependedItems,
254  op._appendedItems,
255  op._deletedItems,
256  op._orderedItems
257  );
258  }
259 
260  bool operator==(const SdfListOp<T> &rhs) const {
261  return _isExplicit == rhs._isExplicit &&
262  _explicitItems == rhs._explicitItems &&
263  _addedItems == rhs._addedItems &&
264  _prependedItems == rhs._prependedItems &&
265  _appendedItems == rhs._appendedItems &&
266  _deletedItems == rhs._deletedItems &&
267  _orderedItems == rhs._orderedItems;
268  };
269 
270  bool operator!=(const SdfListOp<T> &rhs) const {
271  return !(*this == rhs);
272  };
273 
274 private:
275  void _SetExplicit(bool isExplicit);
276 
277  typedef typename Sdf_ListOpTraits<T>::ItemComparator _ItemComparator;
278  typedef std::list<ItemType> _ApplyList;
279  typedef std::map<ItemType, typename _ApplyList::iterator, _ItemComparator>
280  _ApplyMap;
281 
282  void _AddKeys(SdfListOpType, const ApplyCallback& cb,
283  _ApplyList* result, _ApplyMap* search) const;
284  void _PrependKeys(SdfListOpType, const ApplyCallback& cb,
285  _ApplyList* result, _ApplyMap* search) const;
286  void _AppendKeys(SdfListOpType, const ApplyCallback& cb,
287  _ApplyList* result, _ApplyMap* search) const;
288  void _DeleteKeys(SdfListOpType, const ApplyCallback& cb,
289  _ApplyList* result, _ApplyMap* search) const;
290  void _ReorderKeys(SdfListOpType, const ApplyCallback& cb,
291  _ApplyList* result, _ApplyMap* search) const;
292 
293 private:
294  bool _isExplicit;
295  ItemVector _explicitItems;
296  ItemVector _addedItems;
297  ItemVector _prependedItems;
298  ItemVector _appendedItems;
299  ItemVector _deletedItems;
300  ItemVector _orderedItems;
301 };
302 
303 // ADL swap.
304 template <class T>
306 {
307  x.Swap(y);
308 }
309 
310 // Helper function for applying an ordering operation described by \p orderVector
311 // to vector \p v.
312 template <class ItemType>
313 SDF_API
314 void SdfApplyListOrdering(std::vector<ItemType>* v,
315  const std::vector<ItemType>& order);
316 
317 // Ostream output methods for list values (useful for debugging and required
318 // for storing a list value in a VtValue).
319 template <typename T>
320 SDF_API
321 std::ostream & operator<<( std::ostream &, const SdfListOp<T> & );
322 
323 // Concrete, instantiated listop types.
324 typedef class SdfListOp<int> SdfIntListOp;
325 typedef class SdfListOp<unsigned int> SdfUIntListOp;
326 typedef class SdfListOp<int64_t> SdfInt64ListOp;
327 typedef class SdfListOp<uint64_t> SdfUInt64ListOp;
329 typedef class SdfListOp<std::string> SdfStringListOp;
330 typedef class SdfListOp<class SdfPath> SdfPathListOp;
331 typedef class SdfListOp<class SdfReference> SdfReferenceListOp;
332 typedef class SdfListOp<class SdfPayload> SdfPayloadListOp;
333 typedef class SdfListOp<class SdfUnregisteredValue> SdfUnregisteredValueListOp;
334 
336 
337 #endif // PXR_USD_SDF_LIST_OP_H
void swap(ArAssetInfo &lhs, ArAssetInfo &rhs)
Definition: assetInfo.h:74
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
ItemVector value_vector_type
Definition: listOp.h:80
const ItemVector & GetPrependedItems() const
Returns the explicit items.
Definition: listOp.h:139
SDF_API void ComposeOperations(const SdfListOp< T > &stronger, SdfListOpType op)
const ItemVector & GetExplicitItems() const
Returns the explicit items.
Definition: listOp.h:127
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
const ItemVector & GetAppendedItems() const
Returns the explicit items.
Definition: listOp.h:145
const GLdouble * v
Definition: glcorearb.h:837
SDF_API const ItemVector & GetItems(SdfListOpType type) const
Return the item vector identified by type.
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
SDF_API bool ReplaceOperations(const SdfListOpType op, size_t index, size_t n, const ItemVector &newItems)
class SdfListOp< class SdfReference > SdfReferenceListOp
Definition: listOp.h:331
SDF_API void ClearAndMakeExplicit()
Removes all items and changes the list to be explicit.
GLint y
Definition: glcorearb.h:103
**But if you need a result
Definition: thread.h:613
class SdfListOp< uint64_t > SdfUInt64ListOp
Definition: listOp.h:327
SDF_API void Swap(SdfListOp< T > &rhs)
class SdfListOp< class SdfPayload > SdfPayloadListOp
Definition: listOp.h:332
class SdfListOp< class SdfUnregisteredValue > SdfUnregisteredValueListOp
Definition: listOp.h:333
class SdfListOp< class SdfPath > SdfPathListOp
Definition: listOp.h:330
GLdouble n
Definition: glcorearb.h:2008
Definition: token.h:87
T ItemType
Definition: listOp.h:77
class SdfListOp< int > SdfIntListOp
Definition: listOp.h:324
const ItemVector & GetOrderedItems() const
Returns the ordered items.
Definition: listOp.h:157
const ItemVector & GetAddedItems() const
Returns the explicit items.
Definition: listOp.h:133
SdfListOpType
Definition: listOp.h:47
bool HasKeys() const
Definition: listOp.h:103
std::function< hboost::optional< ItemType >SdfListOpType, const ItemType &) > ApplyCallback
Callback type for ApplyOperations.
Definition: listOp.h:192
const ItemVector & GetDeletedItems() const
Returns the deleted items.
Definition: listOp.h:151
SDF_API ItemVector GetAppliedItems() const
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
class SdfListOp< std::string > SdfStringListOp
Definition: listOp.h:329
SDF_API void SetOrderedItems(const ItemVector &items)
SDF_API void SetExplicitItems(const ItemVector &items)
SDF_API void Clear()
Removes all items and changes the list to be non-explicit.
class SdfListOp< int64_t > SdfInt64ListOp
Definition: listOp.h:326
class SdfListOp< TfToken > SdfTokenListOp
Definition: listOp.h:328
GLint GLenum GLint x
Definition: glcorearb.h:409
auto search(const T &set, const V &val) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
A search function.
Definition: CLI11.h:3170
class SdfListOp< unsigned int > SdfUIntListOp
Definition: listOp.h:325
SDF_API SdfListOp()
Create an empty ListOp in non-explicit mode.
bool operator!=(const SdfListOp< T > &rhs) const
Definition: listOp.h:270
#define SDF_API
Definition: api.h:40
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
SDF_API void SetAddedItems(const ItemVector &items)
SDF_API void ApplyOperations(ItemVector *vec, const ApplyCallback &cb=ApplyCallback()) const
static SDF_API SdfListOp CreateExplicit(const ItemVector &explicitItems=ItemVector())
Create a ListOp in explicit mode with the given explicitItems.
SDF_API void SdfApplyListOrdering(std::vector< ItemType > *v, const std::vector< ItemType > &order)
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SDF_API bool ModifyOperations(const ModifyCallback &callback, bool removeDuplicates=false)
std::vector< ItemType > ItemVector
Definition: listOp.h:78
SDF_API void SetDeletedItems(const ItemVector &items)
SDF_API void SetItems(const ItemVector &items, SdfListOpType type)
Sets the item vector for the given operation type.
std::function< hboost::optional< ItemType >const ItemType &) > ModifyCallback
Callback type for ModifyOperations.
Definition: listOp.h:220
static SDF_API SdfListOp Create(const ItemVector &prependedItems=ItemVector(), const ItemVector &appendedItems=ItemVector(), const ItemVector &deletedItems=ItemVector())
SDF_API void SetPrependedItems(const ItemVector &items)
bool operator==(const SdfListOp< T > &rhs) const
Definition: listOp.h:260
std::less< T > ItemComparator
Definition: listOp.h:64
ItemType value_type
Definition: listOp.h:79
type
Definition: core.h:1059
SDF_API bool HasItem(const T &item) const
Returns true if the given item is in any of the item lists.
bool IsExplicit() const
Returns true if the list is explicit.
Definition: listOp.h:121
SDF_API void SetAppendedItems(const ItemVector &items)
friend size_t hash_value(const SdfListOp &op)
Definition: listOp.h:248