// ArduinoJson - https://arduinojson.org // Copyright Benoit Blanchon 2014-2021 // MIT License #pragma once #include "lib/ArduinoJson/Numbers/Integer.hpp" #include "lib/ArduinoJson/Polyfills/type_traits.hpp" namespace ARDUINOJSON_NAMESPACE { enum CompareResult { COMPARE_RESULT_DIFFER = 0, COMPARE_RESULT_EQUAL = 1, COMPARE_RESULT_GREATER = 2, COMPARE_RESULT_LESS = 4, COMPARE_RESULT_GREATER_OR_EQUAL = 3, COMPARE_RESULT_LESS_OR_EQUAL = 5 }; template CompareResult arithmeticCompare(const T &lhs, const T &rhs) { if (lhs < rhs) return COMPARE_RESULT_LESS; else if (lhs > rhs) return COMPARE_RESULT_GREATER; else return COMPARE_RESULT_EQUAL; } template CompareResult arithmeticCompare( const T1 &lhs, const T2 &rhs, typename enable_if::value && is_integral::value && sizeof(T1) < sizeof(T2), int // Using int instead of void to avoid C2572 on // Visual Studio 2012, 2013, and 2015 >::type * = 0) { return arithmeticCompare(static_cast(lhs), rhs); } template CompareResult arithmeticCompare( const T1 &lhs, const T2 &rhs, typename enable_if::value && is_integral::value && sizeof(T2) < sizeof(T1)>::type * = 0) { return arithmeticCompare(lhs, static_cast(rhs)); } template CompareResult arithmeticCompare( const T1 &lhs, const T2 &rhs, typename enable_if::value && is_integral::value && is_signed::value == is_signed::value && sizeof(T2) == sizeof(T1)>::type * = 0) { return arithmeticCompare(lhs, static_cast(rhs)); } template CompareResult arithmeticCompare( const T1 &lhs, const T2 &rhs, typename enable_if::value && is_integral::value && is_unsigned::value && is_signed::value && sizeof(T2) == sizeof(T1)>::type * = 0) { if (rhs < 0) return COMPARE_RESULT_GREATER; return arithmeticCompare(lhs, static_cast(rhs)); } template CompareResult arithmeticCompare( const T1 &lhs, const T2 &rhs, typename enable_if::value && is_integral::value && is_signed::value && is_unsigned::value && sizeof(T2) == sizeof(T1)>::type * = 0) { if (lhs < 0) return COMPARE_RESULT_LESS; return arithmeticCompare(static_cast(lhs), rhs); } template CompareResult arithmeticCompare( const T1 &lhs, const T2 &rhs, typename enable_if::value || is_floating_point::value>::type * = 0) { return arithmeticCompare(static_cast(lhs), static_cast(rhs)); } template CompareResult arithmeticCompareNegateLeft( UInt, const T2 &, typename enable_if::value>::type * = 0) { return COMPARE_RESULT_LESS; } template CompareResult arithmeticCompareNegateLeft( UInt lhs, const T2 &rhs, typename enable_if::value>::type * = 0) { if (rhs > 0) return COMPARE_RESULT_LESS; return arithmeticCompare(-rhs, static_cast(lhs)); } template CompareResult arithmeticCompareNegateRight( const T1 &, UInt, typename enable_if::value>::type * = 0) { return COMPARE_RESULT_GREATER; } template CompareResult arithmeticCompareNegateRight( const T1 &lhs, UInt rhs, typename enable_if::value>::type * = 0) { if (lhs > 0) return COMPARE_RESULT_GREATER; return arithmeticCompare(static_cast(rhs), -lhs); } } // namespace ARDUINOJSON_NAMESPACE