All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
validationRegistry.h
Go to the documentation of this file.
1//
2// Copyright 2024 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_USD_VALIDATION_REGISTRY_H
8#define PXR_USD_USD_VALIDATION_REGISTRY_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/usd/api.h"
14
15#include <shared_mutex>
16#include <memory>
17#include <unordered_map>
18
20
21PXR_NAMESPACE_OPEN_SCOPE
22
147{
149 UsdValidationRegistry& operator=(const UsdValidationRegistry&) = delete;
150public:
151 USD_API
152 static UsdValidationRegistry& GetInstance() {
154 }
155
171 USD_API
172 void RegisterPluginValidator(const TfToken &validatorName,
173 const UsdValidateLayerTaskFn &layerTaskFn);
174
190 USD_API
191 void RegisterPluginValidator(const TfToken &validatorName,
192 const UsdValidateStageTaskFn &stageTaskFn);
193
209 USD_API
210 void RegisterPluginValidator(const TfToken &validatorName,
211 const UsdValidatePrimTaskFn &primTaskFn);
212
229 USD_API
231 const UsdValidateLayerTaskFn &layerTaskFn);
232
249 USD_API
251 const UsdValidateStageTaskFn &stageTaskFn);
252
269 USD_API
271 const UsdValidatePrimTaskFn &primTaskFn);
272
292 USD_API
293 void RegisterPluginValidatorSuite(const TfToken &validatorSuiteName,
294 const std::vector<const UsdValidator*>&
295 containedValidators);
296
316 USD_API
318 const std::vector<const UsdValidator*>&
319 containedValidators);
320
323 USD_API
324 bool HasValidator(const TfToken &validatorName) const;
325
328 USD_API
329 bool HasValidatorSuite(const TfToken &suiteName) const;
330
341 USD_API
342 std::vector<const UsdValidator*> GetOrLoadAllValidators();
343
351 USD_API
352 const UsdValidator* GetOrLoadValidatorByName(const TfToken &validatorName);
353
362 USD_API
363 std::vector<const UsdValidator*>
365
376 USD_API
377 std::vector<const UsdValidatorSuite*> GetOrLoadAllValidatorSuites();
378
386 USD_API
388 const TfToken &suiteName);
389
398 USD_API
399 std::vector<const UsdValidatorSuite*>
401
406 USD_API
407 bool GetValidatorMetadata(const TfToken &name,
408 UsdValidatorMetadata *metadata) const;
409
411 USD_API
412 UsdValidatorMetadataVector GetAllValidatorMetadata() const;
413
421 USD_API
422 UsdValidatorMetadataVector GetValidatorMetadataForPlugin(
423 const TfToken &pluginName) const;
424
432 USD_API
433 UsdValidatorMetadataVector GetValidatorMetadataForKeyword(
434 const TfToken &keyword) const;
435
443 USD_API
444 UsdValidatorMetadataVector GetValidatorMetadataForSchemaType(
445 const TfToken &schemaType) const;
446
457 USD_API
458 UsdValidatorMetadataVector GetValidatorMetadataForPlugins(
459 const TfTokenVector &pluginNames) const;
460
471 USD_API
472 UsdValidatorMetadataVector GetValidatorMetadataForKeywords(
473 const TfTokenVector &keywords) const;
474
485 USD_API
486 UsdValidatorMetadataVector GetValidatorMetadataForSchemaTypes(
487 const TfTokenVector &schemaTypes) const;
488
489private:
491
493
494 // Initialize _validatorNameToMetadata, _keywordToValidatorNames and
495 // _schemaTypeToValidatorNames by parsing all plugInfo.json, find all
496 // Validators.
497 void _PopulateMetadataFromPlugInfo();
498
499 // Templated method to register validator, called by appropriate
500 // RegisterValidator methods, providing UsdValidateLayerTaskFn,
501 // UsdValidateStageTaskFn or UsdValidatePrimTaskFn.
502 template<typename ValidateTaskFn>
503 void _RegisterPluginValidator(const TfToken &validatorName,
504 const ValidateTaskFn &taskFn);
505
506 // Overloaded templated _RegisterValidator, where metadata is explicitly
507 // provided.
508 template<typename ValidateTaskFn>
509 void _RegisterValidator(const UsdValidatorMetadata &metadata,
510 const ValidateTaskFn &taskFn, bool addMetadata = true);
511
512 void _RegisterValidatorSuite(const UsdValidatorMetadata &metadata,
513 const std::vector<const UsdValidator*>& containedValidators,
514 bool addMetadata = true);
515
516 // makes sure metadata provided is legal
517 // checkForPrimTask parameter is used to determine if schemaTypes metadata
518 // is provided and if the task being registered for the validator is
519 // UsdValidatePrimTaskFn.
520 // expectSuite parameter is used to determine if the isSuite metadata is
521 // appropriately set (for UsdValidatorSuite) or not (for UsdValidator).
522 static
523 bool _CheckMetadata(const UsdValidatorMetadata &metadata,
524 bool checkForPrimTask, bool expectSuite = false);
525
526 // Add validator metadata to _validatorNameToMetadata, also updates
527 // _schemaTypeToValidatorNames and _keywordToValidatorNames, for easy access
528 // to what validators are linked to specific schemaTypes or keywords.
529 // _mutex must be acquired before calling this method.
530 bool _AddValidatorMetadata(const UsdValidatorMetadata &metadata);
531
532 using _ValidatorNameToValidatorMap =
533 std::unordered_map<TfToken, std::unique_ptr<UsdValidator>,
535 using _ValidatorSuiteNameToValidatorSuiteMap =
536 std::unordered_map<TfToken, std::unique_ptr<UsdValidatorSuite>,
538 using _ValidatorNameToMetadataMap =
539 std::unordered_map<TfToken, UsdValidatorMetadata, TfToken::HashFunctor>;
540 using _TokenToValidatorNamesMap =
541 std::unordered_map<TfToken, TfTokenVector, TfToken::HashFunctor>;
542
543 // Helper to query
544 UsdValidatorMetadataVector _GetValidatorMetadataForToken(
545 const _TokenToValidatorNamesMap &tokenToValidatorNames,
546 const TfTokenVector &tokens) const;
547
548 // Helper to populate _keywordToValidatorNames and
549 // _schemaTypeToValidatorNames
550 // _mutex must be acquired before calling this method.
551 static
552 void _UpdateValidatorNamesMappings(_TokenToValidatorNamesMap &tokenMap,
553 const TfToken &validatorName, const TfTokenVector &tokens);
554
555 // Main datastructure which holds validatorName to
556 // std::unique_ptr<UsdValidator>
557 _ValidatorNameToValidatorMap _validators;
558 // Main datastructure which holds suiteName to
559 // std::unique_ptr<UsdValidatorSuite>
560 _ValidatorSuiteNameToValidatorSuiteMap _validatorSuites;
561
562 // ValidatorName to ValidatorMetadata map
563 _ValidatorNameToMetadataMap _validatorNameToMetadata;
564
565 // Following 3 are helper data structures to easy lookup for Validators,
566 // when queried for keywords, schemaType or pluginName.
567
568 // This map stores the mapping from keyword to validator names. It may get
569 // updated as validators can be registered dynamically outside of the plugin
570 // infrastructure.
571 _TokenToValidatorNamesMap _keywordToValidatorNames;
572
573 // This map stores the mapping from schemaTypes to validator names. It may
574 // get updated as validators can be registered dynamically outside of the
575 // plugin infrastructure.
576 _TokenToValidatorNamesMap _schemaTypeToValidatorNames;
577
578 // This map stores the mapping from plugin names to validator names.
579 // It is populated during the initialization of UsdValidationRegistry
580 // and remains constant thereafter.
581 _TokenToValidatorNamesMap _pluginNameToValidatorNames;
582
583 // Mutex to protect access to all data members.
584 mutable std::shared_mutex _mutex;
585};
586
587// Specialize and delete the DeleteInstance function to prevent destruction.
588// This will prevent the singleton instance for UsdValidationRegistry from
589// being destroyed and hence making it immortal.
590template <>
592
593USD_API_TEMPLATE_CLASS(TfSingleton<UsdValidationRegistry>);
594
595PXR_NAMESPACE_CLOSE_SCOPE
596
597#endif // PXR_USD_USD_VALIDATION_REGISTRY_H
Manage a single instance of an object (see.
Definition: singleton.h:105
static void DeleteInstance()
Destroy the sole instance object of type T, if it exists.
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:120
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
UsdValidationRegistry manages and provides access to UsdValidator / UsdValidatorSuite for USD Validat...
USD_API UsdValidatorMetadataVector GetValidatorMetadataForSchemaType(const TfToken &schemaType) const
Returns vector of UsdValidatorMetadata associated with the Validators which has the schemaType.
USD_API void RegisterValidator(const UsdValidatorMetadata &metadata, const UsdValidatePrimTaskFn &primTaskFn)
Register UsdValidator using metadata and primTaskFn with the UsdValidationRegistry.
USD_API std::vector< const UsdValidatorSuite * > GetOrLoadValidatorSuitesByName(const TfTokenVector &suiteNames)
Returns a vector of const pointer to UsdValidatorSuite corresponding to suiteNames found in the regis...
USD_API bool HasValidatorSuite(const TfToken &suiteName) const
Return true if a UsdValidatorSuite is registered with the name validatorSuiteName; false otherwise.
USD_API UsdValidatorMetadataVector GetValidatorMetadataForKeywords(const TfTokenVector &keywords) const
Returns vector of UsdValidatorMetadata associated with the Validators which has at least one of the k...
USD_API void RegisterValidator(const UsdValidatorMetadata &metadata, const UsdValidateLayerTaskFn &layerTaskFn)
Register UsdValidator using metadata and layerTaskFn with the UsdValidationRegistry.
USD_API UsdValidatorMetadataVector GetValidatorMetadataForSchemaTypes(const TfTokenVector &schemaTypes) const
Returns vector of UsdValidatorMetadata associated with the Validators which has at least one of the s...
USD_API UsdValidatorMetadataVector GetValidatorMetadataForPlugin(const TfToken &pluginName) const
Returns vector of UsdValidatorMetadata associated with the Validators which belong to the pluginName.
USD_API UsdValidatorMetadataVector GetValidatorMetadataForPlugins(const TfTokenVector &pluginNames) const
Returns vector of UsdValidatorMetadata associated with the Validators which belong to the pluginNames...
USD_API bool GetValidatorMetadata(const TfToken &name, UsdValidatorMetadata *metadata) const
Returns true if metadata is found in the _validatorNameToMetadata for a validator/suite name,...
USD_API void RegisterValidatorSuite(const UsdValidatorMetadata &metadata, const std::vector< const UsdValidator * > &containedValidators)
Register UsdValidatorSuite using metadata and containedValidators with the UsdValidationRegistry.
USD_API UsdValidatorMetadataVector GetAllValidatorMetadata() const
Return vector of all UsdValidatorMetadata known to the registry.
USD_API std::vector< const UsdValidator * > GetOrLoadValidatorsByName(const TfTokenVector &validatorNames)
Returns a vector of const pointer to UsdValidator corresponding to validatorNames found in the regist...
USD_API void RegisterPluginValidatorSuite(const TfToken &validatorSuiteName, const std::vector< const UsdValidator * > &containedValidators)
Register UsdValidatorSuite defined in a plugin using validatorSuiteName and containedValidators with ...
USD_API void RegisterPluginValidator(const TfToken &validatorName, const UsdValidatePrimTaskFn &primTaskFn)
Register UsdValidator defined in a plugin using validatorName and primTaskFn with the UsdValidationRe...
USD_API void RegisterValidator(const UsdValidatorMetadata &metadata, const UsdValidateStageTaskFn &stageTaskFn)
Register UsdValidator using metadata and stageTaskFn with the UsdValidationRegistry.
USD_API void RegisterPluginValidator(const TfToken &validatorName, const UsdValidateLayerTaskFn &layerTaskFn)
Register UsdValidator defined in a plugin using validatorName and layerTaskFn with the UsdValidationR...
USD_API const UsdValidator * GetOrLoadValidatorByName(const TfToken &validatorName)
Returns a const pointer to UsdValidator if validatorName is found in the registry.
USD_API const UsdValidatorSuite * GetOrLoadValidatorSuiteByName(const TfToken &suiteName)
Returns a const pointer to UsdValidatorSuite if suiteName is found in the registry.
USD_API std::vector< const UsdValidator * > GetOrLoadAllValidators()
Returns a vector of const pointer to UsdValidator corresponding to all validators registered in the U...
USD_API std::vector< const UsdValidatorSuite * > GetOrLoadAllValidatorSuites()
Returns a vector of const pointer to UsdValidatorSuite corresponding to all validator suites register...
USD_API UsdValidatorMetadataVector GetValidatorMetadataForKeyword(const TfToken &keyword) const
Returns vector of UsdValidatorMetadata associated with the Validators which has the keyword.
USD_API bool HasValidator(const TfToken &validatorName) const
Return true if a UsdValidator is registered with the name validatorName; false otherwise.
USD_API void RegisterPluginValidator(const TfToken &validatorName, const UsdValidateStageTaskFn &stageTaskFn)
Register UsdValidator defined in a plugin using validatorName and stageTaskFn with the UsdValidationR...
UsdValidator is a class describing a single test.
Definition: validator.h:120
UsdValidatorSuite acts like a suite for a collection of tests, which clients can use to bundle all te...
Definition: validator.h:232
std::function< UsdValidationErrorVector(const UsdStagePtr &)> UsdValidateStageTaskFn
UsdValidateStageTaskFn: Validation logic operating on a given UsdStage.
Definition: validator.h:98
std::function< UsdValidationErrorVector(const SdfLayerHandle &)> UsdValidateLayerTaskFn
UsdValidateLayerTaskFn: Validation logic operating on a given SdfLayerHandle.
Definition: validator.h:95
std::function< UsdValidationErrorVector(const UsdPrim &)> UsdValidatePrimTaskFn
UsdValidatePrimTaskFn: Validation logic operating on a given UsdPrim.
Definition: validator.h:101
Manage a single instance of an object.
Functor to use for hash maps from tokens to other things.
Definition: token.h:149
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
A structure which describes metadata for a UsdValidator.
Definition: validator.h:46