Loading...
Searching...
No Matches
error.h
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
8#ifndef PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
9#define PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
10
11#include "pxr/pxr.h"
12#include "pxr/usd/sdf/path.h"
13#include "pxr/usd/usd/property.h"
14#include "pxr/usd/usd/stage.h"
15#include "pxr/usdValidation/usdValidation/api.h"
16
17#include <string>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21class UsdPrim;
23
37{
38 None = 0,
39 Error,
40 Warn,
41 Info
42};
43
54{
55public:
56 UsdValidationErrorSite() = default;
57
65 USDVALIDATION_API
66 UsdValidationErrorSite(const SdfLayerHandle &layer,
67 const SdfPath &objectPath);
68
79 USDVALIDATION_API
80 UsdValidationErrorSite(const UsdStagePtr &usdStage,
81 const SdfPath &objectPath,
82 const SdfLayerHandle &layer = SdfLayerHandle());
83
86 bool IsValid() const
87 {
88 return IsValidSpecInLayer() || IsPrim() || IsProperty();
89 }
90
93 bool IsValidSpecInLayer() const
94 {
95 if (!_layer || _objectPath.IsEmpty()) {
96 return false;
97 }
98 return _layer->HasSpec(_objectPath);
99 }
100
103 bool IsPrim() const
104 {
105 return GetPrim().IsValid();
106 }
107
110 bool IsProperty() const
111 {
112 return GetProperty().IsValid();
113 }
114
121 const SdfPropertySpecHandle GetPropertySpec() const
122 {
123 if (!_layer) {
124 return SdfPropertySpecHandle();
125 }
126 return _layer->GetPropertyAtPath(_objectPath);
127 }
128
134 const SdfPrimSpecHandle GetPrimSpec() const
135 {
136 if (!_layer) {
137 return SdfPrimSpecHandle();
138 }
139 return _layer->GetPrimAtPath(_objectPath);
140 }
141
144 const SdfLayerHandle &GetLayer() const
145 {
146 return _layer;
147 }
148
151 const UsdStagePtr &GetStage() const
152 {
153 return _usdStage;
154 }
155
160 {
161 if (_usdStage) {
162 return _usdStage->GetPrimAtPath(_objectPath);
163 }
164 return UsdPrim();
165 }
166
171 {
172 if (_usdStage) {
173 return _usdStage->GetPropertyAtPath(_objectPath);
174 }
175 return UsdProperty();
176 }
177
180 bool operator==(const UsdValidationErrorSite &other) const
181 {
182 return (_layer == other._layer) && (_usdStage == other._usdStage)
183 && (_objectPath == other._objectPath);
184 }
185
188 bool operator!=(const UsdValidationErrorSite &other) const
189 {
190 return !(*this == other);
191 }
192
193private:
194 UsdStagePtr _usdStage;
195 SdfLayerHandle _layer;
196 SdfPath _objectPath;
197
198}; // UsdValidationErrorSite
199
200using UsdValidationErrorSites = std::vector<UsdValidationErrorSite>;
201
225{
226public:
228 USDVALIDATION_API
230
233 USDVALIDATION_API
235 const UsdValidationErrorType &errorType,
236 const UsdValidationErrorSites &errorSites,
237 const std::string &errorMsg);
238
239 bool operator==(const UsdValidationError &other) const
240 {
241 return (_name == other._name) && (_errorType == other._errorType)
242 && (_errorSites == other._errorSites)
243 && (_errorMsg == other._errorMsg)
244 && (_validator == other._validator);
245 }
246
247 bool operator!=(const UsdValidationError &other) const
248 {
249 return !(*this == other);
250 }
251
253 const TfToken &GetName() const &
254 {
255 return _name;
256 }
257
260 {
261 return std::move(_name);
262 }
263
267 {
268 return _errorType;
269 }
270
273 const UsdValidationErrorSites &GetSites() const &
274 {
275 return _errorSites;
276 }
277
280 UsdValidationErrorSites GetSites() &&
281 {
282 return std::move(_errorSites);
283 }
284
291 {
292 return _validator;
293 }
294
296 const std::string &GetMessage() const
297 {
298 return _errorMsg;
299 }
300
316 USDVALIDATION_API
318
320 USDVALIDATION_API
321 std::string GetErrorAsString() const;
322
325 bool HasNoError() const
326 {
327 return _errorType == UsdValidationErrorType::None;
328 }
329
330private:
331 // UsdValidationValidatorError holds a pointer to the UsdValidationValidator
332 // that generated it, so we need to provide friend access to allow the
333 // necessary mutation.
334 friend class UsdValidationValidator;
335
336 // Used by UsdValidationValidator::Validate methods to embed itself to the
337 // reported errors.
338 void _SetValidator(const UsdValidationValidator *validator);
339
340 // _validator is set when ValidationError is generated via a
341 // UsdValidationValidator::Validate() call.
342 const UsdValidationValidator *_validator;
343
344 // These data members should not be modified other than during
345 // initialization by the validate task functions.
346 TfToken _name;
347 UsdValidationErrorType _errorType;
348 UsdValidationErrorSites _errorSites;
349 std::string _errorMsg;
350
351 // TODO:(Subsequent iterations)
352 // - VtValue of a random value the error wants to propagate down to the
353 // fixer
354
355}; // UsdValidationError
356
357PXR_NAMESPACE_CLOSE_SCOPE
358
359#endif // PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:126
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Base class for UsdAttribute and UsdRelationship scenegraph objects.
Definition: property.h:38
UsdValidationError is an entity returned by a validation task, which is associated with a UsdValidati...
Definition: error.h:225
USDVALIDATION_API UsdValidationError()
A default constructed UsdValidationError signifies no error.
TfToken GetName() &&
Returns the name token of the UsdValidationError by-value.
Definition: error.h:259
const UsdValidationErrorSites & GetSites() const &
Returns the UsdValidationErrorSite associated with this UsdValidationError.
Definition: error.h:273
USDVALIDATION_API UsdValidationError(const TfToken &name, const UsdValidationErrorType &errorType, const UsdValidationErrorSites &errorSites, const std::string &errorMsg)
Instantiate a ValidationError by providing its name, errorType, errorSites and an errorMsg.
UsdValidationErrorType GetType() const
Returns the UsdValidationErrorType associated with this UsdValidationError.
Definition: error.h:266
const TfToken & GetName() const &
Returns the name token of the UsdValidationError.
Definition: error.h:253
const std::string & GetMessage() const
Returns the message associated with this UsdValidationError.
Definition: error.h:296
bool HasNoError() const
Returns true if UsdValidationErrorType is UsdValidationErrorType::None, false otherwise.
Definition: error.h:325
UsdValidationErrorSites GetSites() &&
Returns the UsdValidationErrorSite associated with this UsdValidationError by-value.
Definition: error.h:280
const UsdValidationValidator * GetValidator() const
Returns the UsdValidationValidator that reported this error.
Definition: error.h:290
USDVALIDATION_API TfToken GetIdentifier() const
An identifier for the error constructed from the validator name this error was generated from and its...
USDVALIDATION_API std::string GetErrorAsString() const
Returns UsdValidationErrorType and ErrorMessage concatenated as a string.
UsdValidationErrorType reflects severity of a validation error, which can then be reported appropriat...
UsdValidationValidator is a class describing a single test.
Definition: validator.h:130
UsdValidationErrorSite is important information available from a ValidationError, which annotates the...
Definition: error.h:54
bool operator!=(const UsdValidationErrorSite &other) const
Returns false if other UsdValidationErrorSite has same valued members as this UsdValidationErrorSite,...
Definition: error.h:188
bool IsProperty() const
Returns true if UsdValidationErrorSite represents a property on a stage, false otherwise.
Definition: error.h:110
USDVALIDATION_API UsdValidationErrorSite(const UsdStagePtr &usdStage, const SdfPath &objectPath, const SdfLayerHandle &layer=SdfLayerHandle())
Initialize an UsdValidationErrorSite using a usdStage and an objectPath.
bool IsValidSpecInLayer() const
Returns true if the objectPath and layer represent a spec in the layer; false otherwise.
Definition: error.h:93
UsdProperty GetProperty() const
Returns UsdProperty associated with this UsdValidationErrorSite, that is when UsdStage is present and...
Definition: error.h:170
bool IsPrim() const
Returns true if UsdValidationErrorSite represents a prim on a stage, false otherwise.
Definition: error.h:103
USDVALIDATION_API UsdValidationErrorSite(const SdfLayerHandle &layer, const SdfPath &objectPath)
Initialize an UsdValidationErrorSite using a layer and an objectPath.
UsdPrim GetPrim() const
Returns UsdPrim associated with this UsdValidationErrorSite, that is when UsdStage is present and obj...
Definition: error.h:159
bool operator==(const UsdValidationErrorSite &other) const
Returns true if other UsdValidationErrorSite has same valued members as this UsdValidationErrorSite,...
Definition: error.h:180
const SdfPrimSpecHandle GetPrimSpec() const
Returns the SdfPrimSpecHandle associated with this ValidationErrorSite's layer and objectPath.
Definition: error.h:134
const UsdStagePtr & GetStage() const
Returns the UsdStage associated with this UsdValidationErrorSite; nullptr othewrise.
Definition: error.h:151
bool IsValid() const
Returns true if UsdValidationErrorSite instance can either point to a prim or property spec in a laye...
Definition: error.h:86
const SdfLayerHandle & GetLayer() const
Returns the SdfLayerHandle associated with this UsdValidationValidatorErrorSite.
Definition: error.h:144
const SdfPropertySpecHandle GetPropertySpec() const
Returns the SdfPropertySpecHandle associated with this ValidationErrorSite's layer and objectPath.
Definition: error.h:121