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 
47 using UsdSchemaVersion = unsigned int;
48 
68 class UsdSchemaRegistry : public TfWeakBase, boost::noncopyable {
69 public:
72  struct SchemaInfo {
73 
78 
83 
89 
91  UsdSchemaVersion version;
92 
95  };
96 
97  USD_API
98  static UsdSchemaRegistry& GetInstance() {
100  }
101 
114  USD_API
115  static TfToken
117  const TfToken &schemaFamily,
118  UsdSchemaVersion schemaVersion);
119 
137  USD_API
138  static std::pair<TfToken, UsdSchemaVersion>
139  ParseSchemaFamilyAndVersionFromIdentifier(const TfToken &schemaIdentifier);
140 
148  USD_API
149  static bool
150  IsAllowedSchemaFamily(const TfToken &schemaFamily);
151 
161  USD_API
162  static bool
163  IsAllowedSchemaIdentifier(const TfToken &schemaIdentifier);
164 
168  USD_API
169  static const SchemaInfo *
170  FindSchemaInfo(const TfType &schemaType);
171 
175  USD_API
176  static const SchemaInfo *
177  FindSchemaInfo(const TfToken &schemaIdentifier);
178 
182  USD_API
183  static const SchemaInfo *
184  FindSchemaInfo(const TfToken &schemaFamily, UsdSchemaVersion schemaVersion);
185 
188  enum class VersionPolicy {
189  All,
190  GreaterThan,
191  GreaterThanOrEqual,
192  LessThan,
193  LessThanOrEqual
194  };
195 
198  USD_API
199  static const std::vector<const SchemaInfo *> &
201  const TfToken &schemaFamily);
202 
206  USD_API
207  static std::vector<const SchemaInfo *>
209  const TfToken &schemaFamily,
210  UsdSchemaVersion schemaVersion,
211  VersionPolicy versionPolicy);
212 
215  USD_API
216  static TfToken GetSchemaTypeName(const TfType &schemaType);
217 
220  template <class SchemaType>
221  static
223  return GetSchemaTypeName(SchemaType::_GetStaticTfType());
224  }
225 
228  USD_API
229  static TfToken GetConcreteSchemaTypeName(const TfType &schemaType);
230 
233  USD_API
234  static TfToken GetAPISchemaTypeName(const TfType &schemaType);
235 
238  USD_API
239  static TfType GetTypeFromSchemaTypeName(const TfToken &typeName);
240 
243  USD_API
244  static TfType GetConcreteTypeFromSchemaTypeName(const TfToken &typeName);
245 
248  USD_API
249  static TfType GetAPITypeFromSchemaTypeName(const TfToken &typeName);
250 
258  USD_API
259  static bool IsDisallowedField(const TfToken &fieldName);
260 
262  USD_API
263  static bool IsTyped(const TfType& primType);
264 
270  USD_API
271  static UsdSchemaKind GetSchemaKind(const TfType &schemaType);
272 
278  USD_API
279  static UsdSchemaKind GetSchemaKind(const TfToken &typeName);
280 
283  USD_API
284  static bool IsConcrete(const TfType& primType);
285 
288  USD_API
289  static bool IsConcrete(const TfToken& primType);
290 
293  USD_API
294  static bool IsAbstract(const TfType& primType);
295 
298  USD_API
299  static bool IsAbstract(const TfToken& primType);
300 
302  USD_API
303  static bool IsAppliedAPISchema(const TfType& apiSchemaType);
304 
306  USD_API
307  static bool IsAppliedAPISchema(const TfToken& apiSchemaType);
308 
310  USD_API
311  static bool IsMultipleApplyAPISchema(const TfType& apiSchemaType);
312 
314  USD_API
315  static bool IsMultipleApplyAPISchema(const TfToken& apiSchemaType);
316 
339  USD_API
340  static TfType GetTypeFromName(const TfToken& typeName);
341 
362  USD_API
363  static std::pair<TfToken, TfToken> GetTypeNameAndInstance(
364  const TfToken &apiSchemaName);
365 
375  USD_API
376  static bool IsAllowedAPISchemaInstanceName(
377  const TfToken &apiSchemaName,
378  const TfToken &instanceName);
379 
392  USD_API
394  const TfToken &apiSchemaName,
395  const TfToken &instanceName = TfToken());
396 
405  USD_API
406  static const std::map<TfToken, TfTokenVector> &GetAutoApplyAPISchemas();
407 
419  USD_API
421  std::map<TfToken, TfTokenVector> *autoApplyAPISchemas);
422 
440  USD_API
442  const std::string &namespacePrefix,
443  const std::string &baseName);
444 
459  USD_API
461  const std::string &nameTemplate,
462  const std::string &instanceName);
463 
473  USD_API
475  const std::string &nameTemplate);
476 
485  USD_API
486  static bool IsMultipleApplyNameTemplate(
487  const std::string &nameTemplate);
488 
493  const TfToken &typeName) const {
494  const auto it = _concreteTypedPrimDefinitions.find(typeName);
495  return it != _concreteTypedPrimDefinitions.end() ? it->second.get() : nullptr;
496  }
497 
502  const TfToken &typeName) const {
503  // Check the single apply API schemas first then check for multiple
504  // apply schemas. This function will most often be used to find a
505  // single apply schema's prim definition as the prim definitions for
506  // multiple apply schemas aren't generally useful.
507  const auto it = _appliedAPIPrimDefinitions.find(typeName);
508  if (it != _appliedAPIPrimDefinitions.end()) {
509  return it->second.get();
510  }
511  const auto multiIt = _multiApplyAPIPrimDefinitions.find(typeName);
512  return multiIt != _multiApplyAPIPrimDefinitions.end() ?
513  multiIt->second : nullptr;
514  }
515 
518  return _emptyPrimDefinition;
519  }
520 
525  USD_API
526  std::unique_ptr<UsdPrimDefinition>
528  const TfToken &primType, const TfTokenVector &appliedAPISchemas) const;
529 
542  return _fallbackPrimTypes;
543  }
544 
545 private:
546  friend class TfSingleton<UsdSchemaRegistry>;
547 
549 
550  // For the given full API schema name (which may be "type:instance" for
551  // multiple apply API schemas), finds and returns the prim definition for
552  // the API schema type. If the API schema is an instance of a multiple
553  // apply API, the instance name will be set in instanceName.
554  const UsdPrimDefinition *_FindAPIPrimDefinitionByFullName(
555  const TfToken &apiSchemaName,
556  TfToken *instanceName) const;
557 
558  void _ComposeAPISchemasIntoPrimDefinition(
559  UsdPrimDefinition *primDef,
560  const TfTokenVector &appliedAPISchemas) const;
561 
562  // Private class for helping initialize the schema registry. Defined
563  // entirely in the implementation. Declared here for private access to the
564  // registry.
565  class _SchemaDefInitHelper;
566 
567  using _TypeNameToPrimDefinitionMap = std::unordered_map<
568  TfToken, const std::unique_ptr<UsdPrimDefinition>, TfToken::HashFunctor>;
569 
570  SdfLayerRefPtr _schematics;
571 
572  _TypeNameToPrimDefinitionMap _concreteTypedPrimDefinitions;
573  _TypeNameToPrimDefinitionMap _appliedAPIPrimDefinitions;
574 
575  // This is a mapping from multiple apply API schema name (e.g.
576  // "CollectionAPI") to the template prim definition stored for it in
577  // _appliedAPIPrimDefinitions as the template prim definition is actually
578  // mapped to its template name (e.g. "CollectionAPI:__INSTANCE_NAME__") in
579  // that map.
580  std::unordered_map<TfToken, const UsdPrimDefinition *, TfToken::HashFunctor>
581  _multiApplyAPIPrimDefinitions;
582  UsdPrimDefinition *_emptyPrimDefinition;
583 
584  VtDictionary _fallbackPrimTypes;
585 
586  friend class UsdPrimDefinition;
587 };
588 
589 USD_API_TEMPLATE_CLASS(TfSingleton<UsdSchemaRegistry>);
590 
591 // Utility function for extracting the metadata about applying API schemas from
592 // the plugin metadata for the schema's type. It is useful for certain clients
593 // to be able to access this plugin data in the same way that the
594 // UsdSchemaRegistry does.
595 void Usd_GetAPISchemaPluginApplyToInfoForType(
596  const TfType &apiSchemaType,
597  const TfToken &apiSchemaName,
598  std::map<TfToken, TfTokenVector> *autoApplyAPISchemasMap,
599  TfHashMap<TfToken, TfTokenVector, TfHash> *canOnlyApplyAPISchemasMap,
600  TfHashMap<TfToken, TfToken::Set, TfHash> *allowedInstanceNamesMap);
601 
602 PXR_NAMESPACE_CLOSE_SCOPE
603 
604 #endif //PXR_USD_USD_SCHEMA_REGISTRY_H
TfType type
The schema's type as registered with the TfType registry.
static USD_API bool IsMultipleApplyAPISchema(const TfType &apiSchemaType)
Returns true if apiSchemaType is a multiple-apply API schema type.
static USD_API bool IsAbstract(const TfType &primType)
Returns true if the prim type primType is an abstract schema type and, unlike a concrete 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:119
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
static USD_API bool IsAllowedSchemaIdentifier(const TfToken &schemaIdentifier)
Returns whether the given schemaIdentifier is an allowed schema identifier.
const UsdPrimDefinition * GetEmptyPrimDefinition() const
Returns the empty prim definition.
static USD_API bool IsMultipleApplyNameTemplate(const std::string &nameTemplate)
Returns true if nameTemplate is a multiple apply schema name template.
UsdSchemaVersion version
The version number of the schema within its schema family.
UsdSchemaKind
An enum representing which kind of schema a given schema class belongs to.
Definition: common.h:127
static USD_API TfToken MakeSchemaIdentifierForFamilyAndVersion(const TfToken &schemaFamily, UsdSchemaVersion schemaVersion)
Creates the schema identifier that would be used to define a schema of the given schemaFamily with th...
static USD_API const std::vector< const SchemaInfo * > & FindSchemaInfosInFamily(const TfToken &schemaFamily)
Finds all schemas in the given schemaFamily and returns their their schema info ordered from highest ...
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
TfToken family
The name of the family of schema's which the schema is a version of.
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.
static USD_API std::pair< TfToken, UsdSchemaVersion > ParseSchemaFamilyAndVersionFromIdentifier(const TfToken &schemaIdentifier)
Parses and returns the schema family and version values from the given schemaIdentifier.
const VtDictionary & GetFallbackPrimTypes() const
Returns a dictionary mapping concrete schema prim type names to a VtTokenArray of fallback prim type ...
TfToken identifier
The schema's identifier which is how the schema type is referred to in scene description and is also ...
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:134
static USD_API TfToken GetMultipleApplyNameTemplateBaseName(const std::string &nameTemplate)
Returns the base name for the multiple apply schema name template nameTemplate.
VersionPolicy
A policy for filtering by schema version when querying for schemas in a particular schema family.
static USD_API bool IsAllowedSchemaFamily(const TfToken &schemaFamily)
Returns whether the given schemaFamily is an allowed schema family name.
static USD_API TfToken MakeMultipleApplyNameTemplate(const std::string &namespacePrefix, const std::string &baseName)
Creates a name template that can represent a property or API schema that belongs to a multiple apply ...
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...
static USD_API const SchemaInfo * FindSchemaInfo(const TfType &schemaType)
Finds and returns the schema info for a registered schema with the given schemaType.
UsdSchemaKind kind
The schema's kind: ConcreteTyped, SingleApplyAPI, etc.
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
Structure that holds the information about a schema that is registered with the schema registry.
static USD_API TfToken MakeMultipleApplyNameInstance(const std::string &nameTemplate, const std::string &instanceName)
Returns an instance of a multiple apply schema name from the given nameTemplate for the given instanc...