Add specialization for SafeConvert

This commit is contained in:
Jeffrey Walton 2023-06-23 00:13:16 -04:00
parent dced966b7a
commit df86e69902
No known key found for this signature in database
GPG Key ID: B36AB348921B1838

50
misc.h
View File

@ -707,7 +707,9 @@ template <class T1, class T2> inline const T1 UnsignedMin(const T1& a, const T2&
/// \tparam T2 class or type
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from into to, false otherwise.
/// \return true if its safe to convert \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
template <class T1, class T2>
inline bool SafeConvert(T1 from, T2 &to)
{
@ -717,6 +719,52 @@ inline bool SafeConvert(T1 from, T2 &to)
return true;
}
/// \brief Tests whether a conversion from -> to is safe to perform
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word32 from, word64 &to)
{
to = static_cast<word64>(from);
return true;
}
/// \brief Tests whether a conversion from -> to is safe to perform
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word64 from, sword64 &to)
{
if (from > static_cast<word64>(std::numeric_limits<sword64>::max()))
return false;
to = static_cast<sword64>(from);
return true;
}
/// \brief Tests whether a conversion from -> to is safe to perform
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(sword64 from, word64 &to)
{
if (from < 0)
return false;
to = static_cast<sword64>(from);
return true;
}
/// \brief Converts a value to a string
/// \tparam T class or type
/// \param value the value to convert