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 PXR_USD_USD_SCHEMA_REGISTRY_H
25 #define PXR_USD_USD_SCHEMA_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 #include "pxr/usd/usd/common.h"
30 
31 #include "pxr/usd/sdf/layer.h"
32 #include "pxr/usd/sdf/primSpec.h"
33 
34 #include "pxr/base/tf/hash.h"
35 #include "pxr/base/tf/singleton.h"
36 
37 #include <unordered_map>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 SDF_DECLARE_HANDLES(SdfAttributeSpec);
42 SDF_DECLARE_HANDLES(SdfRelationshipSpec);
43 
44 class UsdPrimDefinition;
45 
65 class UsdSchemaRegistry : public TfWeakBase, boost::noncopyable {
66 public:
67  USD_API
68  static UsdSchemaRegistry& GetInstance() {
70  }
71 
74  USD_API
75  static TfToken GetSchemaTypeName(const TfType &schemaType);
76 
79  template <class SchemaType>
80  static
82  return GetSchemaTypeName(SchemaType::_GetStaticTfType());
83  }
84 
87  USD_API
88  static TfToken GetConcreteSchemaTypeName(const TfType &schemaType);
89 
92  USD_API
93  static TfToken GetAPISchemaTypeName(const TfType &schemaType);
94 
97  USD_API
98  static TfType GetTypeFromSchemaTypeName(const TfToken &typeName);
99 
102  USD_API
103  static TfType GetConcreteTypeFromSchemaTypeName(const TfToken &typeName);
104 
107  USD_API
108  static TfType GetAPITypeFromSchemaTypeName(const TfToken &typeName);
109 
117  USD_API
118  static bool IsDisallowedField(const TfToken &fieldName);
119 
121  USD_API
122  static bool IsTyped(const TfType& primType);
123 
129  USD_API
130  static UsdSchemaKind GetSchemaKind(const TfType &schemaType);
131 
137  USD_API
138  static UsdSchemaKind GetSchemaKind(const TfToken &typeName);
139 
142  USD_API
143  static bool IsConcrete(const TfType& primType);
144 
147  USD_API
148  static bool IsConcrete(const TfToken& primType);
149 
151  USD_API
152  static bool IsAppliedAPISchema(const TfType& apiSchemaType);
153 
155  USD_API
156  static bool IsAppliedAPISchema(const TfToken& apiSchemaType);
157 
159  USD_API
160  static bool IsMultipleApplyAPISchema(const TfType& apiSchemaType);
161 
163  USD_API
164  static bool IsMultipleApplyAPISchema(const TfToken& apiSchemaType);
165 
188  USD_API
189  static TfType GetTypeFromName(const TfToken& typeName);
190 
211  USD_API
212  static std::pair<TfToken, TfToken> GetTypeNameAndInstance(
213  const TfToken &apiSchemaName);
214 
224  USD_API
225  static bool IsAllowedAPISchemaInstanceName(
226  const TfToken &apiSchemaName,
227  const TfToken &instanceName);
228 
241  USD_API
243  const TfToken &apiSchemaName,
244  const TfToken &instanceName = TfToken());
245 
254  USD_API
255  static const std::map<TfToken, TfTokenVector> &GetAutoApplyAPISchemas();
256 
268  USD_API
270  std::map<TfToken, TfTokenVector> *autoApplyAPISchemas);
271 
274  USD_API
276  const TfToken &multiApplyAPISchemaName) const;
277 
282  const TfToken &typeName) const {
283  const auto it = _concreteTypedPrimDefinitions.find(typeName);
284  return it != _concreteTypedPrimDefinitions.end() ? it->second : nullptr;
285  }
286 
291  const TfToken &typeName) const {
292  // Check the single apply API schemas first then check for multiple
293  // apply schemas. This function will most often be used to find a
294  // single apply schema's prim definition as the prim definitions for
295  // multiple apply schemas aren't generally useful.
296  const auto it = _singleApplyAPIPrimDefinitions.find(typeName);
297  if (it != _singleApplyAPIPrimDefinitions.end()) {
298  return it->second;
299  }
300  const auto multiIt = _multiApplyAPIPrimDefinitions.find(typeName);
301  return multiIt != _multiApplyAPIPrimDefinitions.end() ?
302  multiIt->second.primDef : nullptr;
303  }
304 
307  return _emptyPrimDefinition;
308  }
309 
314  USD_API
315  std::unique_ptr<UsdPrimDefinition>
317  const TfToken &primType, const TfTokenVector &appliedAPISchemas) const;
318 
331  return _fallbackPrimTypes;
332  }
333 
334 private:
335  friend class TfSingleton<UsdSchemaRegistry>;
336 
338 
339  // For the given full API schema name (which may be "type:instance" for
340  // multiple apply API schemas), finds and returns the prim definition for
341  // the API schema type. If the API schema is an instance of a multiple
342  // apply API, the full prefix that will be prepended to all properties
343  // when the schema is applied will be set in propertyPrefix.
344  const UsdPrimDefinition *_FindAPIPrimDefinitionByFullName(
345  const TfToken &apiSchemaName, std::string *propertyPrefix) const;
346 
347  void _ComposeAPISchemasIntoPrimDefinition(
348  UsdPrimDefinition *primDef,
349  const TfTokenVector &appliedAPISchemas) const;
350 
351  // Private class for helping initialize the schema registry. Defined
352  // entirely in the implementation. Declared here for private access to the
353  // registry.
354  class _SchemaDefInitHelper;
355 
356  // Multiple apply API schema definitions want to be stored along with the
357  // their schema's property namespace prefix which is required to correctly
358  // apply them.
359  struct _MultipleApplyAPIDefinition {
360  UsdPrimDefinition *primDef = nullptr;
361  TfToken propertyNamespace;
362  };
363  using _TypeNameToMultipleApplyAPIDefinitionMap = std::unordered_map<
364  TfToken, _MultipleApplyAPIDefinition, TfToken::HashFunctor>;
365 
366  using _TypeNameToPrimDefinitionMap = std::unordered_map<
368 
369  SdfLayerRefPtr _schematics;
370 
371  _TypeNameToPrimDefinitionMap _concreteTypedPrimDefinitions;
372  _TypeNameToPrimDefinitionMap _singleApplyAPIPrimDefinitions;
373  _TypeNameToMultipleApplyAPIDefinitionMap _multiApplyAPIPrimDefinitions;
374  UsdPrimDefinition *_emptyPrimDefinition;
375 
376  VtDictionary _fallbackPrimTypes;
377 
378  friend class UsdPrimDefinition;
379 };
380 
381 USD_API_TEMPLATE_CLASS(TfSingleton<UsdSchemaRegistry>);
382 
383 // Utility function for extracting the metadata about applying API schemas from
384 // the plugin metadata for the schema's type. It is useful for certain clients
385 // to be able to access this plugin data in the same way that the
386 // UsdSchemaRegistry does.
387 void Usd_GetAPISchemaPluginApplyToInfoForType(
388  const TfType &apiSchemaType,
389  const TfToken &apiSchemaName,
390  std::map<TfToken, TfTokenVector> *autoApplyAPISchemasMap,
391  TfHashMap<TfToken, TfTokenVector, TfHash> *canOnlyApplyAPISchemasMap,
392  TfHashMap<TfToken, TfToken::Set, TfHash> *allowedInstanceNamesMap);
393 
394 PXR_NAMESPACE_CLOSE_SCOPE
395 
396 #endif //PXR_USD_USD_SCHEMA_REGISTRY_H
static USD_API bool IsMultipleApplyAPISchema(const TfType &apiSchemaType)
Returns true if apiSchemaType is a multiple-apply API schema type.
Manage a single instance of an object.
static USD_API const TfTokenVector & GetAPISchemaCanOnlyApplyToTypeNames(const TfToken &apiSchemaName, const TfToken &instanceName=TfToken())
Returns a list of prim type names that the given apiSchemaName can only be applied to.
Manage a single instance of an object (see.
Definition: singleton.h:122
static USD_API TfToken GetAPISchemaTypeName(const TfType &schemaType)
Return the type name in the USD schema for API schema types only from the given registered schemaType...
A map with string keys and VtValue values.
Definition: dictionary.h:63
USD_API std::unique_ptr< UsdPrimDefinition > BuildComposedPrimDefinition(const TfToken &primType, const TfTokenVector &appliedAPISchemas) const
Composes and returns a new UsdPrimDefinition from the given primType and list of appliedSchemas.
Functor to use for hash maps from tokens to other things.
Definition: token.h:166
const UsdPrimDefinition * GetEmptyPrimDefinition() const
Returns the empty prim definition.
UsdSchemaKind
An enum representing which kind of schema a given schema class belongs to.
Definition: common.h:127
static USD_API bool IsAppliedAPISchema(const TfType &apiSchemaType)
Returns true if apiSchemaType is an applied API schema type.
static USD_API bool IsAllowedAPISchemaInstanceName(const TfToken &apiSchemaName, const TfToken &instanceName)
Returns true if the given instanceName is an allowed instance name for the multiple apply API schema ...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
A subclass of SdfPropertySpec that holds typed data.
Definition: attributeSpec.h:56
static USD_API std::pair< TfToken, TfToken > GetTypeNameAndInstance(const TfToken &apiSchemaName)
Returns the schema type name and the instance name parsed from the given apiSchemaName.
const VtDictionary & GetFallbackPrimTypes() const
Returns a dictionary mapping concrete schema prim type names to a VtTokenArray of fallback prim type ...
static USD_API UsdSchemaKind GetSchemaKind(const TfType &schemaType)
Returns the kind of the schema the given schemaType represents.
static USD_API void CollectAddtionalAutoApplyAPISchemasFromPlugins(std::map< TfToken, TfTokenVector > *autoApplyAPISchemas)
Collects all the additional auto apply schemas that can be defined in a plugin through "AutoApplyAPIS...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:137
static USD_API TfType GetTypeFromSchemaTypeName(const TfToken &typeName)
Return the TfType of the schema corresponding to the given prim or API schema name typeName.
static USD_API bool IsDisallowedField(const TfToken &fieldName)
Returns true if the field fieldName cannot have fallback values specified in schemas.
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 TfType GetConcreteTypeFromSchemaTypeName(const TfToken &typeName)
Return the TfType of the schema corresponding to the given concrete prim type name typeName.
Class representing the builtin definition of a prim given the schemas registered in the schema regist...
const UsdPrimDefinition * FindConcretePrimDefinition(const TfToken &typeName) const
Finds the prim definition for the given typeName token if typeName is a registered concrete typed sch...
static USD_API TfType GetAPITypeFromSchemaTypeName(const TfToken &typeName)
Return the TfType of the schema corresponding to the given API schema type name typeName.
static TfToken GetSchemaTypeName()
Return the type name in the USD schema for prims or API schemas of the given registered SchemaType.
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:64
static USD_API const std::map< TfToken, TfTokenVector > & GetAutoApplyAPISchemas()
Returns a map of the names of all registered auto apply API schemas to the list of type names each is...
Singleton registry that provides access to schema type information and the prim definitions for regis...
const UsdPrimDefinition * FindAppliedAPIPrimDefinition(const TfToken &typeName) const
Finds the prim definition for the given typeName token if typeName is a registered applied API schema...
static USD_API TfToken GetConcreteSchemaTypeName(const TfType &schemaType)
Return the type name in the USD schema for concrete prim types only from the given registered schemaT...
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
USD_API TfToken GetPropertyNamespacePrefix(const TfToken &multiApplyAPISchemaName) const
Returns the namespace prefix that is prepended to all properties of the given multiApplyAPISchemaName...