Skip to content

Commit

Permalink
optimize checkUpperLimit and checkLowerLimit in VarHolder.h avoid (#4072
Browse files Browse the repository at this point in the history
)

compile-time warnings when argument type is float and condition always
true

Co-authored-by: Alexander B <[email protected]>
  • Loading branch information
2 people authored and aleks-f committed Nov 27, 2023
1 parent 5ebd251 commit 0d2e3e8
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions Foundation/include/Poco/Dynamic/VarHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,8 @@ class Foundation_API VarHolder
poco_static_assert (std::numeric_limits<F>::is_signed);
poco_static_assert (std::numeric_limits<T>::is_signed);

if (std::numeric_limits<F>::is_integer)
{
checkUpperLimit<F,T>(from);
checkLowerLimit<F,T>(from);
}
else
{
checkUpperLimitFloat<F,T>(from);
checkLowerLimitFloat<F,T>(from);
}
checkUpperLimit<F,T>(from);
checkLowerLimit<F,T>(from);

to = static_cast<T>(from);
}
Expand Down Expand Up @@ -372,7 +364,7 @@ class Foundation_API VarHolder
to = static_cast<T>(from);
}

template <typename F, typename T>
template <typename F, typename T, std::enable_if_t<std::is_floating_point<F>::value, bool> = true>
void convertSignedFloatToUnsigned(const F& from, T& to) const
/// This function is meant for converting floating point data types to
/// unsigned integral data types. Negative values can not be converted and if one
Expand All @@ -387,7 +379,7 @@ class Foundation_API VarHolder

if (from < 0)
throw RangeException("Value too small.");
checkUpperLimitFloat<F,T>(from);
checkUpperLimit<F,T>(from);
to = static_cast<T>(from);
}

Expand All @@ -409,22 +401,22 @@ class Foundation_API VarHolder

private:

template <typename F, typename T>
template <typename F, typename T, std::enable_if_t<std::is_integral<F>::value, bool> = true>
void checkUpperLimit(const F& from) const
{
if (from > std::numeric_limits<T>::max())
throw RangeException("Value too large.");
}

template <typename F, typename T>
template <typename F, typename T, std::enable_if_t<std::is_integral<F>::value, bool> = true>
void checkLowerLimit(const F& from) const
{
if (from < std::numeric_limits<T>::min())
throw RangeException("Value too small.");
}

template <typename F, typename T>
void checkUpperLimitFloat(const F& from) const
template <typename F, typename T, std::enable_if_t<std::is_floating_point<F>::value, bool> = true>
void checkUpperLimit(const F& from) const
{
if (std::is_floating_point<T>::value)
{
Expand All @@ -439,8 +431,8 @@ class Foundation_API VarHolder
}
}

template <typename F, typename T>
void checkLowerLimitFloat(const F& from) const
template <typename F, typename T, std::enable_if_t<std::is_floating_point<F>::value, bool> = true>
void checkLowerLimit(const F& from) const
{
if (std::is_floating_point<T>::value)
{
Expand Down

0 comments on commit 0d2e3e8

Please sign in to comment.