All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
path.h
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 SDF_PATH_H
25 #define SDF_PATH_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/sdf/api.h"
29 #include "pxr/usd/sdf/pool.h"
30 #include "pxr/usd/sdf/tokens.h"
31 #include "pxr/base/tf/stl.h"
32 #include "pxr/base/tf/token.h"
33 #include "pxr/base/vt/traits.h"
34 
35 #include <boost/intrusive_ptr.hpp>
36 #include <boost/operators.hpp>
37 
38 #include <algorithm>
39 #include <iterator>
40 #include <set>
41 #include <string>
42 #include <type_traits>
43 #include <utility>
44 #include <vector>
45 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
48 class Sdf_PathNode;
49 
50 // Ref-counting pointer to a path node.
51 // Intrusive ref-counts are used to keep the size of SdfPath
52 // the same as a raw pointer. (shared_ptr, by comparison,
53 // is the size of two pointers.)
54 
55 typedef boost::intrusive_ptr<const Sdf_PathNode> Sdf_PathNodeConstRefPtr;
56 
57 void intrusive_ptr_add_ref(Sdf_PathNode const *);
58 void intrusive_ptr_release(Sdf_PathNode const *);
59 
60 // Tags used for the pools of path nodes.
61 struct Sdf_PathPrimTag;
62 struct Sdf_PathPropTag;
63 
64 // These are validated below.
65 static constexpr size_t Sdf_SizeofPrimPathNode = sizeof(void *) * 3;
66 static constexpr size_t Sdf_SizeofPropPathNode = sizeof(void *) * 3;
67 
68 using Sdf_PathPrimPartPool = Sdf_Pool<
69  Sdf_PathPrimTag, Sdf_SizeofPrimPathNode, /*regionBits=*/8>;
70 
71 using Sdf_PathPropPartPool = Sdf_Pool<
72  Sdf_PathPropTag, Sdf_SizeofPropPathNode, /*regionBits=*/8>;
73 
74 using Sdf_PathPrimHandle = Sdf_PathPrimPartPool::Handle;
75 using Sdf_PathPropHandle = Sdf_PathPropPartPool::Handle;
76 
77 // This handle class wraps up the raw Prim/PropPartPool handles.
78 template <class Handle, bool Counted, class PathNode=Sdf_PathNode const>
79 struct Sdf_PathNodeHandleImpl {
80 private:
81  typedef Sdf_PathNodeHandleImpl this_type;
82 
83 public:
84  constexpr Sdf_PathNodeHandleImpl() noexcept {};
85 
86  explicit
87  Sdf_PathNodeHandleImpl(Sdf_PathNode const *p, bool add_ref = true)
88  : _poolHandle(Handle::GetHandle(reinterpret_cast<char const *>(p))) {
89  if (p && add_ref) {
90  _AddRef(p);
91  }
92  }
93 
94  explicit
95  Sdf_PathNodeHandleImpl(Handle h, bool add_ref = true)
96  : _poolHandle(h) {
97  if (h && add_ref) {
98  _AddRef();
99  }
100  }
101 
102  Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl const &rhs)
103  : _poolHandle(rhs._poolHandle) {
104  if (_poolHandle) {
105  _AddRef();
106  }
107  }
108 
109  ~Sdf_PathNodeHandleImpl() {
110  if (_poolHandle) {
111  _DecRef();
112  }
113  }
114 
115  Sdf_PathNodeHandleImpl &
116  operator=(Sdf_PathNodeHandleImpl const &rhs) {
117  if (Counted && *this == rhs) {
118  return *this;
119  }
120  this_type(rhs).swap(*this);
121  return *this;
122  }
123 
124  Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl &&rhs) noexcept
125  : _poolHandle(rhs._poolHandle) {
126  rhs._poolHandle = nullptr;
127  }
128 
129  Sdf_PathNodeHandleImpl &
130  operator=(Sdf_PathNodeHandleImpl &&rhs) noexcept {
131  this_type(std::move(rhs)).swap(*this);
132  return *this;
133  }
134 
135  Sdf_PathNodeHandleImpl &
136  operator=(Sdf_PathNode const *rhs) noexcept {
137  this_type(rhs).swap(*this);
138  return *this;
139  }
140 
141  void reset() noexcept {
142  _poolHandle = Handle { nullptr };
143  }
144 
145  Sdf_PathNode const *
146  get() const noexcept {
147  return reinterpret_cast<Sdf_PathNode *>(_poolHandle.GetPtr());
148  }
149 
150  Sdf_PathNode const &
151  operator*() const {
152  return *get();
153  }
154 
155  Sdf_PathNode const *
156  operator->() const {
157  return get();
158  }
159 
160  explicit operator bool() const noexcept {
161  return static_cast<bool>(_poolHandle);
162  }
163 
164  void swap(Sdf_PathNodeHandleImpl &rhs) noexcept {
165  _poolHandle.swap(rhs._poolHandle);
166  }
167 
168  inline bool operator==(Sdf_PathNodeHandleImpl const &rhs) const noexcept {
169  return _poolHandle == rhs._poolHandle;
170  }
171  inline bool operator!=(Sdf_PathNodeHandleImpl const &rhs) const noexcept {
172  return _poolHandle != rhs._poolHandle;
173  }
174  inline bool operator<(Sdf_PathNodeHandleImpl const &rhs) const noexcept {
175  return _poolHandle < rhs._poolHandle;
176  }
177 private:
178 
179  void _AddRef(Sdf_PathNode const *p) const {
180  if (Counted) {
181  intrusive_ptr_add_ref(p);
182  }
183  }
184 
185  void _AddRef() const {
186  _AddRef(get());
187  }
188 
189  void _DecRef() const {
190  if (Counted) {
191  intrusive_ptr_release(get());
192  }
193  }
194 
195  Handle _poolHandle { nullptr };
196 };
197 
198 using Sdf_PathPrimNodeHandle =
199  Sdf_PathNodeHandleImpl<Sdf_PathPrimHandle, /*Counted=*/true>;
200 
201 using Sdf_PathPropNodeHandle =
202  Sdf_PathNodeHandleImpl<Sdf_PathPropHandle, /*Counted=*/false>;
203 
204 
206 typedef std::set<class SdfPath> SdfPathSet;
208 typedef std::vector<class SdfPath> SdfPathVector;
209 
210 // Tell VtValue that SdfPath is cheap to copy.
211 VT_TYPE_IS_CHEAP_TO_COPY(class SdfPath);
212 
287 class SdfPath : boost::totally_ordered<SdfPath>
288 {
289 public:
291  SDF_API static const SdfPath & EmptyPath();
292 
295  SDF_API static const SdfPath & AbsoluteRootPath();
296 
298  SDF_API static const SdfPath & ReflexiveRelativePath();
299 
302 
305  constexpr SdfPath() = default;
306 
319  //
320  // XXX We may want to revisit the behavior when constructing
321  // a path with an empty string ("") to accept it without error and
322  // return EmptyPath.
323  SDF_API explicit SdfPath(const std::string &path);
324 
326 
329 
331  SDF_API size_t GetPathElementCount() const;
332 
334  SDF_API bool IsAbsolutePath() const;
335 
337  SDF_API bool IsPrimPath() const;
338 
340  SDF_API bool IsAbsoluteRootOrPrimPath() const;
341 
346  SDF_API bool IsRootPrimPath() const;
347 
353  SDF_API bool IsPropertyPath() const;
354 
358  SDF_API bool IsPrimPropertyPath() const;
359 
363  SDF_API bool IsNamespacedPropertyPath() const;
364 
367  SDF_API bool IsPrimVariantSelectionPath() const;
368 
371  SDF_API bool IsPrimOrPrimVariantSelectionPath() const;
372 
375  SDF_API bool ContainsPrimVariantSelection() const;
376 
383  return static_cast<bool>(_propPart);
384  }
385 
388  SDF_API bool ContainsTargetPath() const;
389 
393  SDF_API bool IsRelationalAttributePath() const;
394 
397  SDF_API bool IsTargetPath() const;
398 
400  SDF_API bool IsMapperPath() const;
401 
403  SDF_API bool IsMapperArgPath() const;
404 
406  SDF_API bool IsExpressionPath() const;
407 
409  inline bool IsEmpty() const noexcept {
410  return *this == SdfPath();
411  }
412 
414  SDF_API TfToken const &GetToken() const;
415 
417  SDF_API const std::string &GetString() const;
418 
420  SDF_API const char *GetText() const;
421 
426  SDF_API SdfPathVector GetPrefixes() const;
427 
434  SDF_API void GetPrefixes(SdfPathVector *prefixes) const;
435 
446  SDF_API const std::string &GetName() const;
447 
450  SDF_API const TfToken &GetNameToken() const;
451 
469  SDF_API std::string GetElementString() const;
470 
472  SDF_API TfToken GetElementToken() const;
473 
493  SDF_API SdfPath ReplaceName(TfToken const &newName) const;
494 
507  SDF_API const SdfPath &GetTargetPath() const;
508 
516  SDF_API void GetAllTargetPathsRecursively(SdfPathVector *result) const;
517 
522  SDF_API
523  std::pair<std::string, std::string> GetVariantSelection() const;
524 
527  SDF_API bool HasPrefix( const SdfPath &prefix ) const;
528 
530 
533 
552  SDF_API SdfPath GetParentPath() const;
553 
561  SDF_API SdfPath GetPrimPath() const;
562 
571  SDF_API SdfPath GetPrimOrPrimVariantSelectionPath() const;
572 
578  SDF_API SdfPath GetAbsoluteRootOrPrimPath() const;
579 
583  SDF_API SdfPath StripAllVariantSelections() const;
584 
592  SDF_API SdfPath AppendPath(const SdfPath &newSuffix) const;
593 
599  SDF_API SdfPath AppendChild(TfToken const &childName) const;
600 
605  SDF_API SdfPath AppendProperty(TfToken const &propName) const;
606 
611  SDF_API
612  SdfPath AppendVariantSelection(const std::string &variantSet,
613  const std::string &variant) const;
614 
619  SDF_API SdfPath AppendTarget(const SdfPath &targetPath) const;
620 
625  SDF_API
626  SdfPath AppendRelationalAttribute(TfToken const &attrName) const;
627 
631  SDF_API
632  SdfPath ReplaceTargetPath( const SdfPath &newTargetPath ) const;
633 
638  SDF_API SdfPath AppendMapper(const SdfPath &targetPath) const;
639 
644  SDF_API SdfPath AppendMapperArg(TfToken const &argName) const;
645 
649  SDF_API SdfPath AppendExpression() const;
650 
660  SDF_API SdfPath AppendElementString(const std::string &element) const;
661 
663  SDF_API SdfPath AppendElementToken(const TfToken &elementTok) const;
664 
674  SDF_API
675  SdfPath ReplacePrefix(const SdfPath &oldPrefix,
676  const SdfPath &newPrefix,
677  bool fixTargetPaths=true) const;
678 
681  SDF_API SdfPath GetCommonPrefix(const SdfPath &path) const;
682 
698  SDF_API
699  std::pair<SdfPath, SdfPath>
700  RemoveCommonSuffix(const SdfPath& otherPath,
701  bool stopAtRootPrim = false) const;
702 
712  SDF_API SdfPath MakeAbsolutePath(const SdfPath & anchor) const;
713 
726  SDF_API SdfPath MakeRelativePath(const SdfPath & anchor) const;
727 
729 
732 
735  SDF_API static bool IsValidIdentifier(const std::string &name);
736 
739  SDF_API static bool IsValidNamespacedIdentifier(const std::string &name);
740 
744  SDF_API static std::vector<std::string> TokenizeIdentifier(const std::string &name);
745 
749  SDF_API
750  static TfTokenVector TokenizeIdentifierAsTokens(const std::string &name);
751 
754  SDF_API
755  static std::string JoinIdentifier(const std::vector<std::string> &names);
756 
759  SDF_API
760  static std::string JoinIdentifier(const TfTokenVector& names);
761 
766  SDF_API
767  static std::string JoinIdentifier(const std::string &lhs,
768  const std::string &rhs);
769 
774  SDF_API
775  static std::string JoinIdentifier(const TfToken &lhs, const TfToken &rhs);
776 
780  SDF_API
781  static std::string StripNamespace(const std::string &name);
782 
786  SDF_API
787  static TfToken StripNamespace(const TfToken &name);
788 
797  SDF_API
798  static std::pair<std::string, bool>
799  StripPrefixNamespace(const std::string &name,
800  const std::string &matchNamespace);
801 
806  SDF_API
807  static bool IsValidPathString(const std::string &pathString,
808  std::string *errMsg = 0);
809 
811 
814 
817  inline bool operator==(const SdfPath &rhs) const {
818  return _AsInt() == rhs._AsInt();
819  }
820 
825  inline bool operator<(const SdfPath &rhs) const {
826  if (_AsInt() == rhs._AsInt()) {
827  return false;
828  }
829  if (!_primPart || !rhs._primPart) {
830  return !_primPart && rhs._primPart;
831  }
832  // Valid prim parts -- must walk node structure, etc.
833  return _LessThanInternal(*this, rhs);
834  }
835 
836  // For hash maps and sets
837  struct Hash {
838  inline size_t operator()(const SdfPath& path) const {
839  // The hash function is pretty sensitive performance-wise. Be
840  // careful making changes here, and run tests.
841  uint32_t primPart, propPart;
842  memcpy(&primPart, &path._primPart, sizeof(primPart));
843  memcpy(&propPart, &path._propPart, sizeof(propPart));
844 
845  // Important considerations here:
846  // - It must be fast to execute.
847  // - It must do well in hash tables that find indexes by taking
848  // the remainder divided by a prime number of buckets.
849  // - It must do well in hash tables that find indexes by taking
850  // just the low-order bits.
851 
852  // This hash function maps the (primPart, propPart) pair to a single
853  // value by using triangular numbers. So the first few path hash
854  // values would look like this, for primPart as X increasing
855  // left-to-right and for propPart as Y increasing top-to-bottom.
856  //
857  // 0 2 5 9 14 20
858  // 1 4 8 13 19 26
859  // 3 7 12 18 25 33
860  // 6 11 17 24 32 41
861  // 10 16 23 31 40 50
862  // 15 22 30 39 49 60
863 
864  uint64_t x = primPart >> 8;
865  uint64_t y = x + (propPart >> 8);
866  return x + (y * (y + 1)) / 2;
867  }
868  };
869 
870  inline size_t GetHash() const {
871  return Hash()(*this);
872  }
873 
874  // For cases where an unspecified total order that is not stable from
875  // run-to-run is needed.
876  struct FastLessThan {
877  inline bool operator()(const SdfPath& a, const SdfPath& b) const {
878  return a._AsInt() < b._AsInt();
879  }
880  };
881 
883 
886 
893  SDF_API static SdfPathVector
894  GetConciseRelativePaths(const SdfPathVector& paths);
895 
899  SDF_API static void RemoveDescendentPaths(SdfPathVector *paths);
900 
903  SDF_API static void RemoveAncestorPaths(SdfPathVector *paths);
904 
906 
907 private:
908 
909  // This is used for all internal path construction where we do operations
910  // via nodes and then want to return a new path with a resulting prim and
911  // property parts.
912 
913  // Accept rvalues.
914  explicit SdfPath(Sdf_PathPrimNodeHandle &&primNode)
915  : _primPart(std::move(primNode)) {}
916 
917  // Construct from prim & prop parts.
918  SdfPath(Sdf_PathPrimNodeHandle const &primPart,
919  Sdf_PathPropNodeHandle const &propPart)
920  : _primPart(primPart)
921  , _propPart(propPart) {}
922 
923  // Construct from prim & prop node pointers.
924  SdfPath(Sdf_PathNode const *primPart,
925  Sdf_PathNode const *propPart)
926  : _primPart(primPart)
927  , _propPart(propPart) {}
928 
929  friend class Sdf_PathNode;
930  friend class Sdfext_PathAccess;
931 
932  // converts elements to a string for parsing (unfortunate)
933  static std::string
934  _ElementsToString(bool absolute, const std::vector<std::string> &elements);
935 
936  SdfPath _ReplacePrimPrefix(SdfPath const &oldPrefix,
937  SdfPath const &newPrefix) const;
938 
939  SdfPath _ReplaceTargetPathPrefixes(SdfPath const &oldPrefix,
940  SdfPath const &newPrefix) const;
941 
942  SdfPath _ReplacePropPrefix(SdfPath const &oldPrefix,
943  SdfPath const &newPrefix,
944  bool fixTargetPaths) const;
945 
946  // Helper to implement the uninlined portion of operator<.
947  SDF_API static bool
948  _LessThanInternal(SdfPath const &lhs, SdfPath const &rhs);
949 
950  inline uint64_t _AsInt() const {
951  static_assert(sizeof(*this) == sizeof(uint64_t), "");
952  uint64_t ret;
953  std::memcpy(&ret, this, sizeof(*this));
954  return ret;
955  }
956 
957  friend void swap(SdfPath &lhs, SdfPath &rhs) {
958  lhs._primPart.swap(rhs._primPart);
959  lhs._propPart.swap(rhs._propPart);
960  }
961 
962  Sdf_PathPrimNodeHandle _primPart;
963  Sdf_PathPropNodeHandle _propPart;
964 
965 };
966 
967 // Overload hash_value for SdfPath. Used by things like boost::hash.
968 inline size_t hash_value(SdfPath const &path)
969 {
970  return path.GetHash();
971 }
972 
974 SDF_API std::ostream & operator<<( std::ostream &out, const SdfPath &path );
975 
976 // Helper for SdfPathFindPrefixedRange & SdfPathFindLongestPrefix. A function
977 // object that returns an SdfPath const & unchanged.
978 struct Sdf_PathIdentity {
979  inline SdfPath const &operator()(SdfPath const &arg) const {
980  return arg;
981  }
982 };
983 
990 template <class ForwardIterator, class GetPathFn = Sdf_PathIdentity>
991 std::pair<ForwardIterator, ForwardIterator>
992 SdfPathFindPrefixedRange(ForwardIterator begin, ForwardIterator end,
993  SdfPath const &prefix,
994  GetPathFn const &getPath = GetPathFn()) {
995  using IterRef =
996  typename std::iterator_traits<ForwardIterator>::reference;
997 
998  struct Compare {
999  Compare(GetPathFn const &getPath) : _getPath(getPath) {}
1000  GetPathFn const &_getPath;
1001  bool operator()(IterRef a, SdfPath const &b) const {
1002  return _getPath(a) < b;
1003  }
1004  };
1005 
1006  std::pair<ForwardIterator, ForwardIterator> result;
1007 
1008  // First, use lower_bound to find where \a prefix would go.
1009  result.first = std::lower_bound(begin, end, prefix, Compare(getPath));
1010 
1011  // Next, find end of range starting from the lower bound, using the
1012  // prefixing condition to define the boundary.
1013  result.second = TfFindBoundary(result.first, end,
1014  [&prefix, &getPath](IterRef iterRef) {
1015  return getPath(iterRef).HasPrefix(prefix);
1016  });
1017 
1018  return result;
1019 }
1020 
1021 template <class RandomAccessIterator, class GetPathFn>
1022 RandomAccessIterator
1023 Sdf_PathFindLongestPrefixImpl(RandomAccessIterator begin,
1024  RandomAccessIterator end,
1025  SdfPath const &path,
1026  bool strictPrefix,
1027  GetPathFn const &getPath)
1028 {
1029  using IterRef =
1030  typename std::iterator_traits<RandomAccessIterator>::reference;
1031 
1032  struct Compare {
1033  Compare(GetPathFn const &getPath) : _getPath(getPath) {}
1034  GetPathFn const &_getPath;
1035  bool operator()(IterRef a, SdfPath const &b) const {
1036  return _getPath(a) < b;
1037  }
1038  };
1039 
1040  // Search for the path in [begin, end). If present, return it. If not,
1041  // examine prior element in [begin, end). If none, return end. Else, is it
1042  // a prefix of path? If so, return it. Else find common prefix of that
1043  // element and path and recurse.
1044 
1045  // If empty sequence, return.
1046  if (begin == end)
1047  return end;
1048 
1049  // Search for where this path would lexicographically appear in the range.
1050  RandomAccessIterator result =
1051  std::lower_bound(begin, end, path, Compare(getPath));
1052 
1053  // If we didn't get the end, check to see if we got the path exactly if
1054  // we're not looking for a strict prefix.
1055  if (!strictPrefix && result != end && getPath(*result) == path)
1056  return result;
1057 
1058  // If we got begin (and didn't match in the case of a non-strict prefix)
1059  // then there's no prefix.
1060  if (result == begin)
1061  return end;
1062 
1063  // If the prior element is a prefix, we're done.
1064  if (path.HasPrefix(getPath(*--result)))
1065  return result;
1066 
1067  // Otherwise, find the common prefix of the lexicographical predecessor and
1068  // recurse looking for it or its longest prefix in the preceding range. We
1069  // always pass strictPrefix=false, since now we're operating on prefixes of
1070  // the original caller's path.
1071  RandomAccessIterator final =
1072  Sdf_PathFindLongestPrefixImpl(
1073  begin, result, path.GetCommonPrefix(getPath(*result)),
1074  /*strictPrefix=*/ false, getPath);
1075 
1076  // If the recursion failed, promote the recursive call's end to our end.
1077  return final == result ? end : final;
1078 }
1079 
1087 template <class RandomAccessIterator, class GetPathFn = Sdf_PathIdentity,
1088  class = typename std::enable_if<
1089  std::is_base_of<
1090  std::random_access_iterator_tag,
1091  typename std::iterator_traits<
1092  RandomAccessIterator>::iterator_category
1093  >::value
1094  >::type
1095  >
1096 RandomAccessIterator
1097 SdfPathFindLongestPrefix(RandomAccessIterator begin,
1098  RandomAccessIterator end,
1099  SdfPath const &path,
1100  GetPathFn const &getPath = GetPathFn())
1101 {
1102  return Sdf_PathFindLongestPrefixImpl(
1103  begin, end, path, /*strictPrefix=*/false, getPath);
1104 }
1105 
1113 template <class RandomAccessIterator, class GetPathFn = Sdf_PathIdentity,
1114  class = typename std::enable_if<
1115  std::is_base_of<
1116  std::random_access_iterator_tag,
1117  typename std::iterator_traits<
1118  RandomAccessIterator>::iterator_category
1119  >::value
1120  >::type
1121  >
1122 RandomAccessIterator
1123 SdfPathFindLongestStrictPrefix(RandomAccessIterator begin,
1124  RandomAccessIterator end,
1125  SdfPath const &path,
1126  GetPathFn const &getPath = GetPathFn())
1127 {
1128  return Sdf_PathFindLongestPrefixImpl(
1129  begin, end, path, /*strictPrefix=*/true, getPath);
1130 }
1131 
1132 template <class Iter, class MapParam, class GetPathFn = Sdf_PathIdentity>
1133 Iter
1134 Sdf_PathFindLongestPrefixImpl(
1135  MapParam map, SdfPath const &path, bool strictPrefix,
1136  GetPathFn const &getPath = GetPathFn())
1137 {
1138  // Search for the path in map. If present, return it. If not, examine
1139  // prior element in map. If none, return end. Else, is it a prefix of
1140  // path? If so, return it. Else find common prefix of that element and
1141  // path and recurse.
1142 
1143  const Iter mapEnd = map.end();
1144 
1145  // If empty, return.
1146  if (map.empty())
1147  return mapEnd;
1148 
1149  // Search for where this path would lexicographically appear in the range.
1150  Iter result = map.lower_bound(path);
1151 
1152  // If we didn't get the end, check to see if we got the path exactly if
1153  // we're not looking for a strict prefix.
1154  if (!strictPrefix && result != mapEnd && getPath(*result) == path)
1155  return result;
1156 
1157  // If we got begin (and didn't match in the case of a non-strict prefix)
1158  // then there's no prefix.
1159  if (result == map.begin())
1160  return mapEnd;
1161 
1162  // If the prior element is a prefix, we're done.
1163  if (path.HasPrefix(getPath(*--result)))
1164  return result;
1165 
1166  // Otherwise, find the common prefix of the lexicographical predecessor and
1167  // recurse looking for it or its longest prefix in the preceding range. We
1168  // always pass strictPrefix=false, since now we're operating on prefixes of
1169  // the original caller's path.
1170  return Sdf_PathFindLongestPrefixImpl<Iter, MapParam>(
1171  map, path.GetCommonPrefix(getPath(*result)), /*strictPrefix=*/false,
1172  getPath);
1173 }
1174 
1178 SDF_API
1179 typename std::set<SdfPath>::const_iterator
1180 SdfPathFindLongestPrefix(std::set<SdfPath> const &set, SdfPath const &path);
1181 
1185 template <class T>
1186 typename std::map<SdfPath, T>::const_iterator
1187 SdfPathFindLongestPrefix(std::map<SdfPath, T> const &map, SdfPath const &path)
1188 {
1189  return Sdf_PathFindLongestPrefixImpl<
1190  typename std::map<SdfPath, T>::const_iterator,
1191  std::map<SdfPath, T> const &>(map, path, /*strictPrefix=*/false,
1192  TfGet<0>());
1193 }
1194 template <class T>
1195 typename std::map<SdfPath, T>::iterator
1196 SdfPathFindLongestPrefix(std::map<SdfPath, T> &map, SdfPath const &path)
1197 {
1198  return Sdf_PathFindLongestPrefixImpl<
1199  typename std::map<SdfPath, T>::iterator,
1200  std::map<SdfPath, T> &>(map, path, /*strictPrefix=*/false,
1201  TfGet<0>());
1202 }
1203 
1207 SDF_API
1208 typename std::set<SdfPath>::const_iterator
1209 SdfPathFindLongestStrictPrefix(std::set<SdfPath> const &set,
1210  SdfPath const &path);
1211 
1215 template <class T>
1216 typename std::map<SdfPath, T>::const_iterator
1217 SdfPathFindLongestStrictPrefix(
1218  std::map<SdfPath, T> const &map, SdfPath const &path)
1219 {
1220  return Sdf_PathFindLongestPrefixImpl<
1221  typename std::map<SdfPath, T>::const_iterator,
1222  std::map<SdfPath, T> const &>(map, path, /*strictPrefix=*/true,
1223  TfGet<0>());
1224 }
1225 template <class T>
1226 typename std::map<SdfPath, T>::iterator
1227 SdfPathFindLongestStrictPrefix(
1228  std::map<SdfPath, T> &map, SdfPath const &path)
1229 {
1230  return Sdf_PathFindLongestPrefixImpl<
1231  typename std::map<SdfPath, T>::iterator,
1232  std::map<SdfPath, T> &>(map, path, /*strictPrefix=*/true,
1233  TfGet<0>());
1234 }
1235 
1236 PXR_NAMESPACE_CLOSE_SCOPE
1237 
1238 // Sdf_PathNode is not public API, but we need to include it here
1239 // so we can inline the ref-counting operations, which must manipulate
1240 // its internal _refCount member.
1241 #include "pxr/usd/sdf/pathNode.h"
1242 
1243 PXR_NAMESPACE_OPEN_SCOPE
1244 
1245 static_assert(Sdf_SizeofPrimPathNode == sizeof(Sdf_PrimPathNode), "");
1246 static_assert(Sdf_SizeofPropPathNode == sizeof(Sdf_PrimPropertyPathNode), "");
1247 
1248 PXR_NAMESPACE_CLOSE_SCOPE
1249 
1250 #endif // SDF_PATH_H
SDF_API TfToken const & GetToken() const
Returns the string representation of this path as a TfToken.
SDF_API SdfPathVector GetPrefixes() const
Returns the prefix paths of this path.
SDF_API bool IsMapperPath() const
Returns whether the path identifies a connection mapper.
static SDF_API std::string StripNamespace(const std::string &name)
Returns name stripped of any namespaces.
bool operator<(const SdfPath &rhs) const
Comparison operator.
Definition: path.h:825
SDF_API bool ContainsPrimVariantSelection() const
Returns whether the path or any of its parent paths identifies a variant selection for a prim...
SDF_API bool IsExpressionPath() const
Returns whether the path identifies a connection expression.
SDF_API bool IsNamespacedPropertyPath() const
Returns whether the path identifies a namespaced property.
SDF_API SdfPath AppendProperty(TfToken const &propName) const
Creates a path by appending an element for propName to this path.
SDF_API bool IsTargetPath() const
Returns whether the path identifies a relationship or connection target.
SDF_API bool ContainsTargetPath() const
Return true if this path is or has a prefix that&#39;s a target path or a mapper path.
SDF_API SdfPath AppendMapperArg(TfToken const &argName) const
Creates a path by appending an element for argName.
bool ContainsPropertyElements() const
Return true if this path contains any property elements, false otherwise.
Definition: path.h:382
SDF_API SdfPath ReplaceTargetPath(const SdfPath &newTargetPath) const
Replaces the relational attribute&#39;s target path.
SDF_API bool IsPrimOrPrimVariantSelectionPath() const
Return true if this path is a prim path or is a prim variant selection path.
SDF_API SdfPath GetParentPath() const
Return the path that identifies this path&#39;s namespace parent.
SDF_API bool IsPrimPath() const
Returns whether the path identifies a prim.
bool operator==(const SdfPath &rhs) const
Equality operator.
Definition: path.h:817
static SDF_API bool IsValidNamespacedIdentifier(const std::string &name)
Returns whether name is a legal namespaced identifier.
SDF_API SdfPath AppendTarget(const SdfPath &targetPath) const
Creates a path by appending an element for targetPath.
SDF_API SdfPath GetAbsoluteRootOrPrimPath() const
Creates a path by stripping all properties and relational attributes from this path, leaving the path to the containing prim.
SDF_API bool IsAbsolutePath() const
Returns whether the path is absolute.
constexpr SdfPath()=default
Constructs the default, empty path.
static SDF_API TfTokenVector TokenizeIdentifierAsTokens(const std::string &name)
Tokenizes name by the namespace delimiter.
SDF_API const TfToken & GetNameToken() const
Returns the name of the prim, property or relational attribute identified by the path, as a token.
static SDF_API bool IsValidPathString(const std::string &pathString, std::string *errMsg=0)
Return true if pathString is a valid path string, meaning that passing the string to the SdfPath cons...
SDF_API SdfPath AppendChild(TfToken const &childName) const
Creates a path by appending an element for childName to this path.
SDF_API const std::string & GetName() const
Returns the name of the prim, property or relational attribute identified by the path.
SDF_API SdfPath GetPrimPath() const
Creates a path by stripping all relational attributes, targets, properties, and variant selections fr...
SDF_API size_t GetPathElementCount() const
Returns the number of path elements in this path.
static SDF_API void RemoveDescendentPaths(SdfPathVector *paths)
Remove all elements of paths that are prefixed by other elements in paths.
SDF_API bool IsRelationalAttributePath() const
Returns whether the path identifies a relational attribute.
SDF_API SdfPath ReplacePrefix(const SdfPath &oldPrefix, const SdfPath &newPrefix, bool fixTargetPaths=true) const
Returns a path with all occurrences of the prefix path oldPrefix replaced with the prefix path newPre...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
static SDF_API SdfPathVector GetConciseRelativePaths(const SdfPathVector &paths)
Given some vector of paths, get a vector of concise unambiguous relative paths.
SDF_API bool IsPrimVariantSelectionPath() const
Returns whether the path identifies a variant selection for a prim.
SDF_API SdfPath MakeRelativePath(const SdfPath &anchor) const
Returns the relative form of this path using anchor as the relative basis.
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
SDF_API SdfPath AppendElementToken(const TfToken &elementTok) const
Like AppendElementString() but take the element as a TfToken.
SDF_API SdfPath ReplaceName(TfToken const &newName) const
Return a copy of this path with its final component changed to newName.
SDF_API void GetAllTargetPathsRecursively(SdfPathVector *result) const
Returns all the relationship target or connection target paths contained in this path, and recursively all the target paths contained in those target paths in reverse depth-first order.
static SDF_API const SdfPath & ReflexiveRelativePath()
The relative path representing &quot;self&quot;.
SDF_API TfToken GetElementToken() const
Like GetElementString() but return the value as a TfToken.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
SDF_API bool IsMapperArgPath() const
Returns whether the path identifies a connection mapper arg.
Function object for retrieving the N&#39;th element of a std::pair or std::tuple.
Definition: stl.h:391
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
SDF_API bool IsPropertyPath() const
Returns whether the path identifies a property.
SDF_API const std::string & GetString() const
Returns the string representation of this path as a std::string.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
static SDF_API std::string JoinIdentifier(const std::vector< std::string > &names)
Join names into a single identifier using the namespace delimiter.
SDF_API bool IsPrimPropertyPath() const
Returns whether the path identifies a prim&#39;s property.
SDF_API bool HasPrefix(const SdfPath &prefix) const
Return true if both this path and prefix are not the empty path and this path has prefix as a prefix...
SDF_API SdfPath AppendPath(const SdfPath &newSuffix) const
Creates a path by appending a given relative path to this path.
SDF_API SdfPath AppendVariantSelection(const std::string &variantSet, const std::string &variant) const
Creates a path by appending an element for variantSet and variant to this path.
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
SDF_API bool IsAbsoluteRootOrPrimPath() const
Returns whether the path identifies a prim or the absolute root.
SDF_API std::pair< SdfPath, SdfPath > RemoveCommonSuffix(const SdfPath &otherPath, bool stopAtRootPrim=false) const
Find and remove the longest common suffix from two paths.
static SDF_API std::pair< std::string, bool > StripPrefixNamespace(const std::string &name, const std::string &matchNamespace)
Returns (name, true) where name is stripped of the prefix specified by matchNamespace if name indeed ...
SDF_API const SdfPath & GetTargetPath() const
Returns the relational attribute or mapper target path for this path.
SDF_API SdfPath MakeAbsolutePath(const SdfPath &anchor) const
Returns the absolute form of this path using anchor as the relative basis.
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
SDF_API SdfPath AppendRelationalAttribute(TfToken const &attrName) const
Creates a path by appending an element for attrName to this path.
SDF_API bool IsRootPrimPath() const
Returns whether the path identifies a root prim.
SDF_API std::string GetElementString() const
Returns an ascii representation of the &quot;terminal&quot; element of this path, which can be used to reconstr...
static SDF_API std::vector< std::string > TokenizeIdentifier(const std::string &name)
Tokenizes name by the namespace delimiter.
SDF_API SdfPath AppendExpression() const
Creates a path by appending an expression element.
SDF_API SdfPath AppendElementString(const std::string &element) const
Creates a path by extracting and appending an element from the given ascii element encoding...
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:409
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
SDF_API SdfPath GetPrimOrPrimVariantSelectionPath() const
Creates a path by stripping all relational attributes, targets, and properties, leaving the nearest p...
SDF_API SdfPath GetCommonPrefix(const SdfPath &path) const
Returns a path with maximal length that is a prefix path of both this path and path.
SDF_API SdfPath AppendMapper(const SdfPath &targetPath) const
Creates a path by appending a mapper element for targetPath.
static SDF_API void RemoveAncestorPaths(SdfPathVector *paths)
Remove all elements of paths that prefix other elements in paths.
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
static SDF_API bool IsValidIdentifier(const std::string &name)
Returns whether name is a legal identifier for any path component.
SDF_API std::pair< std::string, std::string > GetVariantSelection() const
Returns the variant selection for this path, if this is a variant selection path. ...
SDF_API SdfPath StripAllVariantSelections() const
Create a path by stripping all variant selections from all components of this path, leaving a path with no embedded variant selections.