24 #ifndef PXR_USD_SDF_PATH_H
25 #define PXR_USD_SDF_PATH_H
28 #include "pxr/usd/sdf/api.h"
29 #include "pxr/usd/sdf/pool.h"
30 #include "pxr/usd/sdf/tokens.h"
35 #include <boost/intrusive_ptr.hpp>
36 #include <boost/operators.hpp>
42 #include <type_traits>
46 PXR_NAMESPACE_OPEN_SCOPE
56 typedef boost::intrusive_ptr<const Sdf_PathNode> Sdf_PathNodeConstRefPtr;
58 void intrusive_ptr_add_ref(Sdf_PathNode
const *);
59 void intrusive_ptr_release(Sdf_PathNode
const *);
62 struct Sdf_PathPrimTag;
63 struct Sdf_PathPropTag;
66 static constexpr
size_t Sdf_SizeofPrimPathNode =
sizeof(
void *) * 3;
67 static constexpr
size_t Sdf_SizeofPropPathNode =
sizeof(
void *) * 3;
69 using Sdf_PathPrimPartPool = Sdf_Pool<
70 Sdf_PathPrimTag, Sdf_SizeofPrimPathNode, 8>;
72 using Sdf_PathPropPartPool = Sdf_Pool<
73 Sdf_PathPropTag, Sdf_SizeofPropPathNode, 8>;
75 using Sdf_PathPrimHandle = Sdf_PathPrimPartPool::Handle;
76 using Sdf_PathPropHandle = Sdf_PathPropPartPool::Handle;
79 template <
class Handle,
bool Counted,
class PathNode=Sdf_PathNode const>
80 struct Sdf_PathNodeHandleImpl {
82 typedef Sdf_PathNodeHandleImpl this_type;
85 constexpr Sdf_PathNodeHandleImpl() noexcept {};
88 Sdf_PathNodeHandleImpl(Sdf_PathNode
const *p,
bool add_ref =
true)
89 : _poolHandle(Handle::GetHandle(reinterpret_cast<char const *>(p))) {
96 Sdf_PathNodeHandleImpl(Handle h,
bool add_ref =
true)
103 Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl
const &rhs)
104 : _poolHandle(rhs._poolHandle) {
110 ~Sdf_PathNodeHandleImpl() {
116 Sdf_PathNodeHandleImpl &
117 operator=(Sdf_PathNodeHandleImpl
const &rhs) {
118 if (Counted && *
this == rhs) {
121 this_type(rhs).swap(*
this);
125 Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl &&rhs) noexcept
126 : _poolHandle(rhs._poolHandle) {
127 rhs._poolHandle =
nullptr;
130 Sdf_PathNodeHandleImpl &
131 operator=(Sdf_PathNodeHandleImpl &&rhs) noexcept {
132 this_type(std::move(rhs)).swap(*
this);
136 Sdf_PathNodeHandleImpl &
137 operator=(Sdf_PathNode
const *rhs) noexcept {
138 this_type(rhs).swap(*
this);
142 void reset() noexcept {
143 _poolHandle = Handle {
nullptr };
147 get()
const noexcept {
148 return reinterpret_cast<Sdf_PathNode *
>(_poolHandle.GetPtr());
161 explicit operator bool() const noexcept {
162 return static_cast<bool>(_poolHandle);
165 void swap(Sdf_PathNodeHandleImpl &rhs) noexcept {
166 _poolHandle.swap(rhs._poolHandle);
169 inline bool operator==(Sdf_PathNodeHandleImpl
const &rhs)
const noexcept {
170 return _poolHandle == rhs._poolHandle;
172 inline bool operator!=(Sdf_PathNodeHandleImpl
const &rhs)
const noexcept {
173 return _poolHandle != rhs._poolHandle;
175 inline bool operator<(Sdf_PathNodeHandleImpl
const &rhs)
const noexcept {
176 return _poolHandle < rhs._poolHandle;
180 void _AddRef(Sdf_PathNode
const *p)
const {
182 intrusive_ptr_add_ref(p);
186 void _AddRef()
const {
190 void _DecRef()
const {
192 intrusive_ptr_release(
get());
196 Handle _poolHandle {
nullptr };
199 using Sdf_PathPrimNodeHandle =
200 Sdf_PathNodeHandleImpl<Sdf_PathPrimHandle,
true>;
202 using Sdf_PathPropNodeHandle =
203 Sdf_PathNodeHandleImpl<Sdf_PathPropHandle,
false>;
207 typedef std::set<class SdfPath> SdfPathSet;
209 typedef std::vector<class SdfPath> SdfPathVector;
212 VT_TYPE_IS_CHEAP_TO_COPY(
class SdfPath);
288 class SdfPath : boost::totally_ordered<SdfPath>
309 memset(
this, 0,
sizeof(*
this));
328 SDF_API
explicit SdfPath(
const std::string &path);
391 return static_cast<bool>(_propPart);
457 SDF_API
const std::string &
GetString()
const;
469 SDF_API
const char *
GetText()
const;
489 SDF_API
void GetPrefixes(SdfPathVector *prefixes)
const;
508 SDF_API
const std::string &
GetName()
const;
679 const std::string &variant)
const;
743 bool fixTargetPaths=
true)
const;
765 std::pair<SdfPath, SdfPath>
767 bool stopAtRootPrim =
false)
const;
821 static std::string
JoinIdentifier(
const std::vector<std::string> &names);
834 const std::string &rhs);
864 static std::pair<std::string, bool>
866 const std::string &matchNamespace);
874 std::string *errMsg = 0);
884 return _AsInt() == rhs._AsInt();
892 if (_AsInt() == rhs._AsInt()) {
895 if (!_primPart || !rhs._primPart) {
896 return !_primPart && rhs._primPart;
899 return _LessThanInternal(*
this, rhs);
902 template <
class HashState>
903 friend void TfHashAppend(HashState &h,
SdfPath const &path) {
906 uint32_t primPart, propPart;
907 memcpy(&primPart, &path._primPart,
sizeof(primPart));
908 memcpy(&propPart, &path._propPart,
sizeof(propPart));
915 inline size_t operator()(
const SdfPath& path)
const {
920 inline size_t GetHash()
const {
921 return Hash()(*this);
926 struct FastLessThan {
927 inline bool operator()(
const SdfPath& a,
const SdfPath& b)
const {
928 return a._AsInt() < b._AsInt();
943 SDF_API
static SdfPathVector
964 explicit SdfPath(Sdf_PathPrimNodeHandle &&primNode)
965 : _primPart(std::move(primNode)) {}
968 SdfPath(Sdf_PathPrimNodeHandle
const &primPart,
969 Sdf_PathPropNodeHandle
const &propPart)
970 : _primPart(primPart)
971 , _propPart(propPart) {}
974 SdfPath(Sdf_PathNode
const *primPart,
975 Sdf_PathNode
const *propPart)
976 : _primPart(primPart)
977 , _propPart(propPart) {}
979 friend class Sdf_PathNode;
980 friend class Sdfext_PathAccess;
985 _ElementsToString(
bool absolute,
const std::vector<std::string> &elements);
988 SdfPath const &newPrefix)
const;
991 SdfPath const &newPrefix)
const;
995 bool fixTargetPaths)
const;
1001 inline uint64_t _AsInt()
const {
1002 static_assert(
sizeof(*
this) ==
sizeof(uint64_t),
"");
1004 std::memcpy(&ret,
this,
sizeof(*
this));
1009 lhs._primPart.swap(rhs._primPart);
1010 lhs._propPart.swap(rhs._propPart);
1013 Sdf_PathPrimNodeHandle _primPart;
1014 Sdf_PathPropNodeHandle _propPart;
1042 const SdfPath& GetPath()
const {
return _path; }
1045 using iterator_category = std::forward_iterator_tag;
1047 using difference_type = std::ptrdiff_t;
1048 using reference =
const SdfPath&;
1049 using pointer =
const SdfPath*;
1051 iterator(
const SdfPath& path) : _path(path) {}
1053 iterator() =
default;
1056 iterator& operator++();
1058 const SdfPath& operator*()
const {
return _path; }
1060 const SdfPath* operator->()
const {
return &_path; }
1062 bool operator==(
const iterator& o)
const {
return _path == o._path; }
1064 bool operator!=(
const iterator& o)
const {
return _path != o._path; }
1069 SDF_API
friend difference_type
1070 distance(
const iterator& first,
const iterator& last);
1076 iterator begin()
const {
return iterator(_path); }
1078 iterator end()
const {
return iterator(); }
1086 inline size_t hash_value(
SdfPath const &path)
1088 return path.GetHash();
1096 struct Sdf_PathIdentity {
1108 template <
class ForwardIterator,
class GetPathFn = Sdf_PathIdentity>
1109 std::pair<ForwardIterator, ForwardIterator>
1110 SdfPathFindPrefixedRange(ForwardIterator begin, ForwardIterator end,
1112 GetPathFn
const &getPath = GetPathFn()) {
1114 typename std::iterator_traits<ForwardIterator>::reference;
1117 Compare(GetPathFn
const &getPath) : _getPath(getPath) {}
1118 GetPathFn
const &_getPath;
1119 bool operator()(IterRef a,
SdfPath const &b)
const {
1120 return _getPath(a) < b;
1124 std::pair<ForwardIterator, ForwardIterator> result;
1127 result.first = std::lower_bound(begin, end, prefix, Compare(getPath));
1131 result.second = TfFindBoundary(result.first, end,
1132 [&prefix, &getPath](IterRef iterRef) {
1133 return getPath(iterRef).HasPrefix(prefix);
1139 template <
class RandomAccessIterator,
class GetPathFn>
1140 RandomAccessIterator
1141 Sdf_PathFindLongestPrefixImpl(RandomAccessIterator begin,
1142 RandomAccessIterator end,
1145 GetPathFn
const &getPath)
1148 typename std::iterator_traits<RandomAccessIterator>::reference;
1151 Compare(GetPathFn
const &getPath) : _getPath(getPath) {}
1152 GetPathFn
const &_getPath;
1153 bool operator()(IterRef a,
SdfPath const &b)
const {
1154 return _getPath(a) < b;
1167 Compare comp(getPath);
1170 RandomAccessIterator result = std::lower_bound(begin, end, path, comp);
1174 if (!strictPrefix && result != end && getPath(*result) == path) {
1180 if (result == begin) {
1185 if (path.
HasPrefix(getPath(*--result))) {
1195 result = std::lower_bound(begin, end, newPath, comp);
1197 if (result != end && getPath(*result) == newPath) {
1200 if (result == begin) {
1203 if (newPath.
HasPrefix(getPath(*--result))) {
1217 template <
class RandomAccessIterator,
class GetPathFn = Sdf_PathIdentity,
1218 class =
typename std::enable_if<
1220 std::random_access_iterator_tag,
1221 typename std::iterator_traits<
1222 RandomAccessIterator>::iterator_category
1226 RandomAccessIterator
1227 SdfPathFindLongestPrefix(RandomAccessIterator begin,
1228 RandomAccessIterator end,
1230 GetPathFn
const &getPath = GetPathFn())
1232 return Sdf_PathFindLongestPrefixImpl(
1233 begin, end, path,
false, getPath);
1243 template <
class RandomAccessIterator,
class GetPathFn = Sdf_PathIdentity,
1244 class =
typename std::enable_if<
1246 std::random_access_iterator_tag,
1247 typename std::iterator_traits<
1248 RandomAccessIterator>::iterator_category
1252 RandomAccessIterator
1253 SdfPathFindLongestStrictPrefix(RandomAccessIterator begin,
1254 RandomAccessIterator end,
1256 GetPathFn
const &getPath = GetPathFn())
1258 return Sdf_PathFindLongestPrefixImpl(
1259 begin, end, path,
true, getPath);
1262 template <
class Iter,
class MapParam,
class GetPathFn = Sdf_PathIdentity>
1264 Sdf_PathFindLongestPrefixImpl(
1265 MapParam map,
SdfPath const &path,
bool strictPrefix,
1266 GetPathFn
const &getPath = GetPathFn())
1273 const Iter mapEnd = map.end();
1280 Iter result = map.lower_bound(path);
1284 if (!strictPrefix && result != mapEnd && getPath(*result) == path)
1289 if (result == map.begin())
1300 return Sdf_PathFindLongestPrefixImpl<Iter, MapParam>(
1309 typename std::set<SdfPath>::const_iterator
1310 SdfPathFindLongestPrefix(std::set<SdfPath>
const &set,
SdfPath const &path);
1316 typename std::map<SdfPath, T>::const_iterator
1317 SdfPathFindLongestPrefix(std::map<SdfPath, T>
const &map,
SdfPath const &path)
1319 return Sdf_PathFindLongestPrefixImpl<
1320 typename std::map<SdfPath, T>::const_iterator,
1321 std::map<SdfPath, T>
const &>(map, path,
false,
1325 typename std::map<SdfPath, T>::iterator
1326 SdfPathFindLongestPrefix(std::map<SdfPath, T> &map,
SdfPath const &path)
1328 return Sdf_PathFindLongestPrefixImpl<
1329 typename std::map<SdfPath, T>::iterator,
1330 std::map<SdfPath, T> &>(map, path,
false,
1338 typename std::set<SdfPath>::const_iterator
1339 SdfPathFindLongestStrictPrefix(std::set<SdfPath>
const &set,
1346 typename std::map<SdfPath, T>::const_iterator
1347 SdfPathFindLongestStrictPrefix(
1348 std::map<SdfPath, T>
const &map,
SdfPath const &path)
1350 return Sdf_PathFindLongestPrefixImpl<
1351 typename std::map<SdfPath, T>::const_iterator,
1352 std::map<SdfPath, T>
const &>(map, path,
true,
1356 typename std::map<SdfPath, T>::iterator
1357 SdfPathFindLongestStrictPrefix(
1358 std::map<SdfPath, T> &map,
SdfPath const &path)
1360 return Sdf_PathFindLongestPrefixImpl<
1361 typename std::map<SdfPath, T>::iterator,
1362 std::map<SdfPath, T> &>(map, path,
true,
1366 PXR_NAMESPACE_CLOSE_SCOPE
1371 #include "pxr/usd/sdf/pathNode.h"
1373 PXR_NAMESPACE_OPEN_SCOPE
1375 static_assert(Sdf_SizeofPrimPathNode ==
sizeof(Sdf_PrimPathNode),
"");
1376 static_assert(Sdf_SizeofPropPathNode ==
sizeof(Sdf_PrimPropertyPathNode),
"");
1378 PXR_NAMESPACE_CLOSE_SCOPE
1380 #endif // PXR_USD_SDF_PATH_H
Range representing a path and ancestors, and providing methods for iterating over them...
SDF_API TfToken const & GetToken() const
Return the string representation of this path as a TfToken lvalue.
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.
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 bool IsAbsoluteRootPath() const
Return true if this path is the AbsoluteRootPath().
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'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.
SDF_API SdfPath ReplaceTargetPath(const SdfPath &newTargetPath) const
Replaces the relational attribute's target path.
SDF_API TfToken GetAsToken() const
Return the string representation of this path as a TfToken.
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's namespace parent.
SDF_API bool IsPrimPath() const
Returns whether the path identifies a prim.
bool operator==(const SdfPath &rhs) const
Equality operator.
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.
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.
A user-extensible hashing mechanism for use with runtime hash tables.
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.
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 "self".
SdfPath() noexcept
Constructs the default, empty path.
SDF_API TfToken GetElementToken() const
Like GetElementString() but return the value as a TfToken.
std::vector< TfToken > TfTokenVector
Convenience types.
SDF_API bool IsMapperArgPath() const
Returns whether the path identifies a connection mapper arg.
Function object for retrieving the N'th element of a std::pair or std::tuple.
A path value used to locate objects in layers or scenegraphs.
SDF_API bool IsPropertyPath() const
Returns whether the path identifies a property.
SDF_API SdfPathAncestorsRange GetAncestorsRange() const
Return a range for iterating over the ancestors of this path.
SDF_API const std::string & GetString() const
Return 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'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 std::string GetAsString() const
Return the string representation of this path as a std::string.
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 "terminal" 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()).
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.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
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.