HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primTypeInfo.h
Go to the documentation of this file.
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"
13 #include "pxr/base/tf/token.h"
14 #include "pxr/base/tf/hash.h"
15 
16 #include <atomic>
17 
19 
20 /// Class that holds the full type information for a prim. It holds the type
21 /// name, applied API schema names, and possibly a mapped schema type name which
22 /// represent a unique full type.
23 /// The info this holds is used to cache and provide the "real" schema type for
24 /// the prim's type name regardless of whether it is a recognized prim type or
25 /// not. The optional "mapped schema type name" is used to obtain a valid schema
26 /// type for an unrecognized prim type name if the stage provides a fallback
27 /// type for the unrecognized type. This class also provides access to the prim
28 /// definition that defines all the built-in properties and metadata of a prim
29 /// of this type.
31 {
32 public:
33  /// Returns the concrete prim type name.
34  const TfToken &GetTypeName() const { return _typeId.primTypeName; }
35 
36  /// Returns the list of applied API schemas, directly authored on the prim,
37  /// that impart additional properties on its prim definition. This does NOT
38  /// include the applied API schemas that may be defined in the conrete prim
39  /// type's prim definition..
41  return _typeId.appliedAPISchemas;
42  }
43 
44  /// Returns the TfType of the actual concrete schema that prims of this
45  /// type will use to create their prim definition. Typically, this will
46  /// be the type registered in the schema registry for the concrete prim type
47  /// returned by GetTypeName. But if the stage provided this type info with
48  /// a fallback type because the prim type name is not a recognized schema,
49  /// this will return the provided fallback schema type instead.
50  ///
51  /// \sa \ref Usd_OM_FallbackPrimTypes
52  const TfType &GetSchemaType() const { return _schemaType; }
53 
54  /// Returns the type name associated with the schema type returned from
55  /// GetSchemaType. This will always be equivalent to calling
56  /// UsdSchemaRegistry::GetConcreteSchemaTypeName on the type returned by
57  /// GetSchemaType and will typically be the same as GetTypeName as long as
58  /// the prim type name is a recognized prim type.
59  ///
60  /// \sa \ref Usd_OM_FallbackPrimTypes
61  const TfToken &GetSchemaTypeName() const { return _schemaTypeName; }
62 
63  /// Returns the prim definition associated with this prim type's schema
64  /// type and applied API schemas.
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 
87  /// Returns the empty prim type info.
88  USD_API
89  static const UsdPrimTypeInfo &GetEmptyPrimType();
90 
91 private:
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 
186 
187 #endif //PXR_USD_USD_PRIM_TYPE_INFO_H
void TfHashAppend(HashState &h, const T &ptr)
Definition: anyWeakPtr.h:220
#define USD_API
Definition: api.h:23
bool operator!=(const UsdPrimTypeInfo &other) const
Definition: primTypeInfo.h:83
const TfToken & GetSchemaTypeName() const
Definition: primTypeInfo.h:61
STATIC_INLINE size_t Hash(const char *s, size_t len)
Definition: farmhash.h:2099
const UsdPrimDefinition & GetPrimDefinition() const
Definition: primTypeInfo.h:65
Definition: hash.h:472
Definition: token.h:70
friend class Usd_PrimTypeInfoCache
Definition: primTypeInfo.h:96
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
const TfTokenVector & GetAppliedAPISchemas() const
Definition: primTypeInfo.h:40
bool operator==(const UsdPrimTypeInfo &other) const
Definition: primTypeInfo.h:77
const TfToken & GetTypeName() const
Returns the concrete prim type name.
Definition: primTypeInfo.h:34
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47
static USD_API const UsdPrimTypeInfo & GetEmptyPrimType()
Returns the empty prim type info.
const TfType & GetSchemaType() const
Definition: primTypeInfo.h:52