♻️ refined SFINAE to fix some warnings

This commit is contained in:
Niels Lohmann 2018-03-07 22:01:44 +01:00
parent 476b2e09be
commit d2d65bb25b
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 194 additions and 174 deletions

View file

@ -704,11 +704,11 @@ class binary_writer
oa->write_characters(vec.data(), sizeof(NumberType));
}
template<typename NumberType>
// UBJSON: write number (floating point)
template<typename NumberType, typename std::enable_if<
std::is_floating_point<NumberType>::value, int>::type = 0>
void write_number_with_ubjson_prefix(const NumberType n,
const bool add_prefix)
{
if (std::is_floating_point<NumberType>::value)
{
if (add_prefix)
{
@ -716,9 +716,14 @@ class binary_writer
}
write_number(n);
}
else if (std::is_unsigned<NumberType>::value)
// UBJSON: write number (unsigned integer)
template<typename NumberType, typename std::enable_if<
std::is_unsigned<NumberType>::value, int>::type = 0>
void write_number_with_ubjson_prefix(const NumberType n,
const bool add_prefix)
{
if (n <= (std::numeric_limits<int8_t>::max)())
if (n <= static_cast<uint64_t>((std::numeric_limits<int8_t>::max)()))
{
if (add_prefix)
{
@ -734,7 +739,7 @@ class binary_writer
}
write_number(static_cast<uint8_t>(n));
}
else if (n <= (std::numeric_limits<int16_t>::max)())
else if (n <= static_cast<uint64_t>((std::numeric_limits<int16_t>::max)()))
{
if (add_prefix)
{
@ -742,7 +747,7 @@ class binary_writer
}
write_number(static_cast<int16_t>(n));
}
else if (n <= (std::numeric_limits<int32_t>::max)())
else if (n <= static_cast<uint64_t>((std::numeric_limits<int32_t>::max)()))
{
if (add_prefix)
{
@ -750,7 +755,7 @@ class binary_writer
}
write_number(static_cast<int32_t>(n));
}
else if (n <= (std::numeric_limits<int64_t>::max)())
else if (n <= static_cast<uint64_t>((std::numeric_limits<int64_t>::max)()))
{
if (add_prefix)
{
@ -763,7 +768,13 @@ class binary_writer
JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n)));
}
}
else
// UBJSON: write number (signed integer)
template<typename NumberType, typename std::enable_if<
std::is_signed<NumberType>::value and
not std::is_floating_point<NumberType>::value, int>::type = 0>
void write_number_with_ubjson_prefix(const NumberType n,
const bool add_prefix)
{
if ((std::numeric_limits<int8_t>::min)() <= n and n <= (std::numeric_limits<int8_t>::max)())
{
@ -773,7 +784,7 @@ class binary_writer
}
write_number(static_cast<int8_t>(n));
}
else if ((std::numeric_limits<uint8_t>::min)() <= n and n <= (std::numeric_limits<uint8_t>::max)())
else if (static_cast<int64_t>((std::numeric_limits<uint8_t>::min)()) <= n and n <= static_cast<int64_t>((std::numeric_limits<uint8_t>::max)()))
{
if (add_prefix)
{
@ -812,7 +823,6 @@ class binary_writer
}
// LCOV_EXCL_STOP
}
}
/*!
@brief determine the type prefix of container values

View file

@ -6909,11 +6909,11 @@ class binary_writer
oa->write_characters(vec.data(), sizeof(NumberType));
}
template<typename NumberType>
// UBJSON: write number (floating point)
template<typename NumberType, typename std::enable_if<
std::is_floating_point<NumberType>::value, int>::type = 0>
void write_number_with_ubjson_prefix(const NumberType n,
const bool add_prefix)
{
if (std::is_floating_point<NumberType>::value)
{
if (add_prefix)
{
@ -6921,9 +6921,14 @@ class binary_writer
}
write_number(n);
}
else if (std::is_unsigned<NumberType>::value)
// UBJSON: write number (unsigned integer)
template<typename NumberType, typename std::enable_if<
std::is_unsigned<NumberType>::value, int>::type = 0>
void write_number_with_ubjson_prefix(const NumberType n,
const bool add_prefix)
{
if (n <= (std::numeric_limits<int8_t>::max)())
if (n <= static_cast<uint64_t>((std::numeric_limits<int8_t>::max)()))
{
if (add_prefix)
{
@ -6939,7 +6944,7 @@ class binary_writer
}
write_number(static_cast<uint8_t>(n));
}
else if (n <= (std::numeric_limits<int16_t>::max)())
else if (n <= static_cast<uint64_t>((std::numeric_limits<int16_t>::max)()))
{
if (add_prefix)
{
@ -6947,7 +6952,7 @@ class binary_writer
}
write_number(static_cast<int16_t>(n));
}
else if (n <= (std::numeric_limits<int32_t>::max)())
else if (n <= static_cast<uint64_t>((std::numeric_limits<int32_t>::max)()))
{
if (add_prefix)
{
@ -6955,7 +6960,7 @@ class binary_writer
}
write_number(static_cast<int32_t>(n));
}
else if (n <= (std::numeric_limits<int64_t>::max)())
else if (n <= static_cast<uint64_t>((std::numeric_limits<int64_t>::max)()))
{
if (add_prefix)
{
@ -6968,7 +6973,13 @@ class binary_writer
JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n)));
}
}
else
// UBJSON: write number (signed integer)
template<typename NumberType, typename std::enable_if<
std::is_signed<NumberType>::value and
not std::is_floating_point<NumberType>::value, int>::type = 0>
void write_number_with_ubjson_prefix(const NumberType n,
const bool add_prefix)
{
if ((std::numeric_limits<int8_t>::min)() <= n and n <= (std::numeric_limits<int8_t>::max)())
{
@ -6978,7 +6989,7 @@ class binary_writer
}
write_number(static_cast<int8_t>(n));
}
else if ((std::numeric_limits<uint8_t>::min)() <= n and n <= (std::numeric_limits<uint8_t>::max)())
else if (static_cast<int64_t>((std::numeric_limits<uint8_t>::min)()) <= n and n <= static_cast<int64_t>((std::numeric_limits<uint8_t>::max)()))
{
if (add_prefix)
{
@ -7017,7 +7028,6 @@ class binary_writer
}
// LCOV_EXCL_STOP
}
}
/*!
@brief determine the type prefix of container values