HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fixer.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_USD_VALIDATION_USD_VALIDATION_FIXER_H
9 #define PXR_USD_VALIDATION_USD_VALIDATION_FIXER_H
10 
11 #include "pxr/pxr.h"
12 #include "pxr/base/tf/token.h"
13 #include "pxr/usd/usd/timeCode.h"
15 
16 #include <functional>
17 
19 
20 class UsdValidationError;
21 class UsdEditTarget;
22 
23 using FixerImplFn =
24  std::function<bool(
25  const UsdValidationError& e, const UsdEditTarget& editTarget,
26  const UsdTimeCode& timeCode)>;
27 
28 using FixerCanApplyFn =
29  std::function<bool(
30  const UsdValidationError& e, const UsdEditTarget& editTarget,
31  const UsdTimeCode& timeCode)>;
32 
33 /// \class UsdValidationFixer
34 ///
35 /// A UsdValidationFixer represents a fix that can be applied to fix a specific
36 /// validation error.
37 ///
38 /// A fixer is associated with a specific validator, and can
39 /// be associated with a specific error name, or can be generic to any
40 /// error associated with the corresponding validator.
41 ///
42 /// A fixer has a name, description, and a set of keywords associated with it.
43 /// The name must be unique among all fixers associated with a specific
44 /// validator. The keywords can be used to group fixers by department, show,
45 /// etc.
46 ///
47 /// A fixer has two functions associated with it:
48 /// - The \p FixerImplFn is the function that will be called to apply the
49 /// fix for a given error.
50 /// - The \p FixerCanApplyFn is the function that will be called to determine
51 /// if the fixer can be applied to a given error.
52 ///
53 /// Note that the Validation framework will not apply any fixers automatically.
54 /// It's the responsibility of the client to pick and apply a fix for a given
55 /// error, by specifically calling the ApplyFix() method on an appropriate
56 /// UsdEditTarget.
57 ///
58 /// Appropriate fixers can be retrieved from UsdValidationValidator or
59 /// UsdValidationError itself via the GetFixers(), GetFixersWithKeyword(),
60 /// GetFixerByName(), GetFixersByErrorName(), etc.
61 ///
63 {
64 public:
65  UsdValidationFixer(const UsdValidationFixer&) = default;
67 
68  UsdValidationFixer(UsdValidationFixer&&) noexcept = default;
69  UsdValidationFixer& operator=(UsdValidationFixer&&) noexcept = default;
70 
71  /// Construct a UsdValidationFixer with the given \p name and \p description.
72  ///
73  /// The \p fixerImplFn is the function that will be called to apply the fix
74  /// for a given error. The \p canApplyFn is the function that will be
75  /// called to determine if the fixer can be applied to a given error.
76  /// The \p errorName, if provided, is the error name that this fixer
77  /// can fix. If not provided, the fixer can be applied to any error
78  /// associated with the corresponding validator. The \p keywords are the
79  /// keywords associated with this fixer. Clients when instantiating a fixer
80  /// for a validator, can provide keywords such as studio, department, or
81  /// show. Clients can later see which keywords are associated with the fixer
82  /// or use the GetFixersByKeywords method to return fixers by the keywords
83  /// they are associated with.
86  const TfToken &name, const std::string &description,
87  const FixerImplFn &fixerImplFn, const FixerCanApplyFn &canApplyFn,
88  const TfTokenVector &keywords = TfTokenVector(),
89  const TfToken &errorName = TfToken());
90 
91  ~UsdValidationFixer() = default;
92 
93  /// Returns the name of this fixer.
94  const TfToken &GetName() const &
95  {
96  return _name;
97  }
98 
99  /// Return the name of this fixer by-value.
101  {
102  return std::move(_name);
103  }
104 
105  /// Returns the description of this fixer.
106  const std::string &GetDescription() const &
107  {
108  return _description;
109  }
110 
111  /// Return the description of this fixer by-value.
112  std::string GetDescription() &&
113  {
114  return std::move(_description);
115  }
116 
117  /// Returns the error name that this fixer can fix, if any.
118  const TfToken &GetErrorName() const &
119  {
120  return _errorName;
121  }
122 
123  /// Returns the error name that this fixer can fix, if any, by-value.
125  {
126  return std::move(_errorName);
127  }
128 
129  /// Returns true if this fixer is associated with the given error name.
131  bool IsAssociatedWithErrorName(const TfToken &errorName) const;
132 
133  /// Returns the keywords associated with this fixer.
134  const TfTokenVector &GetKeywords() const &
135  {
136  return _keywords;
137  }
138 
139  /// Return the keywords associated with this fixer by-value.
141  {
142  return std::move(_keywords);
143  }
144 
145  /// Returns true if this fixer has the given keyword.
147  bool HasKeyword(const TfToken &keyword) const;
148 
149  /// Returns true if this fixer can be applied to the given error and edit
150  /// target; false otherwise.
151  ///
152  /// A fixer can be associated with a specific error name, if so, it can
153  /// only be applied to errors with the same name, otherwise, it can be
154  /// applied to any error generated by the corresponding validator.
155  ///
156  /// Additionally, the \p editTarget can be inspected to determine if the
157  /// fix can be applied to the given target, etc.
158  ///
159  /// \sa ApplyFix
161  bool CanApplyFix(
162  const UsdValidationError &error, const UsdEditTarget &editTarget,
163  const UsdTimeCode &timeCode = UsdTimeCode::Default()) const;
164 
165  /// Applies the fix for the given error at the given time code and edit
166  /// target. Returns true if the fix was applied successfully.
167  ///
168  /// \sa CanApplyFix
170  bool ApplyFix(
171  const UsdValidationError &error, const UsdEditTarget &editTarget,
172  const UsdTimeCode &timeCode = UsdTimeCode::Default()) const;
173 
174 private:
175  TfToken _name;
176  std::string _description;
177  FixerImplFn _fixerImplFn;
178  FixerCanApplyFn _canApplyFn;
179  TfTokenVector _keywords;
180  TfToken _errorName;
181 };
182 
184 
185 #endif // PXR_USD_VALIDATION_USD_VALIDATION_FIXER_H
std::string GetDescription()&&
Return the description of this fixer by-value.
Definition: fixer.h:112
USDVALIDATION_API bool CanApplyFix(const UsdValidationError &error, const UsdEditTarget &editTarget, const UsdTimeCode &timeCode=UsdTimeCode::Default()) const
USDVALIDATION_API bool IsAssociatedWithErrorName(const TfToken &errorName) const
Returns true if this fixer is associated with the given error name.
static constexpr UsdTimeCode Default()
Definition: timeCode.h:113
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
~UsdValidationFixer()=default
const TfTokenVector & GetKeywords() const &
Returns the keywords associated with this fixer.
Definition: fixer.h:134
TfTokenVector GetKeywords()&&
Return the keywords associated with this fixer by-value.
Definition: fixer.h:140
OutGridT const XformOp bool bool
UsdValidationFixer(const UsdValidationFixer &)=default
< returns > If no error
Definition: snippets.dox:2
#define USDVALIDATION_API
Definition: api.h:25
USDVALIDATION_API bool HasKeyword(const TfToken &keyword) const
Returns true if this fixer has the given keyword.
Definition: token.h:70
std::function< bool(const UsdValidationError &e, const UsdEditTarget &editTarget, const UsdTimeCode &timeCode)> FixerCanApplyFn
Definition: fixer.h:31
USDVALIDATION_API bool ApplyFix(const UsdValidationError &error, const UsdEditTarget &editTarget, const UsdTimeCode &timeCode=UsdTimeCode::Default()) const
const std::string & GetDescription() const &
Returns the description of this fixer.
Definition: fixer.h:106
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
GLuint const GLchar * name
Definition: glcorearb.h:786
UsdValidationFixer & operator=(const UsdValidationFixer &)=default
const TfToken & GetName() const &
Returns the name of this fixer.
Definition: fixer.h:94
const TfToken & GetErrorName() const &
Returns the error name that this fixer can fix, if any.
Definition: fixer.h:118
TfToken GetErrorName()&&
Returns the error name that this fixer can fix, if any, by-value.
Definition: fixer.h:124
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::function< bool(const UsdValidationError &e, const UsdEditTarget &editTarget, const UsdTimeCode &timeCode)> FixerImplFn
Definition: fixer.h:26
TfToken GetName()&&
Return the name of this fixer by-value.
Definition: fixer.h:100