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 UsdSchemaBase;
45 class UsdPrimDefinition;
46 
48 using UsdSchemaVersion = unsigned int;
49 
69 class UsdSchemaRegistry : public TfWeakBase, boost::noncopyable {
70 public:
71  using TokenToTokenVectorMap =
72  std::unordered_map<TfToken, TfTokenVector, TfHash>;
73 
76  struct SchemaInfo {
77 
82 
87 
93 
95  UsdSchemaVersion version;
96 
99  };
100 
101  USD_API
102  static UsdSchemaRegistry& GetInstance() {
104  }
105 
118  USD_API
119  static TfToken
121  const TfToken &schemaFamily,
122  UsdSchemaVersion schemaVersion);
123 
141  USD_API
142  static std::pair<TfToken, UsdSchemaVersion>
143  ParseSchemaFamilyAndVersionFromIdentifier(const TfToken &schemaIdentifier);
144 
152  USD_API
153  static bool
154  IsAllowedSchemaFamily(const TfToken &schemaFamily);
155 
165  USD_API
166  static bool
167  IsAllowedSchemaIdentifier(const TfToken &schemaIdentifier);
168 
172  USD_API
173  static const SchemaInfo *
174  FindSchemaInfo(const TfType &schemaType);
175 
184  template <class SchemaType>
185  static const SchemaInfo *
187  static_assert(std::is_base_of<UsdSchemaBase, SchemaType>::value,
188  "Provided type must derive UsdSchemaBase.");
189  return FindSchemaInfo(SchemaType::_GetStaticTfType());
190  }
191 
195  USD_API
196  static const SchemaInfo *
197  FindSchemaInfo(const TfToken &schemaIdentifier);
198 
202  USD_API
203  static const SchemaInfo *
204  FindSchemaInfo(const TfToken &schemaFamily, UsdSchemaVersion schemaVersion);
205 
208  enum class VersionPolicy {
209  All,
210  GreaterThan,
211  GreaterThanOrEqual,
212  LessThan,
213  LessThanOrEqual
214  };
215 
218  USD_API
219  static const std::vector<const SchemaInfo *> &
221  const TfToken &schemaFamily);
222 
226  USD_API
227  static std::vector<const SchemaInfo *>
229  const TfToken &schemaFamily,
230  UsdSchemaVersion schemaVersion,
231  VersionPolicy versionPolicy);
232 
235  USD_API
236  static TfToken GetSchemaTypeName(const TfType &schemaType);
237 
240  template <class SchemaType>
241  static
243  return GetSchemaTypeName(SchemaType::_GetStaticTfType());
244  }
245 
248  USD_API
249  static TfToken GetConcreteSchemaTypeName(const TfType &schemaType);
250 
253  USD_API
254  static TfToken GetAPISchemaTypeName(const TfType &schemaType);
255 
258  USD_API
259  static TfType GetTypeFromSchemaTypeName(const TfToken &typeName);
260 
263  USD_API
264  static TfType GetConcreteTypeFromSchemaTypeName(const TfToken &typeName);
265 
268  USD_API
269  static TfType GetAPITypeFromSchemaTypeName(const TfToken &typeName);
270 
278  USD_API
279  static bool IsDisallowedField(const TfToken &fieldName);
280 
282  USD_API
283  static bool IsTyped(const TfType& primType);
284 
290  USD_API
291  static UsdSchemaKind GetSchemaKind(const TfType &schemaType);
292 
298  USD_API
299  static UsdSchemaKind GetSchemaKind(const TfToken &typeName);
300 
303  USD_API
304  static bool IsConcrete(const TfType& primType);
305 
308  USD_API
309  static bool IsConcrete(const TfToken& primType);
310 
313  USD_API
314  static bool IsAbstract(const TfType& primType);
315 
318  USD_API
319  static bool IsAbstract(const TfToken& primType);
320 
322  USD_API
323  static bool IsAppliedAPISchema(const TfType& apiSchemaType);
324 
326  USD_API
327  static bool IsAppliedAPISchema(const TfToken& apiSchemaType);
328 
330  USD_API
331  static bool IsMultipleApplyAPISchema(const TfType& apiSchemaType);
332 
334  USD_API
335  static bool IsMultipleApplyAPISchema(const TfToken& apiSchemaType);
336 
359  USD_API
360  static TfType GetTypeFromName(const TfToken& typeName);
361 
382  USD_API
383  static std::pair<TfToken, TfToken> GetTypeNameAndInstance(
384  const TfToken &apiSchemaName);
385 
395  USD_API
396  static bool IsAllowedAPISchemaInstanceName(
397  const TfToken &apiSchemaName,
398  const TfToken &instanceName);
399 
412  USD_API
414  const TfToken &apiSchemaName,
415  const TfToken &instanceName = TfToken());
416 
425  USD_API
426  static const TokenToTokenVectorMap &GetAutoApplyAPISchemas();
427 
439  USD_API
441  TokenToTokenVectorMap *autoApplyAPISchemas);
442 
460  USD_API
462  const std::string &namespacePrefix,
463  const std::string &baseName);
464 
479  USD_API
481  const std::string &nameTemplate,
482  const std::string &instanceName);
483 
493  USD_API
495  const std::string &nameTemplate);
496 
505  USD_API
506  static bool IsMultipleApplyNameTemplate(
507  const std::string &nameTemplate);
508 
513  const TfToken &typeName) const {
514  const auto it = _concreteTypedPrimDefinitions.find(typeName);
515  return it != _concreteTypedPrimDefinitions.end() ?
516  it->second.get() : nullptr;
517  }
518 
523  const TfToken &typeName) const {
524  const auto it = _appliedAPIPrimDefinitions.find(typeName);
525  return it != _appliedAPIPrimDefinitions.end() ?
526  it->second.primDef.get() : nullptr;
527  }
528 
531  return _emptyPrimDefinition;
532  }
533 
538  USD_API
539  std::unique_ptr<UsdPrimDefinition>
541  const TfToken &primType, const TfTokenVector &appliedAPISchemas) const;
542 
555  return _fallbackPrimTypes;
556  }
557 
558 private:
559  friend class TfSingleton<UsdSchemaRegistry>;
560 
562 
563  using _FamilyAndInstanceToVersionMap =
564  std::unordered_map<std::pair<TfToken, TfToken>, UsdSchemaVersion, TfHash>;
565 
566  void _ComposeAPISchemasIntoPrimDefinition(
567  UsdPrimDefinition *primDef,
568  const TfTokenVector &appliedAPISchemas,
569  _FamilyAndInstanceToVersionMap *seenSchemaFamilyVersions,
570  bool allowDupes = false) const;
571 
572  // Private class for helping initialize the schema registry. Defined
573  // entirely in the implementation. Declared here for private access to the
574  // registry.
575  class _SchemaDefInitHelper;
576 
577  SdfLayerRefPtr _schematics;
578 
579  std::unordered_map<TfToken, const std::unique_ptr<UsdPrimDefinition>,
580  TfHash> _concreteTypedPrimDefinitions;
581 
582  struct _APISchemaDefinitionInfo {
583  std::unique_ptr<UsdPrimDefinition> primDef;
584  bool applyExpectsInstanceName;
585  };
586  std::unordered_map<TfToken, const _APISchemaDefinitionInfo, TfHash>
587  _appliedAPIPrimDefinitions;
588 
589  UsdPrimDefinition *_emptyPrimDefinition;
590 
591  VtDictionary _fallbackPrimTypes;
592 
593  friend class UsdPrimDefinition;
594 };
595 
596 USD_API_TEMPLATE_CLASS(TfSingleton<UsdSchemaRegistry>);
597 
598 // Utility function for extracting the metadata about applying API schemas from
599 // the plugin metadata for the schema's type. It is useful for certain clients
600 // to be able to access this plugin data in the same way that the
601 // UsdSchemaRegistry does.
602 void Usd_GetAPISchemaPluginApplyToInfoForType(
603  const TfType &apiSchemaType,
604  const TfToken &apiSchemaName,
605  UsdSchemaRegistry::TokenToTokenVectorMap *autoApplyAPISchemasMap,
606  UsdSchemaRegistry::TokenToTokenVectorMap *canOnlyApplyAPISchemasMap,
607  TfHashMap<TfToken, TfToken::Set, TfHash> *allowedInstanceNamesMap);
608 
609 // Utility for sorting a list of auto-applied API schemas. It is useful for
610 // certain clients to be able to make sure they can perform this type of sort
611 // in the exact same way as UsdSchemaRegistry does.
612 void Usd_SortAutoAppliedAPISchemas(
613  TfTokenVector *autoAppliedAPISchemas);
614 
615 PXR_NAMESPACE_CLOSE_SCOPE
616 
617 #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.
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: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 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...