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 PXR_NAMESPACE_OPEN_SCOPE
35 
43 {
44 public:
45 
50  HD_API
51  static const HdDataSourceLocator &EmptyLocator();
52 
57  HD_API
59 
67  HD_API
68  explicit HdDataSourceLocator(const TfToken &t1);
69  HD_API
70  HdDataSourceLocator(const TfToken &t1, const TfToken &t2);
71  HD_API
72  HdDataSourceLocator(const TfToken &t1, const TfToken &t2,
73  const TfToken &t3);
74  HD_API
75  HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
76  const TfToken &t4);
77  HD_API
78  HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
79  const TfToken &t4, const TfToken &t5);
80  HD_API
81  HdDataSourceLocator(const TfToken &t1, const TfToken &t2, const TfToken &t3,
82  const TfToken &t4, const TfToken &t5,
83  const TfToken &t6);
84 
88  HD_API
89  HdDataSourceLocator(size_t count, const TfToken *tokens);
90 
92  HdDataSourceLocator(const HdDataSourceLocator &rhs) = default;
93 
95  HD_API
96  size_t GetElementCount() const;
97 
102  HD_API
103  const TfToken &GetElement(size_t i) const;
104 
106  HD_API
107  const TfToken &GetFirstElement() const;
108 
110  HD_API
111  const TfToken &GetLastElement() const;
112 
117  HD_API
119 
123  HD_API
125 
129  HD_API
131 
133  HD_API
134  HdDataSourceLocator Append(const TfToken &name) const;
135 
137  HD_API
138  HdDataSourceLocator Append(const HdDataSourceLocator &locator) const;
139 
141  HD_API
142  HdDataSourceLocator Prepend(const TfToken &name) const;
143 
145  HD_API
146  HdDataSourceLocator Prepend(const HdDataSourceLocator &locator) const;
147 
151  HD_API
152  bool HasPrefix(const HdDataSourceLocator &prefix) const;
153 
157  HD_API
159 
163  HD_API
165  const HdDataSourceLocator &oldPrefix,
166  const HdDataSourceLocator &newPrefix) const;
167 
172  HD_API
173  bool Intersects(const HdDataSourceLocator &other) const;
174 
175  inline bool operator==(const HdDataSourceLocator &rhs) const {
176  return _tokens == rhs._tokens;
177  }
178 
179  inline bool operator!=(const HdDataSourceLocator &rhs) const {
180  return _tokens != rhs._tokens;
181  }
182 
184  HD_API
185  bool operator<(const HdDataSourceLocator &rhs) const;
186 
187  inline bool IsEmpty() const {
188  return _tokens.empty();
189  }
190 
194  HD_API
195  std::string GetString(const char *delimiter = "/") const;
196 
197  template <class HashState>
198  friend void TfHashAppend(HashState &h, HdDataSourceLocator const &myObj) {
199  h.AppendContiguous(myObj._tokens.data(), myObj._tokens.size());
200  }
201 
202  inline size_t Hash() const;
203 
204 private:
206  _TokenVector _tokens;
207 };
208 
209 inline size_t
210 HdDataSourceLocator::Hash() const
211 {
212  return TfHash()(*this);
213 }
214 
215 //-----------------------------------------------------------------------------
216 
232 {
233 private:
235 public:
236  using const_iterator = typename _Locators::const_iterator;
237 
238  explicit HdDataSourceLocatorSet() {}
239 
240  HD_API
242 
243  // Initializer list constructor.
244  HD_API
246  const std::initializer_list<const HdDataSourceLocator> &l);
247 
249  HdDataSourceLocatorSet(const HdDataSourceLocatorSet &rhs) = default;
250 
253 
256 
259  = default;
260 
261  HD_API
262  void insert(const HdDataSourceLocator &locator);
263 
265  HD_API
266  void insert(const HdDataSourceLocatorSet &locatorSet);
267 
270  HD_API
271  void append(const HdDataSourceLocator &locator);
272 
273  bool operator==(const HdDataSourceLocatorSet &rhs) const {
274  return _locators == rhs._locators;
275  }
276 
277  bool operator!=(const HdDataSourceLocatorSet &rhs) const {
278  return !(*this == rhs);
279  }
280 
283  HD_API
284  const_iterator begin() const;
285  HD_API
286  const_iterator end() const;
287 
294  HD_API
295  bool Intersects(const HdDataSourceLocator &locator) const;
296 
303  HD_API
304  bool Intersects(const HdDataSourceLocatorSet &locatorSet) const;
305 
307  HD_API
308  bool IsEmpty() const;
309 
315  HD_API
316  bool Contains(const HdDataSourceLocator &locator) const;
317 
322  HD_API
324  const HdDataSourceLocator &oldPrefix,
325  const HdDataSourceLocator &newPrefix) const;
326 
327 private:
328  // Sort and uniquify it.
329  void _Normalize();
330 
331  void _InsertAndDeleteSuffixes(_Locators::iterator *position,
332  const HdDataSourceLocator &locator);
333  // Lexicographically sorted minimal list of locators generating
334  // the set.
335  _Locators _locators;
336 };
337 
338 PXR_NAMESPACE_CLOSE_SCOPE
339 
340 #endif // PXR_IMAGING_HD_DATASOURCELOCATOR_H
HD_API bool Contains(const HdDataSourceLocator &locator) const
True if the set (closed under descendancy) contains the given 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...
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
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 const TfToken & GetLastElement() const
Returns the last element, or empty token if none.
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 RemoveFirstElement() const
Returns a copy of this data source locator with the first element removed.
HD_API const TfToken & GetFirstElement() const
Returns the first element, or empty token if none.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:447
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 HdDataSourceLocator()
Creates an empty locator.
HD_API HdDataSourceLocator GetCommonPrefix(const HdDataSourceLocator &other) const
Returns a data source locator that represents the common prefix between this data source and other.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Represents an object that can identify the location of a data source.
HD_API bool operator<(const HdDataSourceLocator &rhs) const
Lexicographic order. If y has x as prefix, x < y.
HD_API const_iterator begin() const
Iterates through minimal, lexicographically sorted list of data source locators generating this set.
Represents a set of data source locators closed under descendancy.
HD_API const TfToken & GetElement(size_t i) const
Returns the element (token) at index i.
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 Prepend(const TfToken &name) const
Prepends name to this data source locator.
HD_API size_t GetElementCount() const
Returns the number of elements (tokens) in this data source.
static HD_API const HdDataSourceLocator & EmptyLocator()
Returns a common empty locator.
size_type size() const
Returns the current size of the vector.
Definition: smallVector.h:629
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 Append(const TfToken &name) const
Appends name to this 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).
HD_API bool IsEmpty() const
True if and only if this set contains no data source locator.
HdDataSourceLocatorSet & operator=(HdDataSourceLocatorSet &&rhs)=default
Move assignment operator.
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.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...