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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_USD_PRIM_TYPE_INFO_H
25 #define PXR_USD_USD_PRIM_TYPE_INFO_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
30 #include "pxr/base/tf/token.h"
31 #include "pxr/base/tf/hash.h"
32 
33 #include <atomic>
34 
36 
37 /// Class that holds the full type information for a prim. It holds the type
38 /// name, applied API schema names, and possibly a mapped schema type name which
39 /// represent a unique full type.
40 /// The info this holds is used to cache and provide the "real" schema type for
41 /// the prim's type name regardless of whether it is a recognized prim type or
42 /// not. The optional "mapped schema type name" is used to obtain a valid schema
43 /// type for an unrecognized prim type name if the stage provides a fallback
44 /// type for the unrecognized type. This class also provides access to the prim
45 /// definition that defines all the built-in properties and metadata of a prim
46 /// of this type.
48 {
49 public:
50  /// Returns the concrete prim type name.
51  const TfToken &GetTypeName() const { return _typeId.primTypeName; }
52 
53  /// Returns the list of applied API schemas, directly authored on the prim,
54  /// that impart additional properties on its prim definition. This does NOT
55  /// include the applied API schemas that may be defined in the conrete prim
56  /// type's prim definition..
58  return _typeId.appliedAPISchemas;
59  }
60 
61  /// Returns the TfType of the actual concrete schema that prims of this
62  /// type will use to create their prim definition. Typically, this will
63  /// be the type registered in the schema registry for the concrete prim type
64  /// returned by GetTypeName. But if the stage provided this type info with
65  /// a fallback type because the prim type name is not a recognized schema,
66  /// this will return the provided fallback schema type instead.
67  ///
68  /// \sa \ref Usd_OM_FallbackPrimTypes
69  const TfType &GetSchemaType() const { return _schemaType; }
70 
71  /// Returns the type name associated with the schema type returned from
72  /// GetSchemaType. This will always be equivalent to calling
73  /// UsdSchemaRegistry::GetConcreteSchemaTypeName on the type returned by
74  /// GetSchemaType and will typically be the same as GetTypeName as long as
75  /// the prim type name is a recognized prim type.
76  ///
77  /// \sa \ref Usd_OM_FallbackPrimTypes
78  const TfToken &GetSchemaTypeName() const { return _schemaTypeName; }
79 
80  /// Returns the prim definition associated with this prim type's schema
81  /// type and applied API schemas.
83  // First check if we've already cached the prim definition pointer;
84  // we can just return it. Note that we use memory_order_acquire for
85  // the case wher _FindOrCreatePrimDefinition needs to build its own
86  // prim definition.
87  if (const UsdPrimDefinition *primDef =
88  _primDefinition.load(std::memory_order_acquire)) {
89  return *primDef;
90  }
91  return *_FindOrCreatePrimDefinition();
92  }
93 
94  bool operator==(const UsdPrimTypeInfo &other) const {
95  // Only need to compare typeId as a typeId is expected to always produce
96  // the same schema type and prim definition.
97  return _typeId == other._typeId;
98  }
99 
100  bool operator!=(const UsdPrimTypeInfo &other) const {
101  return !(*this == other);
102  }
103 
104  /// Returns the empty prim type info.
105  USD_API
106  static const UsdPrimTypeInfo &GetEmptyPrimType();
107 
108 private:
109  // Only the PrimTypeInfoCache can create the PrimTypeInfo prims.
110  // These are cached, one for each unique, prim type/applied schema list
111  // encountered. This provides the PrimData with lazy access to the unique
112  // prim definition for this exact prim type in a thread safe way.
113  friend class Usd_PrimTypeInfoCache;
114 
115  // This struct holds the information used to uniquely identify the type of a
116  // UsdPrimTypeInfo and can be used to key each prim type info in the type
117  // info cache.
118  struct _TypeId
119  {
120  // Authored type name of the prim.
121  TfToken primTypeName;
122 
123  // Optional type name that the type name should be mapped to instead.
124  // Will be used typically to provide a fallback schema type for an
125  // unrecognized prim type name.
126  TfToken mappedTypeName;
127 
128  // The list of applied API schemas authored on the prim.
129  TfTokenVector appliedAPISchemas;
130 
131  _TypeId() = default;
132 
133  // Have both move and copy constructors to minimize the number vector
134  // copies when possible.
135  _TypeId(const _TypeId &typeId) = default;
136  _TypeId(_TypeId &&typeId) = default;
137 
138  // Explicit constructor from just a prim type name.
139  explicit _TypeId(const TfToken &primTypeName_)
140  : primTypeName(primTypeName_) {}
141 
142  // Is empty type
143  bool IsEmpty() const {
144  return primTypeName.IsEmpty() &&
145  mappedTypeName.IsEmpty() &&
146  appliedAPISchemas.empty();
147  }
148 
149  // Hash function for hash map keying.
150  template <class HashState>
151  friend void TfHashAppend(HashState &h, const _TypeId &id)
152  {
153  h.Append(id.primTypeName, id.mappedTypeName, id.appliedAPISchemas);
154  }
155 
156  size_t Hash() const {
157  return TfHash()(*this);
158  }
159 
160  bool operator==(const _TypeId &other) const {
161  return primTypeName == other.primTypeName &&
162  mappedTypeName == other.mappedTypeName &&
163  appliedAPISchemas == other.appliedAPISchemas;
164  }
165 
166  bool operator!=(const _TypeId &other) const {
167  return !(*this == other);
168  }
169  };
170 
171  // Default constructor. Empty type.
172  UsdPrimTypeInfo() : _primDefinition(nullptr) {}
173 
174  // Move constructor from a _TypeId.
175  UsdPrimTypeInfo(_TypeId &&typeId);
176 
177  // Returns the full type ID.
178  const _TypeId &_GetTypeId() const { return _typeId; }
179 
180  // Finds the prim definition, creating it if it doesn't already exist. This
181  // cache access must be thread safe.
182  USD_API
183  const UsdPrimDefinition *_FindOrCreatePrimDefinition() const;
184 
185  _TypeId _typeId;
186  TfType _schemaType;
187  TfToken _schemaTypeName;
188 
189  // Cached pointer to the prim definition.
190  mutable std::atomic<const UsdPrimDefinition *> _primDefinition;
191 
192  // When there are applied API schemas, _FindOrCreatePrimDefinition will
193  // build a custom prim definition that it will own for its lifetime. This
194  // is here to make sure it is explicit when the prim type info owns the
195  // prim definition.
196  // Note that we will always return the prim definition via the atomic
197  // _primDefinition pointer regardless of whether the _ownedPrimDefinition
198  // is set.
199  mutable std::unique_ptr<UsdPrimDefinition> _ownedPrimDefinition;
200 };
201 
203 
204 #endif //PXR_USD_USD_PRIM_TYPE_INFO_H
void TfHashAppend(HashState &h, const T &ptr)
Definition: anyWeakPtr.h:237
#define USD_API
Definition: api.h:40
bool operator!=(const UsdPrimTypeInfo &other) const
Definition: primTypeInfo.h:100
const TfToken & GetSchemaTypeName() const
Definition: primTypeInfo.h:78
STATIC_INLINE size_t Hash(const char *s, size_t len)
Definition: farmhash.h:2038
const UsdPrimDefinition & GetPrimDefinition() const
Definition: primTypeInfo.h:82
Definition: hash.h:504
Definition: token.h:87
friend class Usd_PrimTypeInfoCache
Definition: primTypeInfo.h:113
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
const TfTokenVector & GetAppliedAPISchemas() const
Definition: primTypeInfo.h:57
bool operator==(const UsdPrimTypeInfo &other) const
Definition: primTypeInfo.h:94
const TfToken & GetTypeName() const
Returns the concrete prim type name.
Definition: primTypeInfo.h:51
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: type.h:64
static USD_API const UsdPrimTypeInfo & GetEmptyPrimType()
Returns the empty prim type info.
const TfType & GetSchemaType() const
Definition: primTypeInfo.h:69