swift
Loading...
Searching...
No Matches
mixincompare.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_MIXIN_MIXINCOMPARE_H
7#define SWIFT_MISC_MIXIN_MIXINCOMPARE_H
8
9#include <QList>
10#include <QPair>
11#include <QString>
12
14#include "misc/metaclass.h"
15#include "misc/typetraits.h"
16
17namespace swift::misc
18{
19 class CEmpty;
20
21 namespace mixin
22 {
23
30 template <class Derived>
32 {
33 public:
35 friend bool operator==(const Derived &a, const Derived &b) { return compare(a, b) == 0; }
36
38 friend bool operator!=(const Derived &a, const Derived &b) { return compare(a, b) != 0; }
39 };
40
44 template <class Derived>
46 {
47 public:
49 friend bool operator==(const Derived &a, const Derived &b) { return equals(a, b); }
50
52 friend bool operator!=(const Derived &a, const Derived &b) { return !equals(a, b); }
53
54 private:
55 static bool equals(const Derived &a, const Derived &b);
56 template <typename T>
57 static bool baseEquals(const T *a, const T *b);
58 static bool baseEquals(const void *, const void *);
59 static bool baseEquals(const CEmpty *, const CEmpty *);
60 template <typename T, typename Flags>
61 static bool membersEqual(const T &a, const T &b, Flags);
62 };
63
64 template <class Derived>
65 bool EqualsByMetaClass<Derived>::equals(const Derived &a, const Derived &b)
66 {
67 bool result =
68 baseEquals(static_cast<const TBaseOfT<Derived> *>(&a), static_cast<const TBaseOfT<Derived> *>(&b));
69 introspect<Derived>().forEachMember([&](auto member) {
70 if constexpr (!decltype(member)::has(MetaFlags<DisabledForComparison>()))
71 {
72 result = result && EqualsByMetaClass::membersEqual(member.in(a), member.in(b), member.m_flags);
73 }
74 });
75 return result;
76 }
77
78 template <class Derived>
79 template <typename T>
80 bool EqualsByMetaClass<Derived>::baseEquals(const T *a, const T *b)
81 {
82 return *a == *b;
83 }
84
85 template <class Derived>
86 bool EqualsByMetaClass<Derived>::baseEquals(const void *, const void *)
87 {
88 return true;
89 }
90
91 template <class Derived>
92 bool EqualsByMetaClass<Derived>::baseEquals(const CEmpty *, const CEmpty *)
93 {
94 return true;
95 }
96
97 template <class Derived>
98 template <typename T, typename Flags>
99 bool EqualsByMetaClass<Derived>::membersEqual(const T &a, const T &b, Flags)
100 {
101 if constexpr (static_cast<bool>(Flags::value & CaseInsensitiveComparison))
102 {
103 return a.compare(b, Qt::CaseInsensitive) == 0;
104 }
105 else
106 {
107 return a == b;
108 }
109 }
110
117 template <class Derived>
119 {
120 public:
122 friend bool operator<(const Derived &a, const Derived &b) { return compare(a, b) < 0; }
123
125 friend bool operator>(const Derived &a, const Derived &b) { return compare(a, b) > 0; }
126
128 friend bool operator<=(const Derived &a, const Derived &b) { return compare(a, b) <= 0; }
129
131 friend bool operator>=(const Derived &a, const Derived &b) { return compare(a, b) >= 0; }
132 };
133
137 template <class Derived>
139 {
140 public:
142 friend bool operator<(const Derived &a, const Derived &b) { return less(a, b); }
143
145 friend bool operator>(const Derived &a, const Derived &b) { return less(b, a); }
146
148 friend bool operator<=(const Derived &a, const Derived &b) { return !less(b, a); }
149
151 friend bool operator>=(const Derived &a, const Derived &b) { return !less(a, b); }
152
153 private:
154 static bool less(const Derived &a, const Derived &b);
155 template <typename T>
156 static bool baseLess(const T *a, const T *b);
157 static bool baseLess(const void *, const void *);
158 static bool baseLess(const CEmpty *, const CEmpty *);
159 template <typename T, typename Flags>
160 static bool membersLess(bool &io_greaterThan, const T &a, const T &b, Flags);
161 };
162
163 template <class Derived>
164 bool LessThanByMetaClass<Derived>::less(const Derived &a, const Derived &b)
165 {
166 bool result =
167 baseLess(static_cast<const TBaseOfT<Derived> *>(&a), static_cast<const TBaseOfT<Derived> *>(&b));
168 bool gt = baseLess(static_cast<const TBaseOfT<Derived> *>(&b), static_cast<const TBaseOfT<Derived> *>(&a));
169 introspect<Derived>().forEachMember([&](auto member) {
170 if constexpr (!decltype(member)::has(MetaFlags<DisabledForComparison>()))
171 {
172 result = result || LessThanByMetaClass::membersLess(gt, member.in(a), member.in(b), member.m_flags);
173 }
174 });
175 return result;
176 }
177
178 template <class Derived>
179 template <typename T>
180 bool LessThanByMetaClass<Derived>::baseLess(const T *a, const T *b)
181 {
182 return *a < *b;
183 }
184
185 template <class Derived>
186 bool LessThanByMetaClass<Derived>::baseLess(const void *, const void *)
187 {
188 return false;
189 }
190
191 template <class Derived>
192 bool LessThanByMetaClass<Derived>::baseLess(const CEmpty *, const CEmpty *)
193 {
194 return false;
195 }
196
197 template <class Derived>
198 template <typename T, typename Flags>
199 bool LessThanByMetaClass<Derived>::membersLess(bool &io_greaterThan, const T &a, const T &b, Flags)
200 {
201 if (io_greaterThan)
202 {
203 return false;
204 }
205 if constexpr (static_cast<bool>(Flags::value & CaseInsensitiveComparison))
206 {
207 io_greaterThan = b.compare(a, Qt::CaseInsensitive) < 0;
208 return a.compare(b, Qt::CaseInsensitive) < 0;
209 }
210 else
211 {
212 io_greaterThan = b < a;
213 return a < b;
214 }
215 }
216
220 template <class Derived>
222 {
223 public:
225 friend int compare(const Derived &a, const Derived &b) { return compareImpl(a, b); }
226
227 private:
228 static int compareImpl(const Derived &a, const Derived &b);
229 template <typename T>
230 static int baseCompare(const T *a, const T *b);
231 static int baseCompare(const void *, const void *);
232 static int baseCompare(const CEmpty *, const CEmpty *);
233 template <typename T, typename Flags>
234 static int membersCompare(const T &a, const T &b, Flags);
235 };
236
237 template <class Derived>
238 int CompareByMetaClass<Derived>::compareImpl(const Derived &a, const Derived &b)
239 {
240 int result =
241 baseCompare(static_cast<const TBaseOfT<Derived> *>(&a), static_cast<const TBaseOfT<Derived> *>(&b));
242 introspect<Derived>().forEachMember([&](auto member) {
243 if constexpr (!decltype(member)::has(MetaFlags<DisabledForComparison>()))
244 {
245 result = result ? result :
246 CompareByMetaClass::membersCompare(member.in(a), member.in(b), member.m_flags);
247 }
248 });
249 return result;
250 }
251
252 template <class Derived>
253 template <typename T>
254 int CompareByMetaClass<Derived>::baseCompare(const T *a, const T *b)
255 {
256 return compare(*a, *b);
257 }
258
259 template <class Derived>
260 int CompareByMetaClass<Derived>::baseCompare(const void *, const void *)
261 {
262 return 0;
263 }
264
265 template <class Derived>
266 int CompareByMetaClass<Derived>::baseCompare(const CEmpty *, const CEmpty *)
267 {
268 return 0;
269 }
270
271 template <class Derived>
272 template <typename T, typename Flags>
273 int CompareByMetaClass<Derived>::membersCompare(const T &a, const T &b, Flags)
274 {
275 if constexpr (static_cast<bool>(Flags::value & CaseInsensitiveComparison))
276 {
277 return a.compare(b, Qt::CaseInsensitive);
278 }
279 else if constexpr (THasCompare<T, T>::value)
280 {
281 return compare(a, b);
282 }
283 else
284 {
285 return a < b ? -1 : b < a ? 1 : 0;
286 }
287 }
288
289 } // namespace mixin
290} // namespace swift::misc
291
292#endif // SWIFT_MISC_MIXIN_MIXINCOMPARE_H
Default base class for CValueObject.
Definition valueobject.h:76
CRTP class template from which a derived class can inherit non-member compare() implemented by metacl...
friend int compare(const Derived &a, const Derived &b)
Return negative, zero, or positive if a is less than, equal to, or greater than b.
CRTP class template from which a derived class can inherit operator== implemented using its compare f...
friend bool operator!=(const Derived &a, const Derived &b)
Not equal.
friend bool operator==(const Derived &a, const Derived &b)
Equals.
CRTP class template from which a derived class can inherit operator== implemented by metaclass.
friend bool operator!=(const Derived &a, const Derived &b)
Not equal.
friend bool operator==(const Derived &a, const Derived &b)
Equals.
CRTP class template from which a derived class can inherit operator< implemented using its compare fu...
friend bool operator>(const Derived &a, const Derived &b)
Greater than.
friend bool operator<=(const Derived &a, const Derived &b)
Less than or equal.
friend bool operator<(const Derived &a, const Derived &b)
Less than.
friend bool operator>=(const Derived &a, const Derived &b)
Greater than or equal.
CRTP class template from which a derived class can inherit operator< implemented by metaclass.
friend bool operator>(const Derived &a, const Derived &b)
Greater than.
friend bool operator<=(const Derived &a, const Derived &b)
Less than or equal.
friend bool operator<(const Derived &a, const Derived &b)
Less than.
friend bool operator>=(const Derived &a, const Derived &b)
Greater than or equal.
int compare(T a, T b)
Compare arithmetic values.
constexpr auto introspect()
Obtain the CMetaClassIntrospector for the metaclass of T.
Definition metaclass.h:285
@ CaseInsensitiveComparison
Element will be compared case insensitively (must be a QString).
Definition metaclass.h:205
Mixin classes which implement common operations for value classes.
Free functions in swift::misc.
typename TBaseOf< T >::type TBaseOfT
Alias for typename TBaseOf<T>::type.
CaseInsensitive
Type wrapper for passing MetaFlag to CMetaMember::has.
Definition metaclass.h:117