Loading...
Searching...
No Matches
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"
14#include "pxr/base/tf/hash.h"
15
16#include "pxr/imaging/hd/api.h"
17
18#include <iosfwd>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
22#define HD_DATA_SOURCE_LOCATOR_SENTINEL_TOKENS \
23 ((container, "__containerDataSource"))
24
49TF_DECLARE_PUBLIC_TOKENS(HdDataSourceLocatorSentinelTokens, HD_API,
50 HD_DATA_SOURCE_LOCATOR_SENTINEL_TOKENS);
51
59{
60public:
61
66 HD_API
68
73 HD_API
75
83 HD_API
84 explicit HdDataSourceLocator(const TfToken &t1);
85 HD_API
86 HdDataSourceLocator(const TfToken &t1, const TfToken &t2);
87 HD_API
88 HdDataSourceLocator(const TfToken &t1, const TfToken &t2,
89 const TfToken &t3);
90 HD_API
91 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
92 const TfToken &t4);
93 HD_API
94 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
95 const TfToken &t4, const TfToken &t5);
96 HD_API
97 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
98 const TfToken &t4, const TfToken &t5,
99 const TfToken &t6);
100
104 HD_API
105 HdDataSourceLocator(size_t count, const TfToken *tokens);
106
109
111 HD_API
112 size_t GetElementCount() const;
113
118 HD_API
119 const TfToken &GetElement(size_t i) const;
120
122 HD_API
123 const TfToken &GetFirstElement() const;
124
126 HD_API
127 const TfToken &GetLastElement() const;
128
133 HD_API
135
139 HD_API
141
145 HD_API
147
149 HD_API
151
153 HD_API
155
157 HD_API
159
161 HD_API
163
167 HD_API
168 bool HasPrefix(const HdDataSourceLocator &prefix) const;
169
173 HD_API
175
179 HD_API
181 const HdDataSourceLocator &oldPrefix,
182 const HdDataSourceLocator &newPrefix) const;
183
188 HD_API
189 bool Intersects(const HdDataSourceLocator &other) const;
190
191 inline bool operator==(const HdDataSourceLocator &rhs) const {
192 return _tokens == rhs._tokens;
193 }
194
195 inline bool operator!=(const HdDataSourceLocator &rhs) const {
196 return _tokens != rhs._tokens;
197 }
198
200 HD_API
201 bool operator<(const HdDataSourceLocator &rhs) const;
202
203 inline bool IsEmpty() const {
204 return _tokens.empty();
205 }
206
210 HD_API
211 std::string GetString(const char *delimiter = "/") const;
212
213 template <class HashState>
214 friend void TfHashAppend(HashState &h, HdDataSourceLocator const &myObj) {
215 h.AppendContiguous(myObj._tokens.data(), myObj._tokens.size());
216 }
217
218 inline size_t Hash() const;
219
220private:
221 using _TokenVector = TfSmallVector<TfToken, 6>;
222 _TokenVector _tokens;
223};
224
225inline size_t
226HdDataSourceLocator::Hash() const
227{
228 return TfHash()(*this);
229}
230
231HD_API std::ostream& operator<<(std::ostream& out,
232 const HdDataSourceLocator &self);
233
234//-----------------------------------------------------------------------------
235
251{
252private:
254public:
255 using const_iterator = typename _Locators::const_iterator;
256
259
261 HD_API
263
264 HD_API
266
267 // Initializer list constructor.
268 HD_API
270 const std::initializer_list<const HdDataSourceLocator> &l);
271
274
277
280
283 = default;
284
285 HD_API
286 void insert(const HdDataSourceLocator &locator);
287
289 HD_API
290 void insert(const HdDataSourceLocatorSet &locatorSet);
291
293 HD_API
294 void insert(HdDataSourceLocatorSet &&locatorSet);
295
298 HD_API
299 void append(const HdDataSourceLocator &locator);
300
301 bool operator==(const HdDataSourceLocatorSet &rhs) const {
302 return _locators == rhs._locators;
303 }
304
305 bool operator!=(const HdDataSourceLocatorSet &rhs) const {
306 return !(*this == rhs);
307 }
308
311 HD_API
312 const_iterator begin() const;
313 HD_API
314 const_iterator end() const;
315
322 HD_API
323 bool Intersects(const HdDataSourceLocator &locator) const;
324
331 HD_API
332 bool Intersects(const HdDataSourceLocatorSet &locatorSet) const;
333
335 HD_API
336 bool IsEmpty() const;
337
343 HD_API
344 bool Contains(const HdDataSourceLocator &locator) const;
345
350 HD_API
352 const HdDataSourceLocator &oldPrefix,
353 const HdDataSourceLocator &newPrefix) const;
354
355 class IntersectionIterator;
356 class IntersectionView;
357
369 HD_API
370 IntersectionView Intersection(const HdDataSourceLocator &locator) const;
371
372private:
373 // Sort and uniquify it.
374 void _Normalize();
375
376 void _InsertAndDeleteSuffixes(_Locators::iterator *position,
377 const HdDataSourceLocator &locator);
378
379 const_iterator _FirstIntersection(const HdDataSourceLocator &locator) const;
380
381 // Lexicographically sorted minimal list of locators generating
382 // the set.
383 _Locators _locators;
384};
385
386class HdDataSourceLocatorSet::IntersectionIterator
387{
388public:
389 using iterator_category = std::forward_iterator_tag;
390 using value_type = const HdDataSourceLocator;
391 using reference = value_type&;
392 using pointer = value_type*;
393 using difference_type = std::ptrdiff_t;
394
395 IntersectionIterator()
396 : _isFirst(false)
397 {
398 }
399
400 IntersectionIterator(const bool isFirst,
401 const const_iterator &iterator,
402 const const_iterator &end,
403 const HdDataSourceLocator &locator)
404 : _isFirst(isFirst)
405 , _iterator(iterator)
406 , _end(end)
407 , _locator(locator)
408 {
409 }
410
411 HD_API
412 const HdDataSourceLocator &operator*() const;
413
414 const HdDataSourceLocator* operator->() const
415 {
416 return std::addressof(**this);
417 }
418
419 HD_API
420 IntersectionIterator& operator++();
421
422 HD_API
423 IntersectionIterator operator++(int);
424
425 bool operator==(const IntersectionIterator &other) const noexcept
426 {
427 return _iterator == other._iterator;
428 }
429
430 bool operator!=(const IntersectionIterator &other) const noexcept
431 {
432 return _iterator != other._iterator;
433 }
434
435private:
436 bool _isFirst;
437 const_iterator _iterator;
438 const_iterator _end;
439 HdDataSourceLocator _locator;
440};
441
442class HdDataSourceLocatorSet::IntersectionView
443{
444public:
445 IntersectionView(const IntersectionIterator &begin,
446 const IntersectionIterator &end)
447 : _begin(begin)
448 , _end(end)
449 {
450 }
451
452 const IntersectionIterator &begin() const { return _begin; }
453
454 const IntersectionIterator &end() const { return _end; }
455
456private:
457 const IntersectionIterator _begin;
458 const IntersectionIterator _end;
459};
460
461HD_API std::ostream& operator<<(std::ostream& out,
462 const HdDataSourceLocatorSet &self);
463
464PXR_NAMESPACE_CLOSE_SCOPE
465
466#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:472
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].
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...