All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
primTypeInfo.h
1//
2// Copyright 2020 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_USD_PRIM_TYPE_INFO_H
8#define PXR_USD_USD_PRIM_TYPE_INFO_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/usd/api.h"
12#include "pxr/usd/usd/primDefinition.h"
13#include "pxr/base/tf/token.h"
14#include "pxr/base/tf/hash.h"
15
16#include <atomic>
17
18PXR_NAMESPACE_OPEN_SCOPE
19
31{
32public:
34 const TfToken &GetTypeName() const { return _typeId.primTypeName; }
35
41 return _typeId.appliedAPISchemas;
42 }
43
52 const TfType &GetSchemaType() const { return _schemaType; }
53
61 const TfToken &GetSchemaTypeName() const { return _schemaTypeName; }
62
66 // First check if we've already cached the prim definition pointer;
67 // we can just return it. Note that we use memory_order_acquire for
68 // the case wher _FindOrCreatePrimDefinition needs to build its own
69 // prim definition.
70 if (const UsdPrimDefinition *primDef =
71 _primDefinition.load(std::memory_order_acquire)) {
72 return *primDef;
73 }
74 return *_FindOrCreatePrimDefinition();
75 }
76
77 bool operator==(const UsdPrimTypeInfo &other) const {
78 // Only need to compare typeId as a typeId is expected to always produce
79 // the same schema type and prim definition.
80 return _typeId == other._typeId;
81 }
82
83 bool operator!=(const UsdPrimTypeInfo &other) const {
84 return !(*this == other);
85 }
86
88 USD_API
90
91private:
92 // Only the PrimTypeInfoCache can create the PrimTypeInfo prims.
93 // These are cached, one for each unique, prim type/applied schema list
94 // encountered. This provides the PrimData with lazy access to the unique
95 // prim definition for this exact prim type in a thread safe way.
96 friend class Usd_PrimTypeInfoCache;
97
98 // This struct holds the information used to uniquely identify the type of a
99 // UsdPrimTypeInfo and can be used to key each prim type info in the type
100 // info cache.
101 struct _TypeId
102 {
103 // Authored type name of the prim.
104 TfToken primTypeName;
105
106 // Optional type name that the type name should be mapped to instead.
107 // Will be used typically to provide a fallback schema type for an
108 // unrecognized prim type name.
109 TfToken mappedTypeName;
110
111 // The list of applied API schemas authored on the prim.
112 TfTokenVector appliedAPISchemas;
113
114 _TypeId() = default;
115
116 // Have both move and copy constructors to minimize the number vector
117 // copies when possible.
118 _TypeId(const _TypeId &typeId) = default;
119 _TypeId(_TypeId &&typeId) = default;
120
121 // Explicit constructor from just a prim type name.
122 explicit _TypeId(const TfToken &primTypeName_)
123 : primTypeName(primTypeName_) {}
124
125 // Is empty type
126 bool IsEmpty() const {
127 return primTypeName.IsEmpty() &&
128 mappedTypeName.IsEmpty() &&
129 appliedAPISchemas.empty();
130 }
131
132 // Hash function for hash map keying.
133 template <class HashState>
134 friend void TfHashAppend(HashState &h, const _TypeId &id)
135 {
136 h.Append(id.primTypeName, id.mappedTypeName, id.appliedAPISchemas);
137 }
138
139 size_t Hash() const {
140 return TfHash()(*this);
141 }
142
143 bool operator==(const _TypeId &other) const {
144 return primTypeName == other.primTypeName &&
145 mappedTypeName == other.mappedTypeName &&
146 appliedAPISchemas == other.appliedAPISchemas;
147 }
148
149 bool operator!=(const _TypeId &other) const {
150 return !(*this == other);
151 }
152 };
153
154 // Default constructor. Empty type.
155 UsdPrimTypeInfo() : _primDefinition(nullptr) {}
156
157 // Move constructor from a _TypeId.
158 UsdPrimTypeInfo(_TypeId &&typeId);
159
160 // Returns the full type ID.
161 const _TypeId &_GetTypeId() const { return _typeId; }
162
163 // Finds the prim definition, creating it if it doesn't already exist. This
164 // cache access must be thread safe.
165 USD_API
166 const UsdPrimDefinition *_FindOrCreatePrimDefinition() const;
167
168 _TypeId _typeId;
169 TfType _schemaType;
170 TfToken _schemaTypeName;
171
172 // Cached pointer to the prim definition.
173 mutable std::atomic<const UsdPrimDefinition *> _primDefinition;
174
175 // When there are applied API schemas, _FindOrCreatePrimDefinition will
176 // build a custom prim definition that it will own for its lifetime. This
177 // is here to make sure it is explicit when the prim type info owns the
178 // prim definition.
179 // Note that we will always return the prim definition via the atomic
180 // _primDefinition pointer regardless of whether the _ownedPrimDefinition
181 // is set.
182 mutable std::unique_ptr<UsdPrimDefinition> _ownedPrimDefinition;
183};
184
185PXR_NAMESPACE_CLOSE_SCOPE
186
187#endif //PXR_USD_USD_PRIM_TYPE_INFO_H
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:460
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition: token.h:288
TfType represents a dynamic runtime type.
Definition: type.h:48
Class representing the builtin definition of a prim given the schemas registered in the schema regist...
Class that holds the full type information for a prim.
Definition: primTypeInfo.h:31
const UsdPrimDefinition & GetPrimDefinition() const
Returns the prim definition associated with this prim type's schema type and applied API schemas.
Definition: primTypeInfo.h:65
static USD_API const UsdPrimTypeInfo & GetEmptyPrimType()
Returns the empty prim type info.
const TfType & GetSchemaType() const
Returns the TfType of the actual concrete schema that prims of this type will use to create their pri...
Definition: primTypeInfo.h:52
const TfToken & GetTypeName() const
Returns the concrete prim type name.
Definition: primTypeInfo.h:34
const TfTokenVector & GetAppliedAPISchemas() const
Returns the list of applied API schemas, directly authored on the prim, that impart additional proper...
Definition: primTypeInfo.h:40
const TfToken & GetSchemaTypeName() const
Returns the type name associated with the schema type returned from GetSchemaType.
Definition: primTypeInfo.h:61
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