Loading...
Searching...
No Matches
dataSourceLocator.h
1//
2// Copyright 2021 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_IMAGING_HD_DATASOURCELOCATOR_H
25#define PXR_IMAGING_HD_DATASOURCELOCATOR_H
26
27#include "pxr/pxr.h"
28#include "pxr/base/tf/token.h"
30#include "pxr/base/tf/hash.h"
31
32#include "pxr/imaging/hd/api.h"
33
34#include <iosfwd>
35
36PXR_NAMESPACE_OPEN_SCOPE
37
45{
46public:
47
52 HD_API
54
59 HD_API
61
69 HD_API
70 explicit HdDataSourceLocator(const TfToken &t1);
71 HD_API
72 HdDataSourceLocator(const TfToken &t1, const TfToken &t2);
73 HD_API
74 HdDataSourceLocator(const TfToken &t1, const TfToken &t2,
75 const TfToken &t3);
76 HD_API
77 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
78 const TfToken &t4);
79 HD_API
80 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
81 const TfToken &t4, const TfToken &t5);
82 HD_API
83 HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
84 const TfToken &t4, const TfToken &t5,
85 const TfToken &t6);
86
90 HD_API
91 HdDataSourceLocator(size_t count, const TfToken *tokens);
92
95
97 HD_API
98 size_t GetElementCount() const;
99
104 HD_API
105 const TfToken &GetElement(size_t i) const;
106
108 HD_API
109 const TfToken &GetFirstElement() const;
110
112 HD_API
113 const TfToken &GetLastElement() const;
114
119 HD_API
121
125 HD_API
127
131 HD_API
133
135 HD_API
137
139 HD_API
141
143 HD_API
145
147 HD_API
149
153 HD_API
154 bool HasPrefix(const HdDataSourceLocator &prefix) const;
155
159 HD_API
161
165 HD_API
167 const HdDataSourceLocator &oldPrefix,
168 const HdDataSourceLocator &newPrefix) const;
169
174 HD_API
175 bool Intersects(const HdDataSourceLocator &other) const;
176
177 inline bool operator==(const HdDataSourceLocator &rhs) const {
178 return _tokens == rhs._tokens;
179 }
180
181 inline bool operator!=(const HdDataSourceLocator &rhs) const {
182 return _tokens != rhs._tokens;
183 }
184
186 HD_API
187 bool operator<(const HdDataSourceLocator &rhs) const;
188
189 inline bool IsEmpty() const {
190 return _tokens.empty();
191 }
192
196 HD_API
197 std::string GetString(const char *delimiter = "/") const;
198
199 template <class HashState>
200 friend void TfHashAppend(HashState &h, HdDataSourceLocator const &myObj) {
201 h.AppendContiguous(myObj._tokens.data(), myObj._tokens.size());
202 }
203
204 inline size_t Hash() const;
205
206private:
207 using _TokenVector = TfSmallVector<TfToken, 6>;
208 _TokenVector _tokens;
209};
210
211inline size_t
212HdDataSourceLocator::Hash() const
213{
214 return TfHash()(*this);
215}
216
217HD_API std::ostream& operator<<(std::ostream& out,
218 const HdDataSourceLocator &self);
219
220//-----------------------------------------------------------------------------
221
237{
238private:
240public:
241 using const_iterator = typename _Locators::const_iterator;
242
245
247 HD_API
249
250 HD_API
252
253 // Initializer list constructor.
254 HD_API
256 const std::initializer_list<const HdDataSourceLocator> &l);
257
260
263
266
269 = default;
270
271 HD_API
272 void insert(const HdDataSourceLocator &locator);
273
275 HD_API
276 void insert(const HdDataSourceLocatorSet &locatorSet);
277
279 HD_API
280 void insert(HdDataSourceLocatorSet &&locatorSet);
281
284 HD_API
285 void append(const HdDataSourceLocator &locator);
286
287 bool operator==(const HdDataSourceLocatorSet &rhs) const {
288 return _locators == rhs._locators;
289 }
290
291 bool operator!=(const HdDataSourceLocatorSet &rhs) const {
292 return !(*this == rhs);
293 }
294
297 HD_API
298 const_iterator begin() const;
299 HD_API
300 const_iterator end() const;
301
308 HD_API
309 bool Intersects(const HdDataSourceLocator &locator) const;
310
317 HD_API
318 bool Intersects(const HdDataSourceLocatorSet &locatorSet) const;
319
321 HD_API
322 bool IsEmpty() const;
323
329 HD_API
330 bool Contains(const HdDataSourceLocator &locator) const;
331
336 HD_API
338 const HdDataSourceLocator &oldPrefix,
339 const HdDataSourceLocator &newPrefix) const;
340
341 class IntersectionIterator;
342 class IntersectionView;
343
355 HD_API
356 IntersectionView Intersection(const HdDataSourceLocator &locator) const;
357
358private:
359 // Sort and uniquify it.
360 void _Normalize();
361
362 void _InsertAndDeleteSuffixes(_Locators::iterator *position,
363 const HdDataSourceLocator &locator);
364
365 const_iterator _FirstIntersection(const HdDataSourceLocator &locator) const;
366
367 // Lexicographically sorted minimal list of locators generating
368 // the set.
369 _Locators _locators;
370};
371
372class HdDataSourceLocatorSet::IntersectionIterator
373{
374public:
375 using iterator_category = std::forward_iterator_tag;
376 using value_type = const HdDataSourceLocator;
377 using reference = value_type&;
378 using pointer = value_type*;
379 using difference_type = std::ptrdiff_t;
380
381 IntersectionIterator()
382 : _isFirst(false)
383 {
384 }
385
386 IntersectionIterator(const bool isFirst,
387 const const_iterator &iterator,
388 const const_iterator &end,
389 const HdDataSourceLocator &locator)
390 : _isFirst(isFirst)
391 , _iterator(iterator)
392 , _end(end)
393 , _locator(locator)
394 {
395 }
396
397 HD_API
398 const HdDataSourceLocator &operator*() const;
399
400 const HdDataSourceLocator* operator->() const
401 {
402 return std::addressof(**this);
403 }
404
405 HD_API
406 IntersectionIterator& operator++();
407
408 HD_API
409 IntersectionIterator operator++(int);
410
411 bool operator==(const IntersectionIterator &other) const noexcept
412 {
413 return _iterator == other._iterator;
414 }
415
416 bool operator!=(const IntersectionIterator &other) const noexcept
417 {
418 return _iterator != other._iterator;
419 }
420
421private:
422 bool _isFirst;
423 const_iterator _iterator;
424 const_iterator _end;
425 HdDataSourceLocator _locator;
426};
427
428class HdDataSourceLocatorSet::IntersectionView
429{
430public:
431 IntersectionView(const IntersectionIterator &begin,
432 const IntersectionIterator &end)
433 : _begin(begin)
434 , _end(end)
435 {
436 }
437
438 const IntersectionIterator &begin() const { return _begin; }
439
440 const IntersectionIterator &end() const { return _end; }
441
442private:
443 const IntersectionIterator _begin;
444 const IntersectionIterator _end;
445};
446
447HD_API std::ostream& operator<<(std::ostream& out,
448 const HdDataSourceLocatorSet &self);
449
450PXR_NAMESPACE_CLOSE_SCOPE
451
452#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:477
This is a small-vector class with local storage optimization, the local storage can be specified via ...
Definition: smallVector.h:179
size_type size() const
Returns the current size of the vector.
Definition: smallVector.h:629
bool empty() const
Returns true if this vector is empty.
Definition: smallVector.h:641
value_type * data()
Direct access to the underlying array.
Definition: smallVector.h:768
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].
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...