1 | // Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QCOMPARE_H |
5 | #define QCOMPARE_H |
6 | |
7 | #if 0 |
8 | #pragma qt_class(QtCompare) |
9 | #endif |
10 | |
11 | #include <QtCore/qglobal.h> |
12 | #include <QtCore/qcompare_impl.h> |
13 | |
14 | QT_BEGIN_NAMESPACE |
15 | |
16 | namespace QtPrivate { |
17 | using CompareUnderlyingType = qint8; |
18 | |
19 | // [cmp.categories.pre] / 1 |
20 | enum class Ordering : CompareUnderlyingType |
21 | { |
22 | Equal = 0, |
23 | Equivalent = Equal, |
24 | Less = -1, |
25 | Greater = 1 |
26 | }; |
27 | |
28 | enum class Uncomparable : CompareUnderlyingType |
29 | { |
30 | Unordered = -127 |
31 | }; |
32 | |
33 | } // namespace QtPrivate |
34 | |
35 | // [cmp.partialord] |
36 | class QPartialOrdering |
37 | { |
38 | public: |
39 | static const QPartialOrdering Less; |
40 | static const QPartialOrdering Equivalent; |
41 | static const QPartialOrdering Greater; |
42 | static const QPartialOrdering Unordered; |
43 | |
44 | friend constexpr bool operator==(QPartialOrdering lhs, |
45 | QtPrivate::CompareAgainstLiteralZero) noexcept |
46 | { return lhs.isOrdered() && lhs.m_order == 0; } |
47 | |
48 | friend constexpr bool operator!=(QPartialOrdering lhs, |
49 | QtPrivate::CompareAgainstLiteralZero) noexcept |
50 | { return lhs.isOrdered() && lhs.m_order != 0; } |
51 | |
52 | friend constexpr bool operator< (QPartialOrdering lhs, |
53 | QtPrivate::CompareAgainstLiteralZero) noexcept |
54 | { return lhs.isOrdered() && lhs.m_order < 0; } |
55 | |
56 | friend constexpr bool operator<=(QPartialOrdering lhs, |
57 | QtPrivate::CompareAgainstLiteralZero) noexcept |
58 | { return lhs.isOrdered() && lhs.m_order <= 0; } |
59 | |
60 | friend constexpr bool operator> (QPartialOrdering lhs, |
61 | QtPrivate::CompareAgainstLiteralZero) noexcept |
62 | { return lhs.isOrdered() && lhs.m_order > 0; } |
63 | |
64 | friend constexpr bool operator>=(QPartialOrdering lhs, |
65 | QtPrivate::CompareAgainstLiteralZero) noexcept |
66 | { return lhs.isOrdered() && lhs.m_order >= 0; } |
67 | |
68 | |
69 | friend constexpr bool operator==(QtPrivate::CompareAgainstLiteralZero, |
70 | QPartialOrdering rhs) noexcept |
71 | { return rhs.isOrdered() && 0 == rhs.m_order; } |
72 | |
73 | friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, |
74 | QPartialOrdering rhs) noexcept |
75 | { return rhs.isOrdered() && 0 != rhs.m_order; } |
76 | |
77 | friend constexpr bool operator< (QtPrivate::CompareAgainstLiteralZero, |
78 | QPartialOrdering rhs) noexcept |
79 | { return rhs.isOrdered() && 0 < rhs.m_order; } |
80 | |
81 | friend constexpr bool operator<=(QtPrivate::CompareAgainstLiteralZero, |
82 | QPartialOrdering rhs) noexcept |
83 | { return rhs.isOrdered() && 0 <= rhs.m_order; } |
84 | |
85 | friend constexpr bool operator> (QtPrivate::CompareAgainstLiteralZero, |
86 | QPartialOrdering rhs) noexcept |
87 | { return rhs.isOrdered() && 0 > rhs.m_order; } |
88 | |
89 | friend constexpr bool operator>=(QtPrivate::CompareAgainstLiteralZero, |
90 | QPartialOrdering rhs) noexcept |
91 | { return rhs.isOrdered() && 0 >= rhs.m_order; } |
92 | |
93 | |
94 | friend constexpr bool operator==(QPartialOrdering lhs, QPartialOrdering rhs) noexcept |
95 | { return lhs.m_order == rhs.m_order; } |
96 | |
97 | friend constexpr bool operator!=(QPartialOrdering lhs, QPartialOrdering rhs) noexcept |
98 | { return lhs.m_order != rhs.m_order; } |
99 | |
100 | private: |
101 | constexpr explicit QPartialOrdering(QtPrivate::Ordering order) noexcept |
102 | : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order)) |
103 | {} |
104 | constexpr explicit QPartialOrdering(QtPrivate::Uncomparable order) noexcept |
105 | : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order)) |
106 | {} |
107 | |
108 | // instead of the exposition only is_ordered member in [cmp.partialord], |
109 | // use a private function |
110 | constexpr bool isOrdered() noexcept |
111 | { return m_order != static_cast<QtPrivate::CompareUnderlyingType>(QtPrivate::Uncomparable::Unordered); } |
112 | |
113 | QtPrivate::CompareUnderlyingType m_order; |
114 | }; |
115 | |
116 | inline constexpr QPartialOrdering QPartialOrdering::Less(QtPrivate::Ordering::Less); |
117 | inline constexpr QPartialOrdering QPartialOrdering::Equivalent(QtPrivate::Ordering::Equivalent); |
118 | inline constexpr QPartialOrdering QPartialOrdering::Greater(QtPrivate::Ordering::Greater); |
119 | inline constexpr QPartialOrdering QPartialOrdering::Unordered(QtPrivate::Uncomparable::Unordered); |
120 | |
121 | QT_END_NAMESPACE |
122 | |
123 | #endif // QCOMPARE_H |
124 | |