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 
152  USD_API
153  static bool IsAbstract(const TfType& primType);
154 
157  USD_API
158  static bool IsAbstract(const TfToken& primType);
159 
161  USD_API
162  static bool IsAppliedAPISchema(const TfType& apiSchemaType);
163 
165  USD_API
166  static bool IsAppliedAPISchema(const TfToken& apiSchemaType);
167 
169  USD_API
170  static bool IsMultipleApplyAPISchema(const TfType& apiSchemaType);
171 
173  USD_API
174  static bool IsMultipleApplyAPISchema(const TfToken& apiSchemaType);
175 
198  USD_API
199  static TfType GetTypeFromName(const TfToken& typeName);
200 
221  USD_API
222  static std::pair<TfToken, TfToken> GetTypeNameAndInstance(
223  const TfToken &apiSchemaName);
224 
234  USD_API
235  static bool IsAllowedAPISchemaInstanceName(
236  const TfToken &apiSchemaName,
237  const TfToken &instanceName);
238 
251  USD_API
253  const TfToken &apiSchemaName,
254  const TfToken &instanceName = TfToken());
255 
264  USD_API
265  static const std::map<TfToken, TfTokenVector> &GetAutoApplyAPISchemas();
266 
278  USD_API
280  std::map<TfToken, TfTokenVector> *autoApplyAPISchemas);
281 
299  USD_API
301  const std::string &namespacePrefix,
302  const std::string &baseName);
303 
318  USD_API
320  const std::string &nameTemplate,
321  const std::string &instanceName);
322 
332  USD_API
334  const std::string &nameTemplate);
335 
344  USD_API
345  static bool IsMultipleApplyNameTemplate(
346  const std::string &nameTemplate);
347 
352  const TfToken &typeName) const {
353  const auto it = _concreteTypedPrimDefinitions.find(typeName);
354  return it != _concreteTypedPrimDefinitions.end() ? it->second.get() : nullptr;
355  }
356 
361  const TfToken &typeName) const {
362  // Check the single apply API schemas first then check for multiple
363  // apply schemas. This function will most often be used to find a
364  // single apply schema's prim definition as the prim definitions for
365  // multiple apply schemas aren't generally useful.
366  const auto it = _appliedAPIPrimDefinitions.find(typeName);
367  if (it != _appliedAPIPrimDefinitions.end()) {
368  return it->second.get();
369  }
370  const auto multiIt = _multiApplyAPIPrimDefinitions.find(typeName);
371  return multiIt != _multiApplyAPIPrimDefinitions.end() ?
372  multiIt->second : nullptr;
373  }
374 
377  return _emptyPrimDefinition;
378  }
379 
384  USD_API
385  std::unique_ptr<UsdPrimDefinition>
387  const TfToken &primType, const TfTokenVector &appliedAPISchemas) const;
388 
401  return _fallbackPrimTypes;
402  }
403 
404 private:
405  friend class TfSingleton<UsdSchemaRegistry>;
406 
408 
409  // For the given full API schema name (which may be "type:instance" for
410  // multiple apply API schemas), finds and returns the prim definition for
411  // the API schema type. If the API schema is an instance of a multiple
412  // apply API, the instance name will be set in instanceName.
413  const UsdPrimDefinition *_FindAPIPrimDefinitionByFullName(
414  const TfToken &apiSchemaName,
415  TfToken *instanceName) const;
416 
417  void _ComposeAPISchemasIntoPrimDefinition(
418  UsdPrimDefinition *primDef,
419  const TfTokenVector &appliedAPISchemas) const;
420 
421  // Private class for helping initialize the schema registry. Defined
422  // entirely in the implementation. Declared here for private access to the
423  // registry.
424  class _SchemaDefInitHelper;
425 
426  using _TypeNameToPrimDefinitionMap = std::unordered_map<
427  TfToken, const std::unique_ptr<UsdPrimDefinition>, TfToken::HashFunctor>;
428 
429  SdfLayerRefPtr _schematics;
430 
431  _TypeNameToPrimDefinitionMap _concreteTypedPrimDefinitions;
432  _TypeNameToPrimDefinitionMap _appliedAPIPrimDefinitions;
433 
434  // This is a mapping from multiple apply API schema name (e.g.
435  // "CollectionAPI") to the template prim definition stored for it in
436  // _appliedAPIPrimDefinitions as the template prim definition is actually
437  // mapped to its template name (e.g. "CollectionAPI:__INSTANCE_NAME__") in
438  // that map.
439  std::unordered_map<TfToken, const UsdPrimDefinition *, TfToken::HashFunctor>
440  _multiApplyAPIPrimDefinitions;
441  UsdPrimDefinition *_emptyPrimDefinition;
442 
443  VtDictionary _fallbackPrimTypes;
444 
445  friend class UsdPrimDefinition;
446 };
447 
448 USD_API_TEMPLATE_CLASS(TfSingleton<UsdSchemaRegistry>);
449 
450 // Utility function for extracting the metadata about applying API schemas from
451 // the plugin metadata for the schema's type. It is useful for certain clients
452 // to be able to access this plugin data in the same way that the
453 // UsdSchemaRegistry does.
454 void Usd_GetAPISchemaPluginApplyToInfoForType(
455  const TfType &apiSchemaType,
456  const TfToken &apiSchemaName,
457  std::map<TfToken, TfTokenVector> *autoApplyAPISchemasMap,
458  TfHashMap<TfToken, TfTokenVector, TfHash> *canOnlyApplyAPISchemasMap,
459  TfHashMap<TfToken, TfToken::Set, TfHash> *allowedInstanceNamesMap);
460 
461 PXR_NAMESPACE_CLOSE_SCOPE
462 
463 #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.
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: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.
static USD_API bool IsMultipleApplyNameTemplate(const std::string &nameTemplate)
Returns true if nameTemplate is a multiple apply schema name template.
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 TfToken GetMultipleApplyNameTemplateBaseName(const std::string &nameTemplate)
Returns the base name for the multiple apply schema name template nameTemplate.
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...
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
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...