Added overflow handling in JsonVariant::as<T>() and JsonVariant::is<T>()

This commit is contained in:
Benoit Blanchon
2019-03-06 15:31:37 +01:00
parent 746f2882f7
commit 576543c4b4
42 changed files with 781 additions and 434 deletions

View File

@@ -17,10 +17,10 @@ struct FloatTraits {};
template <typename T>
struct FloatTraits<T, 8 /*64bits*/> {
typedef int64_t mantissa_type;
typedef uint64_t mantissa_type;
static const short mantissa_bits = 52;
static const mantissa_type mantissa_max =
(static_cast<mantissa_type>(1) << mantissa_bits) - 1;
(mantissa_type(1) << mantissa_bits) - 1;
typedef int16_t exponent_type;
static const exponent_type exponent_max = 308;
@@ -95,6 +95,14 @@ struct FloatTraits<T, 8 /*64bits*/> {
return forge(0x7ff00000, 0x00000000);
}
static T highest() {
return forge(0x7FEFFFFF, 0xFFFFFFFF);
}
static T lowest() {
return forge(0xFFEFFFFF, 0xFFFFFFFF);
}
// constructs a double floating point values from its binary representation
// we use this function to workaround platforms with single precision literals
// (for example, when -fsingle-precision-constant is passed to GCC)
@@ -105,10 +113,10 @@ struct FloatTraits<T, 8 /*64bits*/> {
template <typename T>
struct FloatTraits<T, 4 /*32bits*/> {
typedef int32_t mantissa_type;
typedef uint32_t mantissa_type;
static const short mantissa_bits = 23;
static const mantissa_type mantissa_max =
(static_cast<mantissa_type>(1) << mantissa_bits) - 1;
(mantissa_type(1) << mantissa_bits) - 1;
typedef int8_t exponent_type;
static const exponent_type exponent_max = 38;
@@ -156,5 +164,13 @@ struct FloatTraits<T, 4 /*32bits*/> {
static T inf() {
return forge(0x7f800000);
}
static T highest() {
return forge(0x7f7fffff);
}
static T lowest() {
return forge(0xFf7fffff);
}
};
} // namespace ARDUINOJSON_NAMESPACE