Loading...
Searching...
No Matches
path.h
1//
2// Copyright 2016 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_USD_SDF_PATH_H
8#define PXR_USD_SDF_PATH_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/api.h"
12#include "pxr/usd/sdf/pool.h"
13#include "pxr/usd/sdf/tokens.h"
14#include "pxr/base/arch/defines.h"
15#include "pxr/base/tf/delegatedCountPtr.h"
16#include "pxr/base/tf/span.h"
17#include "pxr/base/tf/stl.h"
18#include "pxr/base/tf/token.h"
19#include "pxr/base/vt/traits.h"
20
21#include <algorithm>
22#include <iterator>
23#include <set>
24#include <string>
25#include <type_traits>
26#include <utility>
27#include <vector>
28
29PXR_NAMESPACE_OPEN_SCOPE
30
31class Sdf_PathNode;
33
34// Ref-counting pointer to a path node.
35// Delegated ref-counts are used to keep the size of SdfPath
36// the same as a raw pointer. (shared_ptr, by comparison,
37// is the size of two pointers.)
38
40
41void TfDelegatedCountIncrement(Sdf_PathNode const *) noexcept;
42void TfDelegatedCountDecrement(Sdf_PathNode const *) noexcept;
43
44// Tags used for the pools of path nodes.
45struct Sdf_PathPrimTag;
46struct Sdf_PathPropTag;
47
48// These are validated below.
49static constexpr size_t Sdf_SizeofPrimPathNode = sizeof(void *) * 3;
50static constexpr size_t Sdf_SizeofPropPathNode = sizeof(void *) * 3;
51
52using Sdf_PathPrimPartPool = Sdf_Pool<
53 Sdf_PathPrimTag, Sdf_SizeofPrimPathNode, /*regionBits=*/8>;
54
55using Sdf_PathPropPartPool = Sdf_Pool<
56 Sdf_PathPropTag, Sdf_SizeofPropPathNode, /*regionBits=*/8>;
57
58using Sdf_PathPrimHandle = Sdf_PathPrimPartPool::Handle;
59using Sdf_PathPropHandle = Sdf_PathPropPartPool::Handle;
60
61// This handle class wraps up the raw Prim/PropPartPool handles.
62template <class Handle, bool Counted, class PathNode=Sdf_PathNode const>
63struct Sdf_PathNodeHandleImpl {
64private:
65 typedef Sdf_PathNodeHandleImpl this_type;
66
67public:
68 static constexpr bool IsCounted = Counted;
69
70 constexpr Sdf_PathNodeHandleImpl() noexcept {};
71
72 explicit
73 Sdf_PathNodeHandleImpl(Sdf_PathNode const *p, bool add_ref = true)
74 : _poolHandle(Handle::GetHandle(reinterpret_cast<char const *>(p))) {
75 if (p && add_ref) {
76 _AddRef(p);
77 }
78 }
79
80 explicit
81 Sdf_PathNodeHandleImpl(Handle h, bool add_ref = true)
82 : _poolHandle(h) {
83 if (h && add_ref) {
84 _AddRef();
85 }
86 }
87
88 Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl const &rhs) noexcept
89 : _poolHandle(rhs._poolHandle) {
90 if (_poolHandle) {
91 _AddRef();
92 }
93 }
94
95 ~Sdf_PathNodeHandleImpl() {
96 if (_poolHandle) {
97 _DecRef();
98 }
99 }
100
101 Sdf_PathNodeHandleImpl &
102 operator=(Sdf_PathNodeHandleImpl const &rhs) {
103 if (Counted && *this == rhs) {
104 return *this;
105 }
106 this_type(rhs).swap(*this);
107 return *this;
108 }
109
110 Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl &&rhs) noexcept
111 : _poolHandle(rhs._poolHandle) {
112 rhs._poolHandle = nullptr;
113 }
114
115 Sdf_PathNodeHandleImpl &
116 operator=(Sdf_PathNodeHandleImpl &&rhs) noexcept {
117 this_type(std::move(rhs)).swap(*this);
118 return *this;
119 }
120
121 Sdf_PathNodeHandleImpl &
122 operator=(Sdf_PathNode const *rhs) noexcept {
123 this_type(rhs).swap(*this);
124 return *this;
125 }
126
127 void reset() noexcept {
128 _poolHandle = Handle { nullptr };
129 }
130
131 inline Sdf_PathNode const *
132 get() const noexcept {
133 return reinterpret_cast<Sdf_PathNode *>(_poolHandle.GetPtr());
134 }
135
136 Sdf_PathNode const &
137 operator*() const {
138 return *get();
139 }
140
141 Sdf_PathNode const *
142 operator->() const {
143 return get();
144 }
145
146 explicit operator bool() const noexcept {
147 return static_cast<bool>(_poolHandle);
148 }
149
150 void swap(Sdf_PathNodeHandleImpl &rhs) noexcept {
151 _poolHandle.swap(rhs._poolHandle);
152 }
153
154 inline bool operator==(Sdf_PathNodeHandleImpl const &rhs) const noexcept {
155 return _poolHandle == rhs._poolHandle;
156 }
157 inline bool operator!=(Sdf_PathNodeHandleImpl const &rhs) const noexcept {
158 return _poolHandle != rhs._poolHandle;
159 }
160 inline bool operator<(Sdf_PathNodeHandleImpl const &rhs) const noexcept {
161 return _poolHandle < rhs._poolHandle;
162 }
163private:
164
165 inline void _AddRef(Sdf_PathNode const *p) const {
166 if (Counted) {
167 TfDelegatedCountIncrement(p);
168 }
169 }
170
171 inline void _AddRef() const {
172 _AddRef(get());
173 }
174
175 inline void _DecRef() const {
176 if (Counted) {
177 TfDelegatedCountDecrement(get());
178 }
179 }
180
181 Handle _poolHandle { nullptr };
182};
183
184using Sdf_PathPrimNodeHandle =
185 Sdf_PathNodeHandleImpl<Sdf_PathPrimHandle, /*Counted=*/true>;
186
187using Sdf_PathPropNodeHandle =
188 Sdf_PathNodeHandleImpl<Sdf_PathPropHandle, /*Counted=*/false>;
189
190
192typedef std::set<class SdfPath> SdfPathSet;
194typedef std::vector<class SdfPath> SdfPathVector;
195
196// Tell VtValue that SdfPath is cheap to copy.
197VT_TYPE_IS_CHEAP_TO_COPY(class SdfPath);
198
274{
275public:
277 SDF_API static const SdfPath & EmptyPath();
278
281 SDF_API static const SdfPath & AbsoluteRootPath();
282
284 SDF_API static const SdfPath & ReflexiveRelativePath();
285
288
291 SdfPath() noexcept = default;
292
305 //
306 // XXX We may want to revisit the behavior when constructing
307 // a path with an empty string ("") to accept it without error and
308 // return EmptyPath.
309 SDF_API explicit SdfPath(const std::string &path);
310
312
315
317 SDF_API size_t GetPathElementCount() const;
318
320 SDF_API bool IsAbsolutePath() const;
321
323 SDF_API bool IsAbsoluteRootPath() const;
324
326 SDF_API bool IsPrimPath() const;
327
329 SDF_API bool IsAbsoluteRootOrPrimPath() const;
330
335 SDF_API bool IsRootPrimPath() const;
336
342 SDF_API bool IsPropertyPath() const;
343
347 SDF_API bool IsPrimPropertyPath() const;
348
352 SDF_API bool IsNamespacedPropertyPath() const;
353
356 SDF_API bool IsPrimVariantSelectionPath() const;
357
361
364 SDF_API bool ContainsPrimVariantSelection() const;
365
372 return static_cast<bool>(_propPart);
373 }
374
377 SDF_API bool ContainsTargetPath() const;
378
382 SDF_API bool IsRelationalAttributePath() const;
383
386 SDF_API bool IsTargetPath() const;
387
389 SDF_API bool IsMapperPath() const;
390
392 SDF_API bool IsMapperArgPath() const;
393
395 SDF_API bool IsExpressionPath() const;
396
398 inline bool IsEmpty() const noexcept {
399 // No need to check _propPart, because it can only be non-null if
400 // _primPart is non-null.
401 return !_primPart;
402 }
403
409 SDF_API TfToken GetAsToken() const;
410
420 SDF_API TfToken const &GetToken() const;
421
427 SDF_API std::string GetAsString() const;
428
438 SDF_API const std::string &GetString() const;
439
450 SDF_API const char *GetText() const;
451
459 SDF_API SdfPathVector GetPrefixes() const;
460
469 SDF_API SdfPathVector GetPrefixes(size_t numPrefixes) const;
470
480 SDF_API void GetPrefixes(SdfPathVector *prefixes) const;
481
490 SDF_API void GetPrefixes(SdfPathVector *prefixes, size_t numPrefixes) const;
491
505
514
525 SDF_API const std::string &GetName() const;
526
529 SDF_API const TfToken &GetNameToken() const;
530
548 SDF_API std::string GetElementString() const;
549
551 SDF_API TfToken GetElementToken() const;
552
572 SDF_API SdfPath ReplaceName(TfToken const &newName) const;
573
586 SDF_API const SdfPath &GetTargetPath() const;
587
595 SDF_API void GetAllTargetPathsRecursively(SdfPathVector *result) const;
596
601 SDF_API
602 std::pair<std::string, std::string> GetVariantSelection() const;
603
606 SDF_API bool HasPrefix( const SdfPath &prefix ) const;
607
609
612
635 SDF_API SdfPath GetParentPath() const;
636
644 SDF_API SdfPath GetPrimPath() const;
645
655
662
667
675 SDF_API SdfPath AppendPath(const SdfPath &newSuffix) const;
676
682 SDF_API SdfPath AppendChild(TfToken const &childName) const;
683
688 SDF_API SdfPath AppendProperty(TfToken const &propName) const;
689
694 SDF_API
695 SdfPath AppendVariantSelection(const std::string &variantSet,
696 const std::string &variant) const;
697
702 SDF_API SdfPath AppendTarget(const SdfPath &targetPath) const;
703
708 SDF_API
710
714 SDF_API
715 SdfPath ReplaceTargetPath( const SdfPath &newTargetPath ) const;
716
721 SDF_API SdfPath AppendMapper(const SdfPath &targetPath) const;
722
727 SDF_API SdfPath AppendMapperArg(TfToken const &argName) const;
728
732 SDF_API SdfPath AppendExpression() const;
733
743 SDF_API SdfPath AppendElementString(const std::string &element) const;
744
746 SDF_API SdfPath AppendElementToken(const TfToken &elementTok) const;
747
757 SDF_API
758 SdfPath ReplacePrefix(const SdfPath &oldPrefix,
759 const SdfPath &newPrefix,
760 bool fixTargetPaths=true) const;
761
764 SDF_API SdfPath GetCommonPrefix(const SdfPath &path) const;
765
781 SDF_API
782 std::pair<SdfPath, SdfPath>
783 RemoveCommonSuffix(const SdfPath& otherPath,
784 bool stopAtRootPrim = false) const;
785
795 SDF_API SdfPath MakeAbsolutePath(const SdfPath & anchor) const;
796
809 SDF_API SdfPath MakeRelativePath(const SdfPath & anchor) const;
810
812
815
818 SDF_API static bool IsValidIdentifier(const std::string &name);
819
822 SDF_API static bool IsValidNamespacedIdentifier(const std::string &name);
823
827 SDF_API static std::vector<std::string> TokenizeIdentifier(const std::string &name);
828
832 SDF_API
833 static TfTokenVector TokenizeIdentifierAsTokens(const std::string &name);
834
837 SDF_API
838 static std::string JoinIdentifier(const std::vector<std::string> &names);
839
842 SDF_API
843 static std::string JoinIdentifier(const TfTokenVector& names);
844
849 SDF_API
850 static std::string JoinIdentifier(const std::string &lhs,
851 const std::string &rhs);
852
857 SDF_API
858 static std::string JoinIdentifier(const TfToken &lhs, const TfToken &rhs);
859
863 SDF_API
864 static std::string StripNamespace(const std::string &name);
865
869 SDF_API
870 static TfToken StripNamespace(const TfToken &name);
871
880 SDF_API
881 static std::pair<std::string, bool>
882 StripPrefixNamespace(const std::string &name,
883 const std::string &matchNamespace);
884
889 SDF_API
890 static bool IsValidPathString(const std::string &pathString,
891 std::string *errMsg = 0);
892
894
897
899 inline bool operator==(const SdfPath &rhs) const {
900 return _AsInt() == rhs._AsInt();
901 }
902
904 inline bool operator!=(const SdfPath &rhs) const {
905 return !(*this == rhs);
906 }
907
912 inline bool operator<(const SdfPath &rhs) const {
913 if (_AsInt() == rhs._AsInt()) {
914 return false;
915 }
916 if (!_primPart || !rhs._primPart) {
917 return !_primPart && rhs._primPart;
918 }
919 // Valid prim parts -- must walk node structure, etc.
920 return _LessThanInternal(*this, rhs);
921 }
922
925 inline bool operator>(const SdfPath& rhs) const {
926 return rhs < *this;
927 }
928
931 inline bool operator<=(const SdfPath& rhs) const {
932 return !(rhs < *this);
933 }
934
937 inline bool operator>=(const SdfPath& rhs) const {
938 return !(*this < rhs);
939 }
940
941 template <class HashState>
942 friend void TfHashAppend(HashState &h, SdfPath const &path) {
943 // The hash function is pretty sensitive performance-wise. Be
944 // careful making changes here, and run tests.
945 uint32_t primPart, propPart;
946 memcpy(&primPart, &path._primPart, sizeof(primPart));
947 memcpy(&propPart, &path._propPart, sizeof(propPart));
948 h.Append(primPart);
949 h.Append(propPart);
950 }
951
952 // For hash maps and sets
953 struct Hash {
954 inline size_t operator()(const SdfPath& path) const {
955 return TfHash()(path);
956 }
957 };
958
959 inline size_t GetHash() const {
960 return Hash()(*this);
961 }
962
963 // For cases where an unspecified total order that is not stable from
964 // run-to-run is needed.
965 struct FastLessThan {
966 inline bool operator()(const SdfPath& a, const SdfPath& b) const {
967 return a._AsInt() < b._AsInt();
968 }
969 };
970
972
975
982 SDF_API static SdfPathVector
983 GetConciseRelativePaths(const SdfPathVector& paths);
984
988 SDF_API static void RemoveDescendentPaths(SdfPathVector *paths);
989
992 SDF_API static void RemoveAncestorPaths(SdfPathVector *paths);
993
995
996private:
997
998 // This is used for all internal path construction where we do operations
999 // via nodes and then want to return a new path with a resulting prim and
1000 // property parts.
1001
1002 // Accept rvalues.
1003 explicit SdfPath(Sdf_PathPrimNodeHandle &&primNode)
1004 : _primPart(std::move(primNode)) {}
1005
1006 SdfPath(Sdf_PathPrimNodeHandle &&primPart,
1007 Sdf_PathPropNodeHandle &&propPart)
1008 : _primPart(std::move(primPart))
1009 , _propPart(std::move(propPart)) {}
1010
1011 // Construct from prim & prop parts.
1012 SdfPath(Sdf_PathPrimNodeHandle const &primPart,
1013 Sdf_PathPropNodeHandle const &propPart)
1014 : _primPart(primPart)
1015 , _propPart(propPart) {}
1016
1017 // Construct from prim & prop node pointers.
1018 SdfPath(Sdf_PathNode const *primPart,
1019 Sdf_PathNode const *propPart)
1020 : _primPart(primPart)
1021 , _propPart(propPart) {}
1022
1023 friend class Sdf_PathNode;
1024 friend class Sdfext_PathAccess;
1025 friend class SdfPathAncestorsRange;
1026 friend class Sdf_PathInitAccess;
1027
1028 SdfPath _ReplacePrimPrefix(SdfPath const &oldPrefix,
1029 SdfPath const &newPrefix) const;
1030
1031 SdfPath _ReplaceTargetPathPrefixes(SdfPath const &oldPrefix,
1032 SdfPath const &newPrefix) const;
1033
1034 SdfPath _ReplacePropPrefix(SdfPath const &oldPrefix,
1035 SdfPath const &newPrefix,
1036 bool fixTargetPaths) const;
1037
1038 // Helper to implement the uninlined portion of operator<.
1039 SDF_API static bool
1040 _LessThanInternal(SdfPath const &lhs, SdfPath const &rhs);
1041
1042 inline uint64_t _AsInt() const {
1043 static_assert(sizeof(*this) == sizeof(uint64_t), "");
1044 uint64_t ret;
1045 std::memcpy(&ret, this, sizeof(*this));
1046 return ret;
1047 }
1048
1049 friend void swap(SdfPath &lhs, SdfPath &rhs) {
1050 lhs._primPart.swap(rhs._primPart);
1051 lhs._propPart.swap(rhs._propPart);
1052 }
1053
1054 SDF_API friend char const *
1055 Sdf_PathGetDebuggerPathText(SdfPath const &);
1056
1057 Sdf_PathPrimNodeHandle _primPart;
1058 Sdf_PathPropNodeHandle _propPart;
1059
1060};
1061
1062
1080{
1081public:
1082
1083 SdfPathAncestorsRange(const SdfPath& path)
1084 : _path(path) {}
1085
1086 const SdfPath& GetPath() const { return _path; }
1087
1088 struct iterator {
1089 using iterator_category = std::forward_iterator_tag;
1090 using value_type = SdfPath;
1091 using difference_type = std::ptrdiff_t;
1092 using reference = const SdfPath&;
1093 using pointer = const SdfPath*;
1094
1095 iterator(const SdfPath& path) : _path(path) {}
1096
1097 iterator() = default;
1098
1099 SDF_API
1100 iterator& operator++();
1101
1102 const SdfPath& operator*() const { return _path; }
1103
1104 const SdfPath* operator->() const { return &_path; }
1105
1106 bool operator==(const iterator& o) const { return _path == o._path; }
1107
1108 bool operator!=(const iterator& o) const { return _path != o._path; }
1109
1113 SDF_API friend difference_type
1114 distance(const iterator& first, const iterator& last);
1115
1116 private:
1117 SdfPath _path;
1118 };
1119
1120 iterator begin() const { return iterator(_path); }
1121
1122 iterator end() const { return iterator(); }
1123
1124private:
1125 SdfPath _path;
1126};
1127
1128
1129// Overload hash_value for SdfPath. Used by things like boost::hash.
1130inline size_t hash_value(SdfPath const &path)
1131{
1132 return path.GetHash();
1133}
1134
1136SDF_API std::ostream & operator<<( std::ostream &out, const SdfPath &path );
1137
1138// Helper for SdfPathFindPrefixedRange & SdfPathFindLongestPrefix. A function
1139// object that returns an SdfPath const & unchanged.
1140struct Sdf_PathIdentity {
1141 inline SdfPath const &operator()(SdfPath const &arg) const {
1142 return arg;
1143 }
1144};
1145
1152template <class ForwardIterator, class GetPathFn = Sdf_PathIdentity>
1153std::pair<ForwardIterator, ForwardIterator>
1154SdfPathFindPrefixedRange(ForwardIterator begin, ForwardIterator end,
1155 SdfPath const &prefix,
1156 GetPathFn const &getPath = GetPathFn()) {
1157 using IterRef =
1158 typename std::iterator_traits<ForwardIterator>::reference;
1159
1160 struct Compare {
1161 Compare(GetPathFn const &getPath) : _getPath(getPath) {}
1162 GetPathFn const &_getPath;
1163 bool operator()(IterRef a, SdfPath const &b) const {
1164 return _getPath(a) < b;
1165 }
1166 };
1167
1168 std::pair<ForwardIterator, ForwardIterator> result;
1169
1170 // First, use lower_bound to find where \a prefix would go.
1171 result.first = std::lower_bound(begin, end, prefix, Compare(getPath));
1172
1173 // Next, find end of range starting from the lower bound, using the
1174 // prefixing condition to define the boundary.
1175 result.second = TfFindBoundary(result.first, end,
1176 [&prefix, &getPath](IterRef iterRef) {
1177 return getPath(iterRef).HasPrefix(prefix);
1178 });
1179
1180 return result;
1181}
1182
1183template <class RandomAccessIterator, class GetPathFn>
1184RandomAccessIterator
1185Sdf_PathFindLongestPrefixImpl(RandomAccessIterator begin,
1186 RandomAccessIterator end,
1187 SdfPath const &path,
1188 bool strictPrefix,
1189 GetPathFn const &getPath)
1190{
1191 using IterRef =
1192 typename std::iterator_traits<RandomAccessIterator>::reference;
1193
1194 struct Compare {
1195 Compare(GetPathFn const &getPath) : _getPath(getPath) {}
1196 GetPathFn const &_getPath;
1197 bool operator()(IterRef a, SdfPath const &b) const {
1198 return _getPath(a) < b;
1199 }
1200 };
1201
1202 // Search for the path in [begin, end). If present, return it. If not,
1203 // examine prior element in [begin, end). If none, return end. Else, is it
1204 // a prefix of path? If so, return it. Else find common prefix of that
1205 // element and path and recurse.
1206
1207 // If empty sequence, return.
1208 if (begin == end)
1209 return end;
1210
1211 Compare comp(getPath);
1212
1213 // Search for where this path would lexicographically appear in the range.
1214 RandomAccessIterator result = std::lower_bound(begin, end, path, comp);
1215
1216 // If we didn't get the end, check to see if we got the path exactly if
1217 // we're not looking for a strict prefix.
1218 if (!strictPrefix && result != end && getPath(*result) == path) {
1219 return result;
1220 }
1221
1222 // If we got begin (and didn't match in the case of a non-strict prefix)
1223 // then there's no prefix.
1224 if (result == begin) {
1225 return end;
1226 }
1227
1228 // If the prior element is a prefix, we're done.
1229 if (path.HasPrefix(getPath(*--result))) {
1230 return result;
1231 }
1232
1233 // Otherwise, find the common prefix of the lexicographical predecessor and
1234 // look for its prefix in the preceding range.
1235 SdfPath newPath = path.GetCommonPrefix(getPath(*result));
1236 auto origEnd = end;
1237 do {
1238 end = result;
1239 result = std::lower_bound(begin, end, newPath, comp);
1240
1241 if (result != end && getPath(*result) == newPath) {
1242 return result;
1243 }
1244 if (result == begin) {
1245 return origEnd;
1246 }
1247 if (newPath.HasPrefix(getPath(*--result))) {
1248 return result;
1249 }
1250 newPath = newPath.GetCommonPrefix(getPath(*result));
1251 } while (true);
1252}
1253
1261template <class RandomAccessIterator, class GetPathFn = Sdf_PathIdentity,
1262 class = typename std::enable_if<
1263 std::is_base_of<
1264 std::random_access_iterator_tag,
1265 typename std::iterator_traits<
1266 RandomAccessIterator>::iterator_category
1267 >::value
1268 >::type
1269 >
1270RandomAccessIterator
1271SdfPathFindLongestPrefix(RandomAccessIterator begin,
1272 RandomAccessIterator end,
1273 SdfPath const &path,
1274 GetPathFn const &getPath = GetPathFn())
1275{
1276 return Sdf_PathFindLongestPrefixImpl(
1277 begin, end, path, /*strictPrefix=*/false, getPath);
1278}
1279
1287template <class RandomAccessIterator, class GetPathFn = Sdf_PathIdentity,
1288 class = typename std::enable_if<
1289 std::is_base_of<
1290 std::random_access_iterator_tag,
1291 typename std::iterator_traits<
1292 RandomAccessIterator>::iterator_category
1293 >::value
1294 >::type
1295 >
1296RandomAccessIterator
1297SdfPathFindLongestStrictPrefix(RandomAccessIterator begin,
1298 RandomAccessIterator end,
1299 SdfPath const &path,
1300 GetPathFn const &getPath = GetPathFn())
1301{
1302 return Sdf_PathFindLongestPrefixImpl(
1303 begin, end, path, /*strictPrefix=*/true, getPath);
1304}
1305
1306template <class Iter, class MapParam, class GetPathFn = Sdf_PathIdentity>
1307Iter
1308Sdf_PathFindLongestPrefixImpl(
1309 MapParam map, SdfPath const &path, bool strictPrefix,
1310 GetPathFn const &getPath = GetPathFn())
1311{
1312 // Search for the path in map. If present, return it. If not, examine
1313 // prior element in map. If none, return end. Else, is it a prefix of
1314 // path? If so, return it. Else find common prefix of that element and
1315 // path and recurse.
1316
1317 const Iter mapEnd = map.end();
1318
1319 // If empty, return.
1320 if (map.empty())
1321 return mapEnd;
1322
1323 // Search for where this path would lexicographically appear in the range.
1324 Iter result = map.lower_bound(path);
1325
1326 // If we didn't get the end, check to see if we got the path exactly if
1327 // we're not looking for a strict prefix.
1328 if (!strictPrefix && result != mapEnd && getPath(*result) == path)
1329 return result;
1330
1331 // If we got begin (and didn't match in the case of a non-strict prefix)
1332 // then there's no prefix.
1333 if (result == map.begin())
1334 return mapEnd;
1335
1336 // If the prior element is a prefix, we're done.
1337 if (path.HasPrefix(getPath(*--result)))
1338 return result;
1339
1340 // Otherwise, find the common prefix of the lexicographical predecessor and
1341 // recurse looking for it or its longest prefix in the preceding range. We
1342 // always pass strictPrefix=false, since now we're operating on prefixes of
1343 // the original caller's path.
1344 return Sdf_PathFindLongestPrefixImpl<Iter, MapParam>(
1345 map, path.GetCommonPrefix(getPath(*result)), /*strictPrefix=*/false,
1346 getPath);
1347}
1348
1352SDF_API
1353typename std::set<SdfPath>::const_iterator
1354SdfPathFindLongestPrefix(std::set<SdfPath> const &set, SdfPath const &path);
1355
1359template <class T>
1360typename std::map<SdfPath, T>::const_iterator
1361SdfPathFindLongestPrefix(std::map<SdfPath, T> const &map, SdfPath const &path)
1362{
1363 return Sdf_PathFindLongestPrefixImpl<
1364 typename std::map<SdfPath, T>::const_iterator,
1365 std::map<SdfPath, T> const &>(map, path, /*strictPrefix=*/false,
1366 TfGet<0>());
1367}
1368template <class T>
1369typename std::map<SdfPath, T>::iterator
1370SdfPathFindLongestPrefix(std::map<SdfPath, T> &map, SdfPath const &path)
1371{
1372 return Sdf_PathFindLongestPrefixImpl<
1373 typename std::map<SdfPath, T>::iterator,
1374 std::map<SdfPath, T> &>(map, path, /*strictPrefix=*/false,
1375 TfGet<0>());
1376}
1377
1381SDF_API
1382typename std::set<SdfPath>::const_iterator
1383SdfPathFindLongestStrictPrefix(std::set<SdfPath> const &set,
1384 SdfPath const &path);
1385
1389template <class T>
1390typename std::map<SdfPath, T>::const_iterator
1391SdfPathFindLongestStrictPrefix(
1392 std::map<SdfPath, T> const &map, SdfPath const &path)
1393{
1394 return Sdf_PathFindLongestPrefixImpl<
1395 typename std::map<SdfPath, T>::const_iterator,
1396 std::map<SdfPath, T> const &>(map, path, /*strictPrefix=*/true,
1397 TfGet<0>());
1398}
1399template <class T>
1400typename std::map<SdfPath, T>::iterator
1401SdfPathFindLongestStrictPrefix(
1402 std::map<SdfPath, T> &map, SdfPath const &path)
1403{
1404 return Sdf_PathFindLongestPrefixImpl<
1405 typename std::map<SdfPath, T>::iterator,
1406 std::map<SdfPath, T> &>(map, path, /*strictPrefix=*/true,
1407 TfGet<0>());
1408}
1409
1410// A helper function for debugger pretty-printers, etc. This function is *not*
1411// thread-safe. It writes to a static buffer and returns a pointer to it.
1412// Subsequent calls to this function overwrite the memory written in prior
1413// calls. If the given path's string representation exceeds the static buffer
1414// size, return a pointer to a message indicating so.
1415SDF_API
1416char const *
1417Sdf_PathGetDebuggerPathText(SdfPath const &);
1418
1419PXR_NAMESPACE_CLOSE_SCOPE
1420
1421// Sdf_PathNode is not public API, but we need to include it here
1422// so we can inline the ref-counting operations, which must manipulate
1423// its internal _refCount member.
1424#include "pxr/usd/sdf/pathNode.h"
1425
1426PXR_NAMESPACE_OPEN_SCOPE
1427
1428static_assert(Sdf_SizeofPrimPathNode == sizeof(Sdf_PrimPathNode), "");
1429static_assert(Sdf_SizeofPropPathNode == sizeof(Sdf_PrimPropertyPathNode), "");
1430
1431PXR_NAMESPACE_CLOSE_SCOPE
1432
1433#endif // PXR_USD_SDF_PATH_H
Range representing a path and ancestors, and providing methods for iterating over them.
Definition: path.h:1080
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
SDF_API SdfPath MakeAbsolutePath(const SdfPath &anchor) const
Returns the absolute form of this path using anchor as the relative basis.
SDF_API SdfPath GetParentPath() const
Return the path that identifies this path's namespace parent.
SDF_API const std::string & GetString() const
Return the string representation of this path as a std::string.
SDF_API bool IsPrimVariantSelectionPath() const
Returns whether the path identifies a variant selection for a prim.
static SDF_API std::string JoinIdentifier(const TfTokenVector &names)
Join names into a single identifier using the namespace delimiter.
static SDF_API bool IsValidIdentifier(const std::string &name)
Returns whether name is a legal identifier for any path component.
SDF_API SdfPath ReplaceTargetPath(const SdfPath &newTargetPath) const
Replaces the relational attribute's target path.
SDF_API void GetAllTargetPathsRecursively(SdfPathVector *result) const
Returns all the relationship target or connection target paths contained in this path,...
SDF_API SdfPath GetPrimOrPrimVariantSelectionPath() const
Creates a path by stripping all relational attributes, targets, and properties, leaving the nearest p...
SDF_API std::pair< SdfPath, SdfPath > RemoveCommonSuffix(const SdfPath &otherPath, bool stopAtRootPrim=false) const
Find and remove the longest common suffix from two paths.
SDF_API SdfPath AppendElementString(const std::string &element) const
Creates a path by extracting and appending an element from the given ascii element encoding.
SDF_API bool IsMapperArgPath() const
Returns whether the path identifies a connection mapper arg.
SDF_API bool IsRelationalAttributePath() const
Returns whether the path identifies a relational attribute.
static SDF_API TfToken StripNamespace(const TfToken &name)
Returns name stripped of any namespaces.
SDF_API bool IsAbsoluteRootOrPrimPath() const
Returns whether the path identifies a prim or the absolute root.
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
static SDF_API SdfPathVector GetConciseRelativePaths(const SdfPathVector &paths)
Given some vector of paths, get a vector of concise unambiguous relative paths.
SDF_API std::pair< std::string, std::string > GetVariantSelection() const
Returns the variant selection for this path, if this is a variant selection path.
bool operator<=(const SdfPath &rhs) const
Less than or equal operator.
Definition: path.h:931
SDF_API SdfPath AppendExpression() const
Creates a path by appending an expression element.
SDF_API SdfPath AppendRelationalAttribute(TfToken const &attrName) const
Creates a path by appending an element for attrName to this path.
SDF_API std::string GetElementString() const
Returns an ascii representation of the "terminal" element of this path, which can be used to reconstr...
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
SDF_API bool IsAbsolutePath() const
Returns whether the path is absolute.
SDF_API SdfPath GetAbsoluteRootOrPrimPath() const
Creates a path by stripping all properties and relational attributes from this path,...
SDF_API SdfPath AppendPath(const SdfPath &newSuffix) const
Creates a path by appending a given relative path to this path.
SDF_API SdfPath MakeRelativePath(const SdfPath &anchor) const
Returns the relative form of this path using anchor as the relative basis.
static SDF_API const SdfPath & ReflexiveRelativePath()
The relative path representing "self".
bool operator>(const SdfPath &rhs) const
Greater than operator.
Definition: path.h:925
static SDF_API std::string JoinIdentifier(const std::string &lhs, const std::string &rhs)
Join lhs and rhs into a single identifier using the namespace delimiter.
static SDF_API std::vector< std::string > TokenizeIdentifier(const std::string &name)
Tokenizes name by the namespace delimiter.
SDF_API bool IsPropertyPath() const
Returns whether the path identifies a property.
SDF_API SdfPath AppendMapper(const SdfPath &targetPath) const
Creates a path by appending a mapper element for targetPath.
SDF_API SdfPath AppendElementToken(const TfToken &elementTok) const
Like AppendElementString() but take the element as a TfToken.
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.
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 IsTargetPath() const
Returns whether the path identifies a relationship or connection target.
SDF_API size_t GetPathElementCount() const
Returns the number of path elements in this path.
SDF_API TfToken GetElementToken() const
Like GetElementString() but return the value as a TfToken.
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
SDF_API const std::string & GetName() const
Returns the name of the prim, property or relational attribute identified by the path.
SDF_API TfToken GetAsToken() const
Return the string representation of this path as a TfToken.
SDF_API const SdfPath & GetTargetPath() const
Returns the relational attribute or mapper target path for this path.
SDF_API SdfPathVector GetPrefixes() const
Returns the prefix paths of this path.
static SDF_API std::string StripNamespace(const std::string &name)
Returns name stripped of any namespaces.
SDF_API void GetPrefixes(SdfPathVector *prefixes, size_t numPrefixes) const
Fill prefixes with up to numPrefixes prefixes of this path.
static SDF_API TfTokenVector TokenizeIdentifierAsTokens(const std::string &name)
Tokenizes name by the namespace delimiter.
SDF_API bool IsRootPrimPath() const
Returns whether the path identifies a root prim.
static SDF_API bool IsValidNamespacedIdentifier(const std::string &name)
Returns whether name is a legal namespaced identifier.
SDF_API const TfToken & GetNameToken() const
Returns the name of the prim, property or relational attribute identified by the path,...
SDF_API bool IsPrimPath() const
Returns whether the path identifies a prim.
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 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.
SDF_API SdfPath AppendProperty(TfToken const &propName) const
Creates a path by appending an element for propName to this path.
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 AppendMapperArg(TfToken const &argName) const
Creates a path by appending an element for argName.
SDF_API TfToken const & GetToken() const
Return the string representation of this path as a TfToken lvalue.
bool operator<(const SdfPath &rhs) const
Comparison operator.
Definition: path.h:912
bool operator>=(const SdfPath &rhs) const
Greater than or equal operator.
Definition: path.h:937
SDF_API SdfPath AppendChild(TfToken const &childName) const
Creates a path by appending an element for childName to this path.
bool ContainsPropertyElements() const
Return true if this path contains any property elements, false otherwise.
Definition: path.h:371
SDF_API bool IsMapperPath() const
Returns whether the path identifies a connection mapper.
static SDF_API void RemoveDescendentPaths(SdfPathVector *paths)
Remove all elements of paths that are prefixed by other elements in paths.
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.
static SDF_API std::string JoinIdentifier(const TfToken &lhs, const TfToken &rhs)
Join lhs and rhs into a single identifier using the namespace delimiter.
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 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...
SDF_API TfSpan< SdfPath > GetPrefixes(TfSpan< SdfPath > prefixes) const
Fill prefixes with up to prefixes.size() prefixes of this path.
SDF_API bool IsAbsoluteRootPath() const
Return true if this path is the AbsoluteRootPath().
bool operator==(const SdfPath &rhs) const
Equality operator.
Definition: path.h:899
SDF_API SdfPathAncestorsRange GetAncestorsRange() const
Return a range for iterating over the ancestors of this path.
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 IsPrimPropertyPath() const
Returns whether the path identifies a prim's property.
SDF_API SdfPath StripAllVariantSelections() const
Create a path by stripping all variant selections from all components of this path,...
SDF_API bool ContainsTargetPath() const
Return true if this path is or has a prefix that's a target path or a mapper path.
SDF_API SdfPath ReplaceName(TfToken const &newName) const
Return a copy of this path with its final component changed to newName.
SDF_API void GetPrefixes(SdfPathVector *prefixes) const
Fills prefixes with prefixes of this 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 AppendTarget(const SdfPath &targetPath) const
Creates a path by appending an element for targetPath.
SdfPath() noexcept=default
Constructs the default, empty path.
SDF_API SdfPathVector GetPrefixes(size_t numPrefixes) const
Return up to numPrefixes prefix paths of this path.
SDF_API SdfPath GetPrimPath() const
Creates a path by stripping all relational attributes, targets, properties, and variant selections fr...
static SDF_API void RemoveAncestorPaths(SdfPathVector *paths)
Remove all elements of paths that prefix other elements in paths.
bool operator!=(const SdfPath &rhs) const
Inequality operator.
Definition: path.h:904
SDF_API std::string GetAsString() const
Return the string representation of this path as a std::string.
Stores a pointer to a ValueType which uses TfDelegatedCountIncrement and TfDelegatedCountDecrement to...
Function object for retrieving the N'th element of a std::pair or std::tuple.
Definition: stl.h:362
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
Represents a range of contiguous elements.
Definition: span.h:71
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].
STL namespace.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440