Loading...
Searching...
No Matches
attributeLimits.h
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_USD_ATTRIBUTE_LIMITS_H
9#define PXR_USD_USD_ATTRIBUTE_LIMITS_H
10
11#include "pxr/usd/usd/attribute.h"
12
14#include "pxr/base/tf/token.h"
15#include "pxr/base/vt/value.h"
16
17#include <optional>
18#include <string>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
22#define USD_LIMITS_KEYS \
23 ((Soft, "soft")) \
24 ((Hard, "hard")) \
25 ((Minimum, "minimum")) \
26 ((Maximum, "maximum"))
27
29 UsdLimitsKeys, USD_API, USD_LIMITS_KEYS);
30
108{
109public:
115
118 USD_API
120 const UsdAttribute& attr,
121 const TfToken& subDictKey);
122
128
133 USD_API
134 bool IsValid() const;
135
137 USD_API
139
141 USD_API
143
145
151
154 USD_API
155 bool HasAuthored() const;
156
159 USD_API
160 bool Clear();
161
164 USD_API
165 bool HasAuthored(const TfToken& key) const;
166
169 USD_API
170 bool Clear(const TfToken& key);
171
174 USD_API
175 bool HasAuthoredMinimum() const;
176
179 USD_API
181
184 USD_API
185 bool HasAuthoredMaximum() const;
186
189 USD_API
191
193
198
203 {
204 public:
206 ValidationResult() = default;
207
209 bool Success() const {
210 return _success;
211 }
212
220 return _invalidValuesDict;
221 }
222
232 return _conformedSubDict;
233 }
234
238 USD_API
239 std::string GetErrorString() const;
240
244 explicit operator bool() const {
245 return Success();
246 }
247
249 bool operator==(const ValidationResult& rhs) const {
250 return _success == rhs._success &&
251 _invalidValuesDict == rhs._invalidValuesDict &&
252 _conformedSubDict == rhs._conformedSubDict &&
253 _attrPath == rhs._attrPath &&
254 _attrTypeName == rhs._attrTypeName;
255 }
256
258 bool operator!=(const ValidationResult& rhs) const {
259 return !(*this == rhs);
260 }
261
262 private:
263 friend class UsdAttributeLimits;
265 bool success,
266 const VtDictionary& invalidValuesDict,
267 const VtDictionary& conformedSubDict,
268 const SdfPath& attrPath,
269 const std::string& attrTypeName)
270 : _success(success),
271 _invalidValuesDict(invalidValuesDict),
272 _conformedSubDict(conformedSubDict),
273 _attrPath(attrPath),
274 _attrTypeName(attrTypeName) {}
275
276 private:
277 bool _success = false;
278 VtDictionary _invalidValuesDict;
279 VtDictionary _conformedSubDict;
280 SdfPath _attrPath;
281 std::string _attrTypeName;
282 };
283
286 //
291 USD_API
293 const VtDictionary& subDict,
294 ValidationResult* result = nullptr) const;
295
303 USD_API
304 bool Set(const VtDictionary& subDict);
305
307
313
318 template <typename T>
319 std::optional<T> Get(const TfToken& key) const;
320
324 template <typename T>
325 T GetOr(const TfToken& key, const T& defaultValue) const;
326
329 template <typename T>
330 bool Set(const TfToken& key, const T& value);
331
335 template <typename T>
336 std::optional<T> GetMinimum() const;
337
341 template <typename T>
342 T GetMinimumOr(const T& defaultValue) const;
343
348 template <typename T>
349 bool SetMinimum(const T& value);
350
354 template <typename T>
355 std::optional<T> GetMaximum() const;
356
360 template <typename T>
361 T GetMaximumOr(const T& defaultValue) const;
362
367 template <typename T>
368 bool SetMaximum(const T& value);
369
371
377
379 USD_API
380 VtValue Get(const TfToken& key) const;
381
383 USD_API
384 bool Set(const TfToken& key, const VtValue& value);
385
387 USD_API
389
391 USD_API
392 bool SetMinimum(const VtValue& value);
393
395 USD_API
397
399 USD_API
400 bool SetMaximum(const VtValue& value);
401
403
407 explicit operator bool() const {
408 return IsValid();
409 }
410
412 bool operator==(const UsdAttributeLimits& rhs) const {
413 return _attr == rhs._attr && _subDictKey == rhs._subDictKey;
414 }
415
417 bool operator!=(const UsdAttributeLimits& rhs) const {
418 return !(*this == rhs);
419 }
420
421private:
422 USD_API
423 static TfToken _MakeKeyPath(const TfToken&, const TfToken&);
424
425private:
426 UsdAttribute _attr;
427 TfToken _subDictKey;
428};
429
430template <typename T>
431inline std::optional<T>
433{
434 if (!IsValid() || key.IsEmpty()) {
435 return {};
436 }
437
438 T value;
439 if (_attr.GetMetadataByDictKey(
440 SdfFieldKeys->Limits,
441 _MakeKeyPath(_subDictKey, key),
442 &value)) {
443 return value;
444 }
445 return {};
446}
447
448template <typename T>
449inline T
451 const TfToken& key,
452 const T& defaultValue) const
453{
454 if (!IsValid() || key.IsEmpty()) {
455 return defaultValue;
456 }
457
458 T value;
459 if (_attr.GetMetadataByDictKey(
460 SdfFieldKeys->Limits,
461 _MakeKeyPath(_subDictKey, key),
462 &value)) {
463 return value;
464 }
465 return defaultValue;
466}
467
468template <typename T>
469inline bool
471 const TfToken& key,
472 const T& value)
473{
474 return Set(key, VtValue(value));
475}
476
477template <typename T>
478inline std::optional<T>
480{
481 return Get<T>(UsdLimitsKeys->Minimum);
482}
483
484template <typename T>
485inline T
486UsdAttributeLimits::GetMinimumOr(const T& defaultValue) const
487{
488 return GetOr<T>(UsdLimitsKeys->Minimum, defaultValue);
489}
490
491template <typename T>
492inline bool
494{
495 return Set(UsdLimitsKeys->Minimum, value);
496}
497
498template <typename T>
499inline std::optional<T>
501{
502 return Get<T>(UsdLimitsKeys->Maximum);
503}
504
505template <typename T>
506inline T
507UsdAttributeLimits::GetMaximumOr(const T& defaultValue) const
508{
509 return GetOr<T>(UsdLimitsKeys->Maximum, defaultValue);
510}
511
512template <typename T>
513inline bool
515{
516 return Set(UsdLimitsKeys->Maximum, value);
517}
518
519PXR_NAMESPACE_CLOSE_SCOPE
520
521#endif
A path value used to locate objects in layers or scenegraphs.
Definition path.h:281
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:81
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition token.h:335
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition attribute.h:183
Validation information for a limits sub-dictionary.
bool Success() const
Return whether validation was successful.
bool operator==(const ValidationResult &rhs) const
Equality operator.
bool operator!=(const ValidationResult &rhs) const
Inequality operator.
USD_API std::string GetErrorString() const
Return a formatted error string describing the keys and values in the invalid values dictionary.
const VtDictionary & GetConformedSubDict() const
Return the conformed limits sub-dictionary.
ValidationResult()=default
Construct an empty result.
const VtDictionary & GetInvalidValuesDict() const
Return a dictionary containing values from the source sub-dictionary that did not match (and could no...
Provides API for retrieving and authoring values within a particular sub-dictionary of the limits dic...
bool SetMinimum(const T &value)
Set the minimum value in the limits sub-dictionary.
bool operator==(const UsdAttributeLimits &rhs) const
Equality operator.
T GetMaximumOr(const T &defaultValue) const
Return the maximum value from the limits sub-dictionary.
USD_API bool ClearMinimum()
Clear the authored minimum value opinion in the limits sub-dictionary.
USD_API bool HasAuthoredMaximum() const
Return whether an authored maximum value opinion exists in the limits sub-dictionary.
USD_API bool IsValid() const
Return whether the limits object is valid.
USD_API bool SetMaximum(const VtValue &value)
This is an overloaded member function, provided for convenience. It differs from the above function o...
T GetOr(const TfToken &key, const T &defaultValue) const
Return the value encoded under key in the limits sub-dictionary.
USD_API UsdAttributeLimits(const UsdAttribute &attr, const TfToken &subDictKey)
Construct a limits object for the sub-dictionary given by subDictKey in attr's limits dictionary.
std::optional< T > Get(const TfToken &key) const
Return the value encoded under key in the limits sub-dictionary.
std::optional< T > GetMaximum() const
Return the maximum value from the limits sub-dictionary.
USD_API UsdAttribute GetAttribute() const
Return the limits object's attribute.
USD_API bool Set(const TfToken &key, const VtValue &value)
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API bool Set(const VtDictionary &subDict)
Set the entire limits sub-dictionary to subDict.
USD_API bool SetMinimum(const VtValue &value)
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API VtValue GetMinimum() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T GetMinimumOr(const T &defaultValue) const
Return the minimum value from the limits sub-dictionary.
USD_API bool Clear()
Clear all authored opinions for the limits sub-dictionary at the current edit target.
bool operator!=(const UsdAttributeLimits &rhs) const
Inequality operator.
USD_API bool HasAuthored() const
Return whether any authored opinions exist for the limits sub-dictionary.
USD_API bool Clear(const TfToken &key)
Clear the authored opinion for key in the limits sub-dictionary.
std::optional< T > GetMinimum() const
Return the minimum value from the limits sub-dictionary.
USD_API VtValue GetMaximum() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool SetMaximum(const T &value)
Set the maximum value in the limits sub-dictionary.
USD_API VtValue Get(const TfToken &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API bool HasAuthoredMinimum() const
Return whether an authored minimum value opinion exists in the limits sub-dictionary.
USD_API bool HasAuthored(const TfToken &key) const
Return whether an authored opinion for key exists in the limits sub-dictionary.
USD_API bool Validate(const VtDictionary &subDict, ValidationResult *result=nullptr) const
Return whether subDict is a valid limits sub-dictionary.
USD_API TfToken GetSubDictKey() const
Return the sub-dictionary key the limits object is using.
UsdAttributeLimits()=default
Construct an invalid limits object.
USD_API bool ClearMaximum()
Clear the authored maximum value opinion in the limits sub-dictionary.
bool GetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, T *value) const
Resolve the requested dictionary sub-element keyPath of dictionary-valued metadatum named key into va...
Definition object.h:751
A map with string keys and VtValue values.
Definition dictionary.h:52
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...