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
301 USD_API
302 bool Set(const VtDictionary& subDict);
303
305
311
316 template <typename T>
317 std::optional<T> Get(const TfToken& key) const;
318
322 template <typename T>
323 T GetOr(const TfToken& key, const T& defaultValue) const;
324
327 template <typename T>
328 bool Set(const TfToken& key, const T& value);
329
333 template <typename T>
334 std::optional<T> GetMinimum() const;
335
339 template <typename T>
340 T GetMinimumOr(const T& defaultValue) const;
341
346 template <typename T>
347 bool SetMinimum(const T& value);
348
352 template <typename T>
353 std::optional<T> GetMaximum() const;
354
358 template <typename T>
359 T GetMaximumOr(const T& defaultValue) const;
360
365 template <typename T>
366 bool SetMaximum(const T& value);
367
369
375
377 USD_API
378 VtValue Get(const TfToken& key) const;
379
381 USD_API
382 bool Set(const TfToken& key, const VtValue& value);
383
385 USD_API
387
389 USD_API
390 bool SetMinimum(const VtValue& value);
391
393 USD_API
395
397 USD_API
398 bool SetMaximum(const VtValue& value);
399
401
405 explicit operator bool() const {
406 return IsValid();
407 }
408
410 bool operator==(const UsdAttributeLimits& rhs) const {
411 return _attr == rhs._attr && _subDictKey == rhs._subDictKey;
412 }
413
415 bool operator!=(const UsdAttributeLimits& rhs) const {
416 return !(*this == rhs);
417 }
418
419private:
420 USD_API
421 static TfToken _MakeKeyPath(const TfToken&, const TfToken&);
422
423private:
424 UsdAttribute _attr;
425 TfToken _subDictKey;
426};
427
428template <typename T>
429inline std::optional<T>
431{
432 if (!IsValid() || key.IsEmpty()) {
433 return {};
434 }
435
436 T value;
437 if (_attr.GetMetadataByDictKey(
438 SdfFieldKeys->Limits,
439 _MakeKeyPath(_subDictKey, key),
440 &value)) {
441 return value;
442 }
443 return {};
444}
445
446template <typename T>
447inline T
449 const TfToken& key,
450 const T& defaultValue) const
451{
452 if (!IsValid() || key.IsEmpty()) {
453 return defaultValue;
454 }
455
456 T value;
457 if (_attr.GetMetadataByDictKey(
458 SdfFieldKeys->Limits,
459 _MakeKeyPath(_subDictKey, key),
460 &value)) {
461 return value;
462 }
463 return defaultValue;
464}
465
466template <typename T>
467inline bool
469 const TfToken& key,
470 const T& value)
471{
472 return Set(key, VtValue(value));
473}
474
475template <typename T>
476inline std::optional<T>
478{
479 return Get<T>(UsdLimitsKeys->Minimum);
480}
481
482template <typename T>
483inline T
484UsdAttributeLimits::GetMinimumOr(const T& defaultValue) const
485{
486 return GetOr<T>(UsdLimitsKeys->Minimum, defaultValue);
487}
488
489template <typename T>
490inline bool
492{
493 return Set(UsdLimitsKeys->Minimum, value);
494}
495
496template <typename T>
497inline std::optional<T>
499{
500 return Get<T>(UsdLimitsKeys->Maximum);
501}
502
503template <typename T>
504inline T
505UsdAttributeLimits::GetMaximumOr(const T& defaultValue) const
506{
507 return GetOr<T>(UsdLimitsKeys->Maximum, defaultValue);
508}
509
510template <typename T>
511inline bool
513{
514 return Set(UsdLimitsKeys->Maximum, value);
515}
516
517PXR_NAMESPACE_CLOSE_SCOPE
518
519#endif
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition: token.h:288
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:767
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.
Definition: staticTokens.h:92
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...