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"
31#include "pxr/base/arch/defines.h"
32#include "pxr/base/tf/delegatedCountPtr.h"
45PXR_NAMESPACE_OPEN_SCOPE
57void TfDelegatedCountIncrement(Sdf_PathNode
const *)
noexcept;
58void TfDelegatedCountDecrement(Sdf_PathNode
const *)
noexcept;
61struct Sdf_PathPrimTag;
62struct Sdf_PathPropTag;
65static constexpr size_t Sdf_SizeofPrimPathNode =
sizeof(
void *) * 3;
66static constexpr size_t Sdf_SizeofPropPathNode =
sizeof(
void *) * 3;
68using Sdf_PathPrimPartPool = Sdf_Pool<
69 Sdf_PathPrimTag, Sdf_SizeofPrimPathNode, 8>;
71using Sdf_PathPropPartPool = Sdf_Pool<
72 Sdf_PathPropTag, Sdf_SizeofPropPathNode, 8>;
74using Sdf_PathPrimHandle = Sdf_PathPrimPartPool::Handle;
75using Sdf_PathPropHandle = Sdf_PathPropPartPool::Handle;
78template <
class Handle,
bool Counted,
class PathNode=Sdf_PathNode const>
79struct Sdf_PathNodeHandleImpl {
81 typedef Sdf_PathNodeHandleImpl this_type;
84 static constexpr bool IsCounted = Counted;
86 constexpr Sdf_PathNodeHandleImpl() noexcept {};
89 Sdf_PathNodeHandleImpl(Sdf_PathNode
const *p,
bool add_ref =
true)
90 : _poolHandle(Handle::GetHandle(reinterpret_cast<char const *>(p))) {
97 Sdf_PathNodeHandleImpl(Handle h,
bool add_ref =
true)
104 Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl
const &rhs) noexcept
105 : _poolHandle(rhs._poolHandle) {
111 ~Sdf_PathNodeHandleImpl() {
117 Sdf_PathNodeHandleImpl &
118 operator=(Sdf_PathNodeHandleImpl
const &rhs) {
119 if (Counted && *
this == rhs) {
122 this_type(rhs).swap(*
this);
126 Sdf_PathNodeHandleImpl(Sdf_PathNodeHandleImpl &&rhs) noexcept
127 : _poolHandle(rhs._poolHandle) {
128 rhs._poolHandle =
nullptr;
131 Sdf_PathNodeHandleImpl &
132 operator=(Sdf_PathNodeHandleImpl &&rhs)
noexcept {
133 this_type(std::move(rhs)).swap(*
this);
137 Sdf_PathNodeHandleImpl &
138 operator=(Sdf_PathNode
const *rhs)
noexcept {
139 this_type(rhs).swap(*
this);
143 void reset() noexcept {
144 _poolHandle = Handle {
nullptr };
147 inline Sdf_PathNode
const *
148 get() const noexcept {
149 return reinterpret_cast<Sdf_PathNode *
>(_poolHandle.GetPtr());
162 explicit operator bool() const noexcept {
163 return static_cast<bool>(_poolHandle);
166 void swap(Sdf_PathNodeHandleImpl &rhs)
noexcept {
167 _poolHandle.swap(rhs._poolHandle);
170 inline bool operator==(Sdf_PathNodeHandleImpl
const &rhs)
const noexcept {
171 return _poolHandle == rhs._poolHandle;
173 inline bool operator!=(Sdf_PathNodeHandleImpl
const &rhs)
const noexcept {
174 return _poolHandle != rhs._poolHandle;
176 inline bool operator<(Sdf_PathNodeHandleImpl
const &rhs)
const noexcept {
177 return _poolHandle < rhs._poolHandle;
181 inline void _AddRef(Sdf_PathNode
const *p)
const {
183 TfDelegatedCountIncrement(p);
187 inline void _AddRef()
const {
191 inline void _DecRef()
const {
193 TfDelegatedCountDecrement(get());
197 Handle _poolHandle {
nullptr };
200using Sdf_PathPrimNodeHandle =
201 Sdf_PathNodeHandleImpl<Sdf_PathPrimHandle,
true>;
203using Sdf_PathPropNodeHandle =
204 Sdf_PathNodeHandleImpl<Sdf_PathPropHandle,
false>;
208typedef std::set<class SdfPath> SdfPathSet;
210typedef std::vector<class SdfPath> SdfPathVector;
213VT_TYPE_IS_CHEAP_TO_COPY(
class SdfPath);
388 return static_cast<bool>(_propPart);
506 SDF_API
void GetPrefixes(SdfPathVector *prefixes,
size_t numPrefixes)
const;
696 const std::string &variant)
const;
760 bool fixTargetPaths=
true)
const;
782 std::pair<SdfPath, SdfPath>
784 bool stopAtRootPrim =
false)
const;
851 const std::string &rhs);
881 static std::pair<std::string, bool>
883 const std::string &matchNamespace);
891 std::string *errMsg = 0);
900 return _AsInt() == rhs._AsInt();
905 return !(*
this == rhs);
913 if (_AsInt() == rhs._AsInt()) {
916 if (!_primPart || !rhs._primPart) {
917 return !_primPart && rhs._primPart;
920 return _LessThanInternal(*
this, rhs);
932 return !(rhs < *
this);
938 return !(*
this < rhs);
941 template <
class HashState>
942 friend void TfHashAppend(HashState &h,
SdfPath const &path) {
945 uint32_t primPart, propPart;
946 memcpy(&primPart, &path._primPart,
sizeof(primPart));
947 memcpy(&propPart, &path._propPart,
sizeof(propPart));
954 inline size_t operator()(
const SdfPath& path)
const {
959 inline size_t GetHash()
const {
960 return Hash()(*this);
965 struct FastLessThan {
966 inline bool operator()(
const SdfPath& a,
const SdfPath& b)
const {
967 return a._AsInt() < b._AsInt();
982 SDF_API
static SdfPathVector
1003 explicit SdfPath(Sdf_PathPrimNodeHandle &&primNode)
1004 : _primPart(
std::move(primNode)) {}
1006 SdfPath(Sdf_PathPrimNodeHandle &&primPart,
1007 Sdf_PathPropNodeHandle &&propPart)
1008 : _primPart(
std::move(primPart))
1009 , _propPart(
std::move(propPart)) {}
1012 SdfPath(Sdf_PathPrimNodeHandle
const &primPart,
1013 Sdf_PathPropNodeHandle
const &propPart)
1014 : _primPart(primPart)
1015 , _propPart(propPart) {}
1018 SdfPath(Sdf_PathNode
const *primPart,
1019 Sdf_PathNode
const *propPart)
1020 : _primPart(primPart)
1021 , _propPart(propPart) {}
1023 friend class Sdf_PathNode;
1024 friend class Sdfext_PathAccess;
1029 _ElementsToString(
bool absolute,
const std::vector<std::string> &elements);
1032 SdfPath const &newPrefix)
const;
1035 SdfPath const &newPrefix)
const;
1039 bool fixTargetPaths)
const;
1045 inline uint64_t _AsInt()
const {
1046 static_assert(
sizeof(*this) ==
sizeof(uint64_t),
"");
1048 std::memcpy(&ret,
this,
sizeof(*
this));
1053 lhs._primPart.swap(rhs._primPart);
1054 lhs._propPart.swap(rhs._propPart);
1057 SDF_API
friend char const *
1058 Sdf_PathGetDebuggerPathText(
SdfPath const &);
1060 Sdf_PathPrimNodeHandle _primPart;
1061 Sdf_PathPropNodeHandle _propPart;
1089 const SdfPath& GetPath()
const {
return _path; }
1092 using iterator_category = std::forward_iterator_tag;
1094 using difference_type = std::ptrdiff_t;
1095 using reference =
const SdfPath&;
1096 using pointer =
const SdfPath*;
1098 iterator(
const SdfPath& path) : _path(path) {}
1100 iterator() =
default;
1103 iterator& operator++();
1105 const SdfPath& operator*()
const {
return _path; }
1107 const SdfPath* operator->()
const {
return &_path; }
1109 bool operator==(
const iterator& o)
const {
return _path == o._path; }
1111 bool operator!=(
const iterator& o)
const {
return _path != o._path; }
1116 SDF_API
friend difference_type
1117 distance(
const iterator& first,
const iterator& last);
1123 iterator begin()
const {
return iterator(_path); }
1125 iterator end()
const {
return iterator(); }
1133inline size_t hash_value(
SdfPath const &path)
1135 return path.GetHash();
1143struct Sdf_PathIdentity {
1155template <
class ForwardIterator,
class GetPathFn = Sdf_PathIdentity>
1156std::pair<ForwardIterator, ForwardIterator>
1157SdfPathFindPrefixedRange(ForwardIterator begin, ForwardIterator end,
1159 GetPathFn
const &getPath = GetPathFn()) {
1161 typename std::iterator_traits<ForwardIterator>::reference;
1164 Compare(GetPathFn
const &getPath) : _getPath(getPath) {}
1165 GetPathFn
const &_getPath;
1166 bool operator()(IterRef a,
SdfPath const &b)
const {
1167 return _getPath(a) < b;
1171 std::pair<ForwardIterator, ForwardIterator> result;
1174 result.first = std::lower_bound(begin, end, prefix, Compare(getPath));
1178 result.second = TfFindBoundary(result.first, end,
1179 [&prefix, &getPath](IterRef iterRef) {
1180 return getPath(iterRef).HasPrefix(prefix);
1186template <
class RandomAccessIterator,
class GetPathFn>
1188Sdf_PathFindLongestPrefixImpl(RandomAccessIterator begin,
1189 RandomAccessIterator end,
1192 GetPathFn
const &getPath)
1195 typename std::iterator_traits<RandomAccessIterator>::reference;
1198 Compare(GetPathFn
const &getPath) : _getPath(getPath) {}
1199 GetPathFn
const &_getPath;
1200 bool operator()(IterRef a,
SdfPath const &b)
const {
1201 return _getPath(a) < b;
1214 Compare comp(getPath);
1217 RandomAccessIterator result = std::lower_bound(begin, end, path, comp);
1221 if (!strictPrefix && result != end && getPath(*result) == path) {
1227 if (result == begin) {
1232 if (path.
HasPrefix(getPath(*--result))) {
1242 result = std::lower_bound(begin, end, newPath, comp);
1244 if (result != end && getPath(*result) == newPath) {
1247 if (result == begin) {
1250 if (newPath.
HasPrefix(getPath(*--result))) {
1264template <
class RandomAccessIterator,
class GetPathFn = Sdf_PathIdentity,
1265 class =
typename std::enable_if<
1267 std::random_access_iterator_tag,
1268 typename std::iterator_traits<
1269 RandomAccessIterator>::iterator_category
1274SdfPathFindLongestPrefix(RandomAccessIterator begin,
1275 RandomAccessIterator end,
1277 GetPathFn
const &getPath = GetPathFn())
1279 return Sdf_PathFindLongestPrefixImpl(
1280 begin, end, path,
false, getPath);
1290template <
class RandomAccessIterator,
class GetPathFn = Sdf_PathIdentity,
1291 class =
typename std::enable_if<
1293 std::random_access_iterator_tag,
1294 typename std::iterator_traits<
1295 RandomAccessIterator>::iterator_category
1300SdfPathFindLongestStrictPrefix(RandomAccessIterator begin,
1301 RandomAccessIterator end,
1303 GetPathFn
const &getPath = GetPathFn())
1305 return Sdf_PathFindLongestPrefixImpl(
1306 begin, end, path,
true, getPath);
1309template <
class Iter,
class MapParam,
class GetPathFn = Sdf_PathIdentity>
1311Sdf_PathFindLongestPrefixImpl(
1312 MapParam map,
SdfPath const &path,
bool strictPrefix,
1313 GetPathFn
const &getPath = GetPathFn())
1320 const Iter mapEnd = map.end();
1327 Iter result = map.lower_bound(path);
1331 if (!strictPrefix && result != mapEnd && getPath(*result) == path)
1336 if (result == map.begin())
1347 return Sdf_PathFindLongestPrefixImpl<Iter, MapParam>(
1356typename std::set<SdfPath>::const_iterator
1357SdfPathFindLongestPrefix(std::set<SdfPath>
const &set,
SdfPath const &path);
1363typename std::map<SdfPath, T>::const_iterator
1364SdfPathFindLongestPrefix(std::map<SdfPath, T>
const &map,
SdfPath const &path)
1366 return Sdf_PathFindLongestPrefixImpl<
1367 typename std::map<SdfPath, T>::const_iterator,
1368 std::map<SdfPath, T>
const &>(map, path,
false,
1372typename std::map<SdfPath, T>::iterator
1373SdfPathFindLongestPrefix(std::map<SdfPath, T> &map,
SdfPath const &path)
1375 return Sdf_PathFindLongestPrefixImpl<
1376 typename std::map<SdfPath, T>::iterator,
1377 std::map<SdfPath, T> &>(map, path,
false,
1385typename std::set<SdfPath>::const_iterator
1386SdfPathFindLongestStrictPrefix(std::set<SdfPath>
const &set,
1393typename std::map<SdfPath, T>::const_iterator
1394SdfPathFindLongestStrictPrefix(
1395 std::map<SdfPath, T>
const &map,
SdfPath const &path)
1397 return Sdf_PathFindLongestPrefixImpl<
1398 typename std::map<SdfPath, T>::const_iterator,
1399 std::map<SdfPath, T>
const &>(map, path,
true,
1403typename std::map<SdfPath, T>::iterator
1404SdfPathFindLongestStrictPrefix(
1405 std::map<SdfPath, T> &map,
SdfPath const &path)
1407 return Sdf_PathFindLongestPrefixImpl<
1408 typename std::map<SdfPath, T>::iterator,
1409 std::map<SdfPath, T> &>(map, path,
true,
1420Sdf_PathGetDebuggerPathText(
SdfPath const &);
1422PXR_NAMESPACE_CLOSE_SCOPE
1427#include "pxr/usd/sdf/pathNode.h"
1429PXR_NAMESPACE_OPEN_SCOPE
1431static_assert(Sdf_SizeofPrimPathNode ==
sizeof(Sdf_PrimPathNode),
"");
1432static_assert(Sdf_SizeofPropPathNode ==
sizeof(Sdf_PrimPropertyPathNode),
"");
1434PXR_NAMESPACE_CLOSE_SCOPE
Range representing a path and ancestors, and providing methods for iterating over them.
A path value used to locate objects in layers or scenegraphs.
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.
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()).
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.
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.
bool operator>=(const SdfPath &rhs) const
Greater than or equal operator.
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.
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 bool IsAbsoluteRootPath() const
Return true if this path is the AbsoluteRootPath().
bool operator==(const SdfPath &rhs) const
Equality operator.
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.
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.
A user-extensible hashing mechanism for use with runtime hash tables.
Token for efficient comparison, assignment, and hashing of known strings.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
std::vector< TfToken > TfTokenVector
Convenience types.