/src/arduinojson/src/ArduinoJson/Numbers/arithmeticCompare.hpp
Line | Count | Source |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2025, Benoit BLANCHON |
3 | | // MIT License |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include <ArduinoJson/Numbers/JsonInteger.hpp> |
8 | | #include <ArduinoJson/Polyfills/type_traits.hpp> |
9 | | |
10 | | ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE |
11 | | |
12 | | enum CompareResult { |
13 | | COMPARE_RESULT_DIFFER = 0, |
14 | | COMPARE_RESULT_EQUAL = 1, |
15 | | COMPARE_RESULT_GREATER = 2, |
16 | | COMPARE_RESULT_LESS = 4, |
17 | | |
18 | | COMPARE_RESULT_GREATER_OR_EQUAL = 3, |
19 | | COMPARE_RESULT_LESS_OR_EQUAL = 5 |
20 | | }; |
21 | | |
22 | | template <typename T> |
23 | 0 | CompareResult arithmeticCompare(const T& lhs, const T& rhs) { |
24 | 0 | if (lhs < rhs) |
25 | 0 | return COMPARE_RESULT_LESS; |
26 | 0 | else if (lhs > rhs) |
27 | 0 | return COMPARE_RESULT_GREATER; |
28 | 0 | else |
29 | 0 | return COMPARE_RESULT_EQUAL; |
30 | 0 | } Unexecuted instantiation: ArduinoJson::V742HB42::detail::CompareResult ArduinoJson::V742HB42::detail::arithmeticCompare<double>(double const&, double const&) Unexecuted instantiation: ArduinoJson::V742HB42::detail::CompareResult ArduinoJson::V742HB42::detail::arithmeticCompare<long>(long const&, long const&) Unexecuted instantiation: ArduinoJson::V742HB42::detail::CompareResult ArduinoJson::V742HB42::detail::arithmeticCompare<unsigned long>(unsigned long const&, unsigned long const&) Unexecuted instantiation: ArduinoJson::V742HB42::detail::CompareResult ArduinoJson::V742HB42::detail::arithmeticCompare<bool>(bool const&, bool const&) |
31 | | |
32 | | template <typename T1, typename T2> |
33 | | CompareResult arithmeticCompare( |
34 | | const T1& lhs, const T2& rhs, |
35 | | enable_if_t<is_integral<T1>::value && is_integral<T2>::value && |
36 | 0 | sizeof(T1) < sizeof(T2)>* = 0) { |
37 | 0 | return arithmeticCompare<T2>(static_cast<T2>(lhs), rhs); |
38 | 0 | } Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIblEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXaaaasr11is_integralIS4_EE5valuesr11is_integralIS7_EE5valueltstS4_stS7_EvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIbmEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXaaaasr11is_integralIS4_EE5valuesr11is_integralIS7_EE5valueltstS4_stS7_EvE4typeE |
39 | | |
40 | | template <typename T1, typename T2> |
41 | | CompareResult arithmeticCompare( |
42 | | const T1& lhs, const T2& rhs, |
43 | | enable_if_t<is_integral<T1>::value && is_integral<T2>::value && |
44 | 0 | sizeof(T2) < sizeof(T1)>* = 0) { |
45 | 0 | return arithmeticCompare<T1>(lhs, static_cast<T1>(rhs)); |
46 | 0 | } Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIlbEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXaaaasr11is_integralIS4_EE5valuesr11is_integralIS7_EE5valueltstS7_stS4_EvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareImbEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXaaaasr11is_integralIS4_EE5valuesr11is_integralIS7_EE5valueltstS7_stS4_EvE4typeE |
47 | | |
48 | | template <typename T1, typename T2> |
49 | | CompareResult arithmeticCompare( |
50 | | const T1& lhs, const T2& rhs, |
51 | | enable_if_t<is_integral<T1>::value && is_integral<T2>::value && |
52 | | is_signed<T1>::value == is_signed<T2>::value && |
53 | | sizeof(T2) == sizeof(T1)>* = 0) { |
54 | | return arithmeticCompare<T1>(lhs, static_cast<T1>(rhs)); |
55 | | } |
56 | | |
57 | | template <typename T1, typename T2> |
58 | | CompareResult arithmeticCompare( |
59 | | const T1& lhs, const T2& rhs, |
60 | | enable_if_t<is_integral<T1>::value && is_integral<T2>::value && |
61 | | is_unsigned<T1>::value && is_signed<T2>::value && |
62 | 0 | sizeof(T2) == sizeof(T1)>* = 0) { |
63 | 0 | if (rhs < 0) |
64 | 0 | return COMPARE_RESULT_GREATER; |
65 | 0 | return arithmeticCompare<T1>(lhs, static_cast<T1>(rhs)); |
66 | 0 | } |
67 | | |
68 | | template <typename T1, typename T2> |
69 | | CompareResult arithmeticCompare( |
70 | | const T1& lhs, const T2& rhs, |
71 | | enable_if_t<is_integral<T1>::value && is_integral<T2>::value && |
72 | | is_signed<T1>::value && is_unsigned<T2>::value && |
73 | 0 | sizeof(T2) == sizeof(T1)>* = 0) { |
74 | 0 | if (lhs < 0) |
75 | 0 | return COMPARE_RESULT_LESS; |
76 | 0 | return arithmeticCompare<T2>(static_cast<T2>(lhs), rhs); |
77 | 0 | } |
78 | | |
79 | | template <typename T1, typename T2> |
80 | | CompareResult arithmeticCompare( |
81 | | const T1& lhs, const T2& rhs, |
82 | | enable_if_t<is_floating_point<T1>::value || is_floating_point<T2>::value>* = |
83 | 0 | 0) { |
84 | 0 | return arithmeticCompare<double>(static_cast<double>(lhs), |
85 | 0 | static_cast<double>(rhs)); |
86 | 0 | } Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIfdEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIldEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareImdEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIbdEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIflEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIdlEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIfmEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIdmEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIfbEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE Unexecuted instantiation: _ZN11ArduinoJson8V742HB426detail17arithmeticCompareIdbEENS1_13CompareResultERKT_RKT0_PNS1_9enable_ifIXoosr17is_floating_pointIS4_EE5valuesr17is_floating_pointIS7_EE5valueEvE4typeE |
87 | | |
88 | | template <typename T2> |
89 | | CompareResult arithmeticCompareNegateLeft( |
90 | | JsonUInt, const T2&, enable_if_t<is_unsigned<T2>::value>* = 0) { |
91 | | return COMPARE_RESULT_LESS; |
92 | | } |
93 | | |
94 | | template <typename T2> |
95 | | CompareResult arithmeticCompareNegateLeft( |
96 | | JsonUInt lhs, const T2& rhs, enable_if_t<is_signed<T2>::value>* = 0) { |
97 | | if (rhs > 0) |
98 | | return COMPARE_RESULT_LESS; |
99 | | return arithmeticCompare(-rhs, static_cast<T2>(lhs)); |
100 | | } |
101 | | |
102 | | template <typename T1> |
103 | | CompareResult arithmeticCompareNegateRight( |
104 | | const T1&, JsonUInt, enable_if_t<is_unsigned<T1>::value>* = 0) { |
105 | | return COMPARE_RESULT_GREATER; |
106 | | } |
107 | | |
108 | | template <typename T1> |
109 | | CompareResult arithmeticCompareNegateRight( |
110 | | const T1& lhs, JsonUInt rhs, enable_if_t<is_signed<T1>::value>* = 0) { |
111 | | if (lhs > 0) |
112 | | return COMPARE_RESULT_GREATER; |
113 | | return arithmeticCompare(static_cast<T1>(rhs), -lhs); |
114 | | } |
115 | | |
116 | | ARDUINOJSON_END_PRIVATE_NAMESPACE |