HDK
|
#include "pxr/pxr.h"
#include "pxr/base/gf/traits.h"
#include <cmath>
#include <limits>
#include <optional>
#include <type_traits>
Go to the source code of this file.
Enumerations | |
enum | GfNumericCastFailureType { GfNumericCastPosOverflow, GfNumericCastNegOverflow, GfNumericCastNaN } |
Functions | |
template<class T , class U > | |
PXR_NAMESPACE_OPEN_SCOPE constexpr bool | GfIntegerCompareLess (T t, U u) noexcept |
template<class To , class From > | |
std::optional< To > | GfNumericCast (From from, GfNumericCastFailureType *failType=nullptr) |
Enumerator | |
---|---|
GfNumericCastPosOverflow |
Value too high to convert. |
GfNumericCastNegOverflow |
Value too low to convert. |
GfNumericCastNaN |
Value is a floating-point NaN. |
Definition at line 45 of file numericCast.h.
|
noexcept |
Return true if integer t
compares logically less-than integer u
in a mathematical sense. The comparison is safe against non-value-preserving integral conversion.
This mimics the C++20 std::cmp_less function for comparing integers of different types where negative signed integers always compare less than (and not equal to) unsigned integers.
Definition at line 30 of file numericCast.h.
std::optional<To> GfNumericCast | ( | From | from, |
GfNumericCastFailureType * | failType = nullptr |
||
) |
Attempt to convert from
to a value of type To
"safely". From and To must be arithmetic types according to GfIsArithmetic – either integral or floating-point types (including GfHalf). Return a std::optional holding the converted value if conversion succeeds, otherwise the empty optional. The optional out-parameter failType
can be used to determine why conversion failed if desired.
What "safely" means depends on the types From and To. If From and To are both integral types, then from
can safely convert to To if from
is in To's range. For example if from
is an int32_t and To is uint16_t, then from
can successfully convert if it is in the range [0, 65535].
If To is an integral type and From is a floating-point type (including GfHalf), then from
can safely convert to To if it is neither a NaN nor an infinity, and after truncation to integer its value is in To's range, as above.
Following boost::numeric_cast's behavior, no range checking is performed converting from integral to floating-point or from floating-point to other floating-point types. Note that converting an integral value that is out of GfHalf's finite range will produce a +/- inf GfHalf.
Definition at line 75 of file numericCast.h.