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 #include <vector>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 SDF_DECLARE_HANDLES(SdfAttributeSpec);
43 SDF_DECLARE_HANDLES(SdfRelationshipSpec);
44 
45 class UsdSchemaBase;
46 class UsdPrimDefinition;
47 
49 using UsdSchemaVersion = unsigned int;
50 
70 class UsdSchemaRegistry : public TfWeakBase {
71  UsdSchemaRegistry(const UsdSchemaRegistry&) = delete;
72  UsdSchemaRegistry& operator=(const UsdSchemaRegistry&) = delete;
73 public:
74  using TokenToTokenVectorMap =
75  std::unordered_map<TfToken, TfTokenVector, TfHash>;
76 
79  struct SchemaInfo {
80 
85 
90 
96 
98  UsdSchemaVersion version;
99 
102  };
103 
104  USD_API
105  static UsdSchemaRegistry& GetInstance() {
107  }
108 
121  USD_API
122  static TfToken
124  const TfToken &schemaFamily,
125  UsdSchemaVersion schemaVersion);
126 
144  USD_API
145  static std::pair<TfToken, UsdSchemaVersion>
146  ParseSchemaFamilyAndVersionFromIdentifier(const TfToken &schemaIdentifier);
147 
155  USD_API
156  static bool
157  IsAllowedSchemaFamily(const TfToken &schemaFamily);
158 
168  USD_API
169  static bool
170  IsAllowedSchemaIdentifier(const TfToken &schemaIdentifier);
171 
175  USD_API
176  static const SchemaInfo *
177  FindSchemaInfo(const TfType &schemaType);
178 
187  template <class SchemaType>
188  static const SchemaInfo *
190  static_assert(std::is_base_of<UsdSchemaBase, SchemaType>::value,
191  "Provided type must derive UsdSchemaBase.");
192  return FindSchemaInfo(SchemaType::_GetStaticTfType());
193  }
194 
198  USD_API
199  static const SchemaInfo *
200  FindSchemaInfo(const TfToken &schemaIdentifier);
201 
205  USD_API
206  static const SchemaInfo *
207  FindSchemaInfo(const TfToken &schemaFamily, UsdSchemaVersion schemaVersion);
208 
211  enum class VersionPolicy {
212  All,
213  GreaterThan,
214  GreaterThanOrEqual,
215  LessThan,
216  LessThanOrEqual
217  };
218 
221  USD_API
222  static const std::vector<const SchemaInfo *> &
224  const TfToken &schemaFamily);
225 
229  USD_API
230  static std::vector<const SchemaInfo *>
232  const TfToken &schemaFamily,
233  UsdSchemaVersion schemaVersion,
234  VersionPolicy versionPolicy);
235 
238  USD_API
239  static TfToken GetSchemaTypeName(const TfType &schemaType);
240 
243  template <class SchemaType>
244  static
246  return GetSchemaTypeName(SchemaType::_GetStaticTfType());
247  }
248 
251  USD_API
252  static TfToken GetConcreteSchemaTypeName(const TfType &schemaType);
253 
256  USD_API
257  static TfToken GetAPISchemaTypeName(const TfType &schemaType);
258 
261  USD_API
262  static TfType GetTypeFromSchemaTypeName(const TfToken &typeName);
263 
266  USD_API
267  static TfType GetConcreteTypeFromSchemaTypeName(const TfToken &typeName);
268 
271  USD_API
272  static TfType GetAPITypeFromSchemaTypeName(const TfToken &typeName);
273 
281  USD_API
282  static bool IsDisallowedField(const TfToken &fieldName);
283 
285  USD_API
286  static bool IsTyped(const TfType& primType);
287 
293  USD_API
294  static UsdSchemaKind GetSchemaKind(const TfType &schemaType);
295 
301  USD_API
302  static UsdSchemaKind GetSchemaKind(const TfToken &typeName);
303 
306  USD_API
307  static bool IsConcrete(const TfType& primType);
308 
311  USD_API
312  static bool IsConcrete(const TfToken& primType);
313 
316  USD_API
317  static bool IsAbstract(const TfType& primType);
318 
321  USD_API
322  static bool IsAbstract(const TfToken& primType);
323 
325  USD_API
326  static bool IsAppliedAPISchema(const TfType& apiSchemaType);
327 
329  USD_API
330  static bool IsAppliedAPISchema(const TfToken& apiSchemaType);
331 
333  USD_API
334  static bool IsMultipleApplyAPISchema(const TfType& apiSchemaType);
335 
337  USD_API
338  static bool IsMultipleApplyAPISchema(const TfToken& apiSchemaType);
339 
362  USD_API
363  static TfType GetTypeFromName(const TfToken& typeName);
364 
385  USD_API
386  static std::pair<TfToken, TfToken> GetTypeNameAndInstance(
387  const TfToken &apiSchemaName);
388 
398  USD_API
399  static bool IsAllowedAPISchemaInstanceName(
400  const TfToken &apiSchemaName,
401  const TfToken &instanceName);
402 
415  USD_API
417  const TfToken &apiSchemaName,
418  const TfToken &instanceName = TfToken());
419 
428  USD_API
429  static const TokenToTokenVectorMap &GetAutoApplyAPISchemas();
430 
442  USD_API
444  TokenToTokenVectorMap *autoApplyAPISchemas);
445 
463  USD_API
465  const std::string &namespacePrefix,
466  const std::string &baseName);
467 
482  USD_API
484  const std::string &nameTemplate,
485  const std::string &instanceName);
486 
496  USD_API
498  const std::string &nameTemplate);
499 
508  USD_API
509  static bool IsMultipleApplyNameTemplate(
510  const std::string &nameTemplate);
511 
516  const TfToken &typeName) const {
517  const auto it = _concreteTypedPrimDefinitions.find(typeName);
518  return it != _concreteTypedPrimDefinitions.end() ?
519  it->second.get() : nullptr;
520  }
521 
526  const TfToken &typeName) const {
527  const auto it = _appliedAPIPrimDefinitions.find(typeName);
528  return it != _appliedAPIPrimDefinitions.end() ?
529  it->second.primDef.get() : nullptr;
530  }
531 
534  return _emptyPrimDefinition;
535  }
536 
541  USD_API
542  std::unique_ptr<UsdPrimDefinition>
544  const TfToken &primType, const TfTokenVector &appliedAPISchemas) const;
545 
558  return _fallbackPrimTypes;
559  }
560 
561 private:
562  friend class TfSingleton<UsdSchemaRegistry>;
563 
565 
566  using _FamilyAndInstanceToVersionMap =
567  std::unordered_map<std::pair<TfToken, TfToken>, UsdSchemaVersion, TfHash>;
568 
569  void _ComposeAPISchemasIntoPrimDefinition(
570  UsdPrimDefinition *primDef,
571  const TfTokenVector &appliedAPISchemas,
572  _FamilyAndInstanceToVersionMap *seenSchemaFamilyVersions,
573  bool allowDupes = false) const;
574 
575  // Private class for helping initialize the schema registry. Defined
576  // entirely in the implementation. Declared here for private access to the
577  // registry.
578  class _SchemaDefInitHelper;
579 
580  std::vector<SdfLayerRefPtr> _schematicsLayers;
581 
582  std::unordered_map<TfToken, const std::unique_ptr<UsdPrimDefinition>,
583  TfHash> _concreteTypedPrimDefinitions;
584 
585  struct _APISchemaDefinitionInfo {
586  std::unique_ptr<UsdPrimDefinition> primDef;
587  bool applyExpectsInstanceName;
588  };
589  std::unordered_map<TfToken, const _APISchemaDefinitionInfo, TfHash>
590  _appliedAPIPrimDefinitions;
591 
592  UsdPrimDefinition *_emptyPrimDefinition;
593 
594  VtDictionary _fallbackPrimTypes;
595 
596  friend class UsdPrimDefinition;
597 };
598 
599 USD_API_TEMPLATE_CLASS(TfSingleton<UsdSchemaRegistry>);
600 
601 // Utility function for extracting the metadata about applying API schemas from
602 // the plugin metadata for the schema's type. It is useful for certain clients
603 // to be able to access this plugin data in the same way that the
604 // UsdSchemaRegistry does.
605 void Usd_GetAPISchemaPluginApplyToInfoForType(
606  const TfType &apiSchemaType,
607  const TfToken &apiSchemaName,
608  UsdSchemaRegistry::TokenToTokenVectorMap *autoApplyAPISchemasMap,
609  UsdSchemaRegistry::TokenToTokenVectorMap *canOnlyApplyAPISchemasMap,
610  TfHashMap<TfToken, TfToken::Set, TfHash> *allowedInstanceNamesMap);
611 
612 // Utility for sorting a list of auto-applied API schemas. It is useful for
613 // certain clients to be able to make sure they can perform this type of sort
614 // in the exact same way as UsdSchemaRegistry does.
615 void Usd_SortAutoAppliedAPISchemas(
616  TfTokenVector *autoAppliedAPISchemas);
617 
618 PXR_NAMESPACE_CLOSE_SCOPE
619 
620 #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:120
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:62
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.
static USD_API const TokenToTokenVectorMap & GetAutoApplyAPISchemas()
Returns a map of the names of all registered auto apply API schemas to the list of type names each is...
static const SchemaInfo * FindSchemaInfo()
Finds and returns the schema info for a registered schema with the C++ schema class SchemaType.
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.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:504
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.
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:135
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 void CollectAddtionalAutoApplyAPISchemasFromPlugins(TokenToTokenVectorMap *autoApplyAPISchemas)
Collects all the additional auto apply schemas that can be defined in a plugin through "AutoApplyAPIS...
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
Singleton registry that provides access to schema type information and the prim definitions for regis...
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...
The base class for all schema types in Usd.
Definition: schemaBase.h:56
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...