All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
schemaRegistry.h
1 //
2 // Copyright 2016 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 USD_SCHEMAREGISTRY_H
25 #define USD_SCHEMAREGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 
30 #include "pxr/usd/sdf/layer.h"
31 #include "pxr/usd/sdf/primSpec.h"
32 
33 #include "pxr/base/tf/hash.h"
34 #include "pxr/base/tf/singleton.h"
35 #include "pxr/base/tf/hashmap.h"
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 SDF_DECLARE_HANDLES(SdfAttributeSpec);
40 SDF_DECLARE_HANDLES(SdfRelationshipSpec);
41 
56 class UsdSchemaRegistry : public TfSingleton<UsdSchemaRegistry> {
57 public:
58  USD_API
59  static UsdSchemaRegistry& GetInstance() {
61  }
62 
63  static const SdfLayerRefPtr & GetSchematics() {
64  return GetInstance()._schematics;
65  }
66 
70  USD_API
71  static SdfPrimSpecHandle GetPrimDefinition(const TfToken &primType);
72 
76  USD_API
77  static SdfPrimSpecHandle GetPrimDefinition(const TfType &primType);
78 
82  template <class SchemaType>
83  static SdfPrimSpecHandle GetPrimDefinition() {
84  return GetPrimDefinition(SchemaType::_GetStaticTfType());
85  }
86 
90  USD_API
91  static SdfPropertySpecHandle
92  GetPropertyDefinition(const TfToken& primType,
93  const TfToken& propName);
94 
98  USD_API
99  static SdfAttributeSpecHandle
100  GetAttributeDefinition(const TfToken& primType,
101  const TfToken& attrName);
102 
106  USD_API
107  static SdfRelationshipSpecHandle
108  GetRelationshipDefinition(const TfToken& primType, const TfToken& relName);
109 
112  static SdfSpecType GetSpecType(const TfToken &primType,
113  const TfToken &propName) {
114  const UsdSchemaRegistry &self = GetInstance();
115  const SdfPath &path = self._GetPath(primType, propName);
116  return path.IsEmpty() ? SdfSpecTypeUnknown :
117  self._schematics->GetSpecType(path);
118  }
119 
123  // XXX: Getting these fields via the methods that return spec
124  // handles will be slower than using this method. It's
125  // questionable if those methods should exist at all.
126  template <class T>
127  static bool HasField(const TfToken& primType,
128  const TfToken& propName,
129  const TfToken& fieldName, T* value)
130  {
131  const UsdSchemaRegistry &self = GetInstance();
132  const SdfPath &path = self._GetPath(primType, propName);
133  return !path.IsEmpty() &&
134  self._schematics->HasField(path, fieldName, value);
135  }
136 
137  template <class T>
138  static bool HasFieldDictKey(const TfToken& primType,
139  const TfToken& propName,
140  const TfToken& fieldName,
141  const TfToken& keyPath,
142  T* value)
143  {
144  const UsdSchemaRegistry &self = GetInstance();
145  const SdfPath &path = self._GetPath(primType, propName);
146  return !path.IsEmpty() &&
147  self._schematics->HasFieldDictKey(path, fieldName, keyPath, value);
148  }
149 
157  USD_API
158  static std::vector<TfToken> GetDisallowedFields();
159 
161  USD_API
162  static bool IsTyped(const TfType& primType);
163 
166  USD_API
167  static bool IsConcrete(const TfType& primType);
168 
170  USD_API
171  bool IsAppliedAPISchema(const TfType& apiSchemaType);
172 
174  USD_API
175  bool IsMultipleApplyAPISchema(const TfType& apiSchemaType);
176 
177 
200  USD_API
201  static TfType GetTypeFromName(const TfToken& typeName);
202 
203 private:
204  friend class TfSingleton<UsdSchemaRegistry>;
205 
207 
208  // Helper for template GetPrimDefinition.
209  static SdfPrimSpecHandle
210  _GetPrimDefinitionAtPath(const SdfPath &path);
211 
212  // Helper for looking up the prim definition path for a given primType.
213  const SdfPath& _GetSchemaPrimPath(const TfToken &primType) const;
214 
215  // Helper for looking up the prim definition path for a given primType.
216  const SdfPath& _GetSchemaPrimPath(const TfType &primType) const;
217 
218  USD_API
219  const SdfPath &_GetPath(const TfToken &primType,
220  const TfToken &propName) const;
221 
222  void _FindAndAddPluginSchema();
223 
224  void _BuildPrimTypePropNameToPathMap(const TfToken &typeName,
225  const SdfPath &primPath);
226 
227  SdfLayerRefPtr _schematics;
228 
229  // Registered map of schema class type -> definition prim path.
230  // XXX: Should drop this in favor of _TypeNameToPathMap but
231  // TfType should have a GetTypeNameToken() method so we
232  // don't have to construct a TfToken from a std::string.
233  typedef TfHashMap<TfType, SdfPath, TfHash> _TypeToPathMap;
234  _TypeToPathMap _typeToPathMap;
235 
236  typedef TfHashMap<TfToken, SdfPath, TfToken::HashFunctor>
237  _TypeNameToPathMap;
238  _TypeNameToPathMap _typeNameToPathMap;
239 
240  struct _TokenPairHash {
241  inline size_t operator()(const std::pair<TfToken, TfToken> &p) const {
242  size_t hash = p.first.Hash();
243  boost::hash_combine(hash, p.second);
244  return hash;
245  }
246  };
247 
248  // Cache of primType/propName to specId.
249  typedef TfHashMap<std::pair<TfToken, TfToken>, SdfPath,
250  _TokenPairHash> _PrimTypePropNameToPathMap;
251  _PrimTypePropNameToPathMap _primTypePropNameToPathMap;
252 
253  TfToken::HashSet _appliedAPISchemaNames;
254  TfToken::HashSet _multipleApplyAPISchemaNames;
255 };
256 
257 USD_API_TEMPLATE_CLASS(TfSingleton<UsdSchemaRegistry>);
258 
259 PXR_NAMESPACE_CLOSE_SCOPE
260 
261 #endif //USD_SCHEMAREGISTRY_H
static USD_API SdfAttributeSpecHandle GetAttributeDefinition(const TfToken &primType, const TfToken &attrName)
This is a convenience method.
static SdfSpecType GetSpecType(const TfToken &primType, const TfToken &propName)
Return the SdfSpecType for primType and propName if those identify a builtin property.
Manage a single instance of an object (see.
Definition: singleton.h:122
static USD_API SdfPropertySpecHandle GetPropertyDefinition(const TfToken &primType, const TfToken &propName)
Return the property spec that defines the fallback for the property named propName on prims of type p...
USD_API bool IsAppliedAPISchema(const TfType &apiSchemaType)
Returns true if apiSchemaType is an applied API schema type.
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:91
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
A subclass of SdfPropertySpec that holds typed data.
Definition: attributeSpec.h:56
static bool HasField(const TfToken &primType, const TfToken &propName, const TfToken &fieldName, T *value)
Return in value the field for the property named propName under the prim for type primType or for the...
static SdfPrimSpecHandle GetPrimDefinition()
Return the PrimSpec that contains all the builtin metadata and properties for the given SchemaType...
USD_API bool IsMultipleApplyAPISchema(const TfType &apiSchemaType)
Returns true if apiSchemaType is a multiple-apply API schema type.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:137
A property that contains a reference to one or more SdfPrimSpec instances.
static USD_API bool IsConcrete(const TfType &primType)
Returns true if the prim type primType is instantiable in scene description.
static USD_API SdfRelationshipSpecHandle GetRelationshipDefinition(const TfToken &primType, const TfToken &relName)
This is a convenience method.
TfHashSet< TfToken, TfToken::HashFunctor > HashSet
Predefined type for TfHashSet of tokens, since it&#39;s so awkward to manually specify.
Definition: token.h:177
static USD_API bool IsTyped(const TfType &primType)
Returns true if the prim type primType inherits from UsdTyped.
static USD_API TfType GetTypeFromName(const TfToken &typeName)
Finds the TfType of a schema with typeName.
TfType represents a dynamic runtime type.
Definition: type.h:70
Singleton registry that provides access to prim and property definition information for registered Us...
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:409
static USD_API std::vector< TfToken > GetDisallowedFields()
Returns list of fields that cannot have fallback values specified in schemas.