All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dataSourceLocator.h
1//
2// Copyright 2021 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_IMAGING_HD_DATASOURCELOCATOR_H
8#define PXR_IMAGING_HD_DATASOURCELOCATOR_H
9
10#include "pxr/pxr.h"
11#include "pxr/base/tf/token.h"
13#include "pxr/base/tf/hash.h"
14
15#include "pxr/imaging/hd/api.h"
16
17#include <iosfwd>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
28{
29public:
30
35 HD_API
37
42 HD_API
44
52 HD_API
53 explicit HdDataSourceLocator(const TfToken &t1);
54 HD_API
55 HdDataSourceLocator(const TfToken &t1, const TfToken &t2);
56 HD_API
57 HdDataSourceLocator(const TfToken &t1, const TfToken &t2,
58 const TfToken &t3);
59 HD_API
60 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
61 const TfToken &t4);
62 HD_API
63 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
64 const TfToken &t4, const TfToken &t5);
65 HD_API
66 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
67 const TfToken &t4, const TfToken &t5,
68 const TfToken &t6);
69
73 HD_API
74 HdDataSourceLocator(size_t count, const TfToken *tokens);
75
78
80 HD_API
81 size_t GetElementCount() const;
82
87 HD_API
88 const TfToken &GetElement(size_t i) const;
89
91 HD_API
92 const TfToken &GetFirstElement() const;
93
95 HD_API
96 const TfToken &GetLastElement() const;
97
102 HD_API
104
108 HD_API
110
114 HD_API
116
118 HD_API
120
122 HD_API
124
126 HD_API
128
130 HD_API
132
136 HD_API
137 bool HasPrefix(const HdDataSourceLocator &prefix) const;
138
142 HD_API
144
148 HD_API
150 const HdDataSourceLocator &oldPrefix,
151 const HdDataSourceLocator &newPrefix) const;
152
157 HD_API
158 bool Intersects(const HdDataSourceLocator &other) const;
159
160 inline bool operator==(const HdDataSourceLocator &rhs) const {
161 return _tokens == rhs._tokens;
162 }
163
164 inline bool operator!=(const HdDataSourceLocator &rhs) const {
165 return _tokens != rhs._tokens;
166 }
167
169 HD_API
170 bool operator<(const HdDataSourceLocator &rhs) const;
171
172 inline bool IsEmpty() const {
173 return _tokens.empty();
174 }
175
179 HD_API
180 std::string GetString(const char *delimiter = "/") const;
181
182 template <class HashState>
183 friend void TfHashAppend(HashState &h, HdDataSourceLocator const &myObj) {
184 h.AppendContiguous(myObj._tokens.data(), myObj._tokens.size());
185 }
186
187 inline size_t Hash() const;
188
189private:
190 using _TokenVector = TfSmallVector<TfToken, 6>;
191 _TokenVector _tokens;
192};
193
194inline size_t
195HdDataSourceLocator::Hash() const
196{
197 return TfHash()(*this);
198}
199
200HD_API std::ostream& operator<<(std::ostream& out,
201 const HdDataSourceLocator &self);
202
203//-----------------------------------------------------------------------------
204
220{
221private:
223public:
224 using const_iterator = typename _Locators::const_iterator;
225
228
230 HD_API
232
233 HD_API
235
236 // Initializer list constructor.
237 HD_API
239 const std::initializer_list<const HdDataSourceLocator> &l);
240
243
246
249
252 = default;
253
254 HD_API
255 void insert(const HdDataSourceLocator &locator);
256
258 HD_API
259 void insert(const HdDataSourceLocatorSet &locatorSet);
260
262 HD_API
263 void insert(HdDataSourceLocatorSet &&locatorSet);
264
267 HD_API
268 void append(const HdDataSourceLocator &locator);
269
270 bool operator==(const HdDataSourceLocatorSet &rhs) const {
271 return _locators == rhs._locators;
272 }
273
274 bool operator!=(const HdDataSourceLocatorSet &rhs) const {
275 return !(*this == rhs);
276 }
277
280 HD_API
281 const_iterator begin() const;
282 HD_API
283 const_iterator end() const;
284
291 HD_API
292 bool Intersects(const HdDataSourceLocator &locator) const;
293
300 HD_API
301 bool Intersects(const HdDataSourceLocatorSet &locatorSet) const;
302
304 HD_API
305 bool IsEmpty() const;
306
312 HD_API
313 bool Contains(const HdDataSourceLocator &locator) const;
314
319 HD_API
321 const HdDataSourceLocator &oldPrefix,
322 const HdDataSourceLocator &newPrefix) const;
323
324 class IntersectionIterator;
325 class IntersectionView;
326
338 HD_API
339 IntersectionView Intersection(const HdDataSourceLocator &locator) const;
340
341private:
342 // Sort and uniquify it.
343 void _Normalize();
344
345 void _InsertAndDeleteSuffixes(_Locators::iterator *position,
346 const HdDataSourceLocator &locator);
347
348 const_iterator _FirstIntersection(const HdDataSourceLocator &locator) const;
349
350 // Lexicographically sorted minimal list of locators generating
351 // the set.
352 _Locators _locators;
353};
354
355class HdDataSourceLocatorSet::IntersectionIterator
356{
357public:
358 using iterator_category = std::forward_iterator_tag;
359 using value_type = const HdDataSourceLocator;
360 using reference = value_type&;
361 using pointer = value_type*;
362 using difference_type = std::ptrdiff_t;
363
364 IntersectionIterator()
365 : _isFirst(false)
366 {
367 }
368
369 IntersectionIterator(const bool isFirst,
370 const const_iterator &iterator,
371 const const_iterator &end,
372 const HdDataSourceLocator &locator)
373 : _isFirst(isFirst)
374 , _iterator(iterator)
375 , _end(end)
376 , _locator(locator)
377 {
378 }
379
380 HD_API
381 const HdDataSourceLocator &operator*() const;
382
383 const HdDataSourceLocator* operator->() const
384 {
385 return std::addressof(**this);
386 }
387
388 HD_API
389 IntersectionIterator& operator++();
390
391 HD_API
392 IntersectionIterator operator++(int);
393
394 bool operator==(const IntersectionIterator &other) const noexcept
395 {
396 return _iterator == other._iterator;
397 }
398
399 bool operator!=(const IntersectionIterator &other) const noexcept
400 {
401 return _iterator != other._iterator;
402 }
403
404private:
405 bool _isFirst;
406 const_iterator _iterator;
407 const_iterator _end;
408 HdDataSourceLocator _locator;
409};
410
411class HdDataSourceLocatorSet::IntersectionView
412{
413public:
414 IntersectionView(const IntersectionIterator &begin,
415 const IntersectionIterator &end)
416 : _begin(begin)
417 , _end(end)
418 {
419 }
420
421 const IntersectionIterator &begin() const { return _begin; }
422
423 const IntersectionIterator &end() const { return _end; }
424
425private:
426 const IntersectionIterator _begin;
427 const IntersectionIterator _end;
428};
429
430HD_API std::ostream& operator<<(std::ostream& out,
431 const HdDataSourceLocatorSet &self);
432
433PXR_NAMESPACE_CLOSE_SCOPE
434
435#endif // PXR_IMAGING_HD_DATASOURCELOCATOR_H
Represents an object that can identify the location of a data source.
HD_API HdDataSourceLocator(const TfToken &t1)
The following constructors take a number of tokens and build a locator with the apporpriate number of...
HD_API HdDataSourceLocator Append(const TfToken &name) const
Appends name to this data source locator.
HD_API const TfToken & GetFirstElement() const
Returns the first element, or empty token if none.
HD_API HdDataSourceLocator Prepend(const TfToken &name) const
Prepends name to this data source locator.
HD_API bool Intersects(const HdDataSourceLocator &other) const
Returns true if and only if either of the two locators is a prefix of the other one - in the sense of...
HD_API HdDataSourceLocator()
Creates an empty locator.
HD_API size_t GetElementCount() const
Returns the number of elements (tokens) in this data source.
HD_API HdDataSourceLocator Append(const HdDataSourceLocator &locator) const
Appends all of the elements in locator to this data source locator.
HD_API bool HasPrefix(const HdDataSourceLocator &prefix) const
Returns true if and only if this data source locator has prefix as a prefix.
HD_API HdDataSourceLocator RemoveLastElement() const
Returns a copy of this data source locator with the last element removed.
HD_API HdDataSourceLocator ReplacePrefix(const HdDataSourceLocator &oldPrefix, const HdDataSourceLocator &newPrefix) const
Returns a copy of this data source locator with oldPrefix replaced by newPrefix.
HD_API HdDataSourceLocator GetCommonPrefix(const HdDataSourceLocator &other) const
Returns a data source locator that represents the common prefix between this data source and other.
HdDataSourceLocator(const HdDataSourceLocator &rhs)=default
Copy constructor.
HD_API const TfToken & GetElement(size_t i) const
Returns the element (token) at index i.
HD_API HdDataSourceLocator RemoveFirstElement() const
Returns a copy of this data source locator with the first element removed.
HD_API std::string GetString(const char *delimiter="/") const
Returns a string representation of this data source locator with the given delimiter inserted between...
HD_API HdDataSourceLocator ReplaceLastElement(const TfToken &name) const
Returns a copy of this data source locator with the last element replaced by the one given by name.
HD_API HdDataSourceLocator(size_t count, const TfToken *tokens)
Builds a data source locator from the tokens array of the given count.
HD_API HdDataSourceLocator Prepend(const HdDataSourceLocator &locator) const
Prepends all of the elements in locator to this data source locator.
static HD_API const HdDataSourceLocator & EmptyLocator()
Returns a common empty locator.
HD_API bool operator<(const HdDataSourceLocator &rhs) const
Lexicographic order. If y has x as prefix, x < y.
HD_API const TfToken & GetLastElement() const
Returns the last element, or empty token if none.
Represents a set of data source locators closed under descendancy.
HD_API void insert(HdDataSourceLocatorSet &&locatorSet)
Changes this set to be the union of this set and the given set.
static HD_API const HdDataSourceLocatorSet & UniversalSet()
The set containing everything.
HdDataSourceLocatorSet & operator=(HdDataSourceLocatorSet &&rhs)=default
Move assignment operator.
HdDataSourceLocatorSet()
The empty set.
HD_API const_iterator begin() const
Iterates through minimal, lexicographically sorted list of data source locators generating this set.
HD_API HdDataSourceLocatorSet ReplacePrefix(const HdDataSourceLocator &oldPrefix, const HdDataSourceLocator &newPrefix) const
Returns a lexicographically sorted locator set wherein locators in this set that have oldPrefix as a ...
HD_API void insert(const HdDataSourceLocatorSet &locatorSet)
Changes this set to be the union of this set and the given set.
HD_API bool IsEmpty() const
True if and only if this set contains no data source locator.
HD_API bool Intersects(const HdDataSourceLocator &locator) const
True if and only if locator or any of its descendants is in the set (closed under descendancy).
HdDataSourceLocatorSet(HdDataSourceLocatorSet &&rhs)=default
Move Ctor.
HdDataSourceLocatorSet & operator=(const HdDataSourceLocatorSet &rhs)=default
Copy assignment operator.
HdDataSourceLocatorSet(const HdDataSourceLocatorSet &rhs)=default
Copy Ctor.
HD_API IntersectionView Intersection(const HdDataSourceLocator &locator) const
Returns intersection with a locator as a range-like object so that it can be used in a for-loop.
HD_API void append(const HdDataSourceLocator &locator)
append() is semantically equivalent to insert(), but works much faster if locator would be added to t...
HD_API bool Contains(const HdDataSourceLocator &locator) const
True if the set (closed under descendancy) contains the given locator.
HD_API bool Intersects(const HdDataSourceLocatorSet &locatorSet) const
True if and only if the two sets (closed under descendancy) intersect.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:460
This is a small-vector class with local storage optimization, the local storage can be specified via ...
Definition: smallVector.h:157
size_type size() const
Returns the current size of the vector.
Definition: smallVector.h:596
bool empty() const
Returns true if this vector is empty.
Definition: smallVector.h:608
value_type * data()
Direct access to the underlying array.
Definition: smallVector.h:735
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].
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...