1 /*
2  * DQt - D bindings for the Qt Toolkit
3  *
4  * GNU Lesser General Public License Usage
5  * This file may be used under the terms of the GNU Lesser
6  * General Public License version 3 as published by the Free Software
7  * Foundation and appearing in the file LICENSE.LGPL3 included in the
8  * packaging of this file. Please review the following information to
9  * ensure the GNU Lesser General Public License version 3 requirements
10  * will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
11  */
12 module qt.core.compare;
13 extern(C++):
14 
15 import qt.config;
16 import qt.core.global;
17 import qt.helpers;
18 
19 /+ #if 0
20 #pragma qt_class(QtCompare)
21 #endif +/
22 
23 
24 extern(C++, "QtPrivate") {
25 alias CompareUnderlyingType = qint8;
26 
27 // [cmp.categories.pre] / 1
28 enum /+ class +/ Ordering : CompareUnderlyingType
29 {
30     Equal = 0,
31     Equivalent = Ordering.Equal,
32     Less = -1,
33     Greater = 1
34 }
35 
36 enum /+ class +/ Uncomparable : CompareUnderlyingType
37 {
38     Unordered = -127
39 }
40 
41 // [cmp.categories.pre] / 3, but using a safe bool trick
42 // and also rejecting std::nullptr_t (unlike the example)
43 extern(C++, class) struct CompareAgainstLiteralZero {
44 public:
45     alias SafeZero = ExternCPPFunc!(void function())/+ CompareAgainstLiteralZero::* +/;
46     /+ Q_IMPLICIT +/ this(SafeZero)/+ noexcept+/ {}
47 
48     /+ template <typename T, std::enable_if_t<!std::is_same_v<T, int>, bool> = false> +/
49     this(T,)(T) /+ = delete +/;
50 }
51 } // namespace QtPrivate
52 
53 // [cmp.partialord]
54 /// Binding for C++ class [QPartialOrdering](https://doc.qt.io/qt-6/qpartialordering.html).
55 extern(C++, class) struct QPartialOrdering
56 {
57 public:
58     extern(D) static immutable(QPartialOrdering) Less = QPartialOrdering(Ordering.Less);
59     extern(D) static immutable(QPartialOrdering) Equivalent = QPartialOrdering(Ordering.Equivalent);
60     extern(D) static immutable(QPartialOrdering) Greater = QPartialOrdering(Ordering.Greater);
61     extern(D) static immutable(QPartialOrdering) Unordered = QPartialOrdering(Uncomparable.Unordered);
62 
63     /+ friend constexpr bool operator==(QPartialOrdering p, QtPrivate::CompareAgainstLiteralZero) noexcept
64     { return p.isOrdered() && p.m_order == 0; } +/
65     /+ friend constexpr bool operator!=(QPartialOrdering p, QtPrivate::CompareAgainstLiteralZero) noexcept
66     { return p.isOrdered() && p.m_order != 0; } +/
67     /+ friend constexpr bool operator< (QPartialOrdering p, QtPrivate::CompareAgainstLiteralZero) noexcept
68     { return p.isOrdered() && p.m_order <  0; } +/
69     /+ friend constexpr bool operator<=(QPartialOrdering p, QtPrivate::CompareAgainstLiteralZero) noexcept
70     { return p.isOrdered() && p.m_order <= 0; } +/
71     /+ friend constexpr bool operator> (QPartialOrdering p, QtPrivate::CompareAgainstLiteralZero) noexcept
72     { return p.isOrdered() && p.m_order >  0; } +/
73     /+ friend constexpr bool operator>=(QPartialOrdering p, QtPrivate::CompareAgainstLiteralZero) noexcept
74     { return p.isOrdered() && p.m_order >= 0; } +/
75 
76     /+ friend constexpr bool operator==(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering p) noexcept
77     { return p.isOrdered() && 0 == p.m_order; } +/
78     /+ friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering p) noexcept
79     { return p.isOrdered() && 0 != p.m_order; } +/
80     /+ friend constexpr bool operator< (QtPrivate::CompareAgainstLiteralZero, QPartialOrdering p) noexcept
81     { return p.isOrdered() && 0 <  p.m_order; } +/
82     /+ friend constexpr bool operator<=(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering p) noexcept
83     { return p.isOrdered() && 0 <= p.m_order; } +/
84     /+ friend constexpr bool operator> (QtPrivate::CompareAgainstLiteralZero, QPartialOrdering p) noexcept
85     { return p.isOrdered() && 0 >  p.m_order; } +/
86     /+ friend constexpr bool operator>=(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering p) noexcept
87     { return p.isOrdered() && 0 >= p.m_order; } +/
88 
89     /+ friend constexpr bool operator==(QPartialOrdering p1, QPartialOrdering p2) noexcept
90     { return p1.m_order == p2.m_order; } +/
91     /+ friend constexpr bool operator!=(QPartialOrdering p1, QPartialOrdering p2) noexcept
92     { return p1.m_order != p2.m_order; } +/
93 
94 private:
95     /+ explicit +/this(/+ QtPrivate:: +/Ordering order)/+ noexcept+/
96     {
97         this.m_order = static_cast!(/+ QtPrivate:: +/CompareUnderlyingType)(order);
98     }
99     /+ explicit +/this(/+ QtPrivate:: +/Uncomparable order)/+ noexcept+/
100     {
101         this.m_order = static_cast!(/+ QtPrivate:: +/CompareUnderlyingType)(order);
102     }
103 
104     // instead of the exposition only is_ordered member in [cmp.partialord],
105     // use a private function
106     bool isOrdered()/+ noexcept+/
107     { return m_order != static_cast!(/+ QtPrivate:: +/CompareUnderlyingType)(/+ QtPrivate:: +/Uncomparable.Unordered); }
108 
109     /+ QtPrivate:: +/CompareUnderlyingType m_order;
110     mixin(CREATE_CONVENIENCE_WRAPPERS);
111 }
112 
113