Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qversionnumber.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2022 Intel Corporation.
3// Copyright (C) 2015 Keith Gardner <kreios4004@gmail.com>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
6#ifndef QVERSIONNUMBER_H
7#define QVERSIONNUMBER_H
8
9#include <QtCore/qcompare.h>
10#include <QtCore/qcontainertools_impl.h>
11#include <QtCore/qlist.h>
12#include <QtCore/qmetatype.h>
13#include <QtCore/qnamespace.h>
14#include <QtCore/qspan.h>
15#include <QtCore/qstring.h>
16#include <QtCore/qtypeinfo.h>
17#if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 2
18#include <QtCore/qtyperevision.h>
19#endif // lean headers level 2
20
21QT_BEGIN_NAMESPACE
22
23class QVersionNumber;
24Q_CORE_EXPORT size_t qHash(const QVersionNumber &key, size_t seed = 0);
25
26#ifndef QT_NO_DATASTREAM
27Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QVersionNumber &version);
28Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QVersionNumber &version);
29#endif
30
32{
33 /*
34 * QVersionNumber stores small values inline, without memory allocation.
35 * We do that by setting the LSB in the pointer that would otherwise hold
36 * the longer form of the segments.
37 * The constants below help us deal with the permutations for 32- and 64-bit,
38 * little- and big-endian architectures.
39 */
40 enum {
41 // in little-endian, inline_segments[0] is shared with the pointer's LSB, while
42 // in big-endian, it's inline_segments[7]
43 InlineSegmentMarker = Q_BYTE_ORDER == Q_LITTLE_ENDIAN ? 0 : sizeof(void *) - 1,
44 InlineSegmentStartIdx = !InlineSegmentMarker, // 0 for BE, 1 for LE
45 InlineSegmentCount = sizeof(void *) - 1
46 };
47 static_assert(InlineSegmentCount >= 3); // at least major, minor, micro
48
49 struct SegmentStorage
50 {
51 // Note: we alias the use of dummy and inline_segments in the use of the
52 // union below. This is undefined behavior in C++98, but most compilers implement
53 // the C++11 behavior. The one known exception is older versions of Sun Studio.
54 union {
55 quintptr dummy;
56 qint8 inline_segments[sizeof(void *)];
57 QList<int> *pointer_segments;
58 };
59
60 // set the InlineSegmentMarker and set length to zero
61 SegmentStorage() noexcept : dummy(1) {}
62
63 SegmentStorage(const QList<int> &seg)
64 {
65 if (dataFitsInline(seg.data(), seg.size()))
66 setInlineData(seg.data(), seg.size());
67 else
68 setListData(seg);
69 }
70
71 Q_CORE_EXPORT void setListData(const QList<int> &seg);
72
73 SegmentStorage(const SegmentStorage &other)
74 {
75 if (other.isUsingPointer())
76 setListData(*other.pointer_segments);
77 else
78 dummy = other.dummy;
79 }
80
81 SegmentStorage &operator=(const SegmentStorage &other)
82 {
83 if (isUsingPointer() && other.isUsingPointer()) {
84 *pointer_segments = *other.pointer_segments;
85 } else if (other.isUsingPointer()) {
86 setListData(*other.pointer_segments);
87 } else {
88 if (isUsingPointer())
89 delete pointer_segments;
90 dummy = other.dummy;
91 }
92 return *this;
93 }
94
95 SegmentStorage(SegmentStorage &&other) noexcept
96 : dummy(other.dummy)
97 {
98 other.dummy = 1;
99 }
100
101 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(SegmentStorage)
102
103 void swap(SegmentStorage &other) noexcept
104 {
105 std::swap(dummy, other.dummy);
106 }
107
108 explicit SegmentStorage(QList<int> &&seg)
109 {
110 if (dataFitsInline(std::as_const(seg).data(), seg.size()))
111 setInlineData(std::as_const(seg).data(), seg.size());
112 else
113 setListData(std::move(seg));
114 }
115
116 Q_CORE_EXPORT void setListData(QList<int> &&seg);
117
118 explicit SegmentStorage(QSpan<const int> args)
119 : SegmentStorage(args.begin(), args.end()) {}
120
121 explicit SegmentStorage(const int *first, const int *last)
122 {
123 if (dataFitsInline(first, last - first)) {
124 setInlineData(first, last - first);
125 } else {
126 setListData(first, last);
127 }
128 }
129
130 Q_CORE_EXPORT void setListData(const int *first, const int *last);
131
132 ~SegmentStorage() { if (isUsingPointer()) delete pointer_segments; }
133
134 bool isUsingPointer() const noexcept
135 { return (inline_segments[InlineSegmentMarker] & 1) == 0; }
136
137 qsizetype size() const noexcept
138 { return isUsingPointer() ? pointer_segments->size() : (inline_segments[InlineSegmentMarker] >> 1); }
139
140 void setInlineSize(qsizetype len)
141 {
142 Q_ASSERT(len <= InlineSegmentCount);
143 inline_segments[InlineSegmentMarker] = qint8(1 + 2 * len);
144 }
145
146 Q_CORE_EXPORT void resize(qsizetype len);
147
148 int at(qsizetype index) const
149 {
150 return isUsingPointer() ?
151 pointer_segments->at(index) :
152 inline_segments[InlineSegmentStartIdx + index];
153 }
154
155 void setSegments(int len, int maj, int min = 0, int mic = 0)
156 {
157 if (maj == qint8(maj) && min == qint8(min) && mic == qint8(mic)) {
158 int data[] = { maj, min, mic };
159 setInlineData(data, len);
160 } else {
161 setVector(len, maj, min, mic);
162 }
163 }
164
165 private:
166 static bool dataFitsInline(const int *data, qsizetype len)
167 {
168 if (len > InlineSegmentCount)
169 return false;
170 for (qsizetype i = 0; i < len; ++i)
171 if (data[i] != qint8(data[i]))
172 return false;
173 return true;
174 }
175 void setInlineData(const int *data, qsizetype len)
176 {
177 Q_ASSERT(len <= InlineSegmentCount);
178 dummy = 1 + len * 2;
179#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
180 for (qsizetype i = 0; i < len; ++i)
181 dummy |= quintptr(data[i] & 0xFF) << (8 * (i + 1));
182#elif Q_BYTE_ORDER == Q_BIG_ENDIAN
183 for (qsizetype i = 0; i < len; ++i)
184 dummy |= quintptr(data[i] & 0xFF) << (8 * (sizeof(void *) - i - 1));
185#else
186 // the code above is equivalent to:
187 setInlineSize(len);
188 for (qsizetype i = 0; i < len; ++i)
189 inline_segments[InlineSegmentStartIdx + i] = data[i] & 0xFF;
190#endif
191 }
192
193 Q_CORE_EXPORT void setVector(int len, int maj, int min, int mic);
194 } m_segments;
195
196 class It
197 {
198 const QVersionNumber *v;
199 qsizetype i;
200
201 friend class QVersionNumber;
202 explicit constexpr It(const QVersionNumber *vn, qsizetype idx) noexcept : v(vn), i(idx) {}
203
204 friend constexpr bool comparesEqual(const It &lhs, const It &rhs)
205 { Q_ASSERT(lhs.v == rhs.v); return lhs.i == rhs.i; }
206 friend constexpr Qt::strong_ordering compareThreeWay(const It &lhs, const It &rhs)
207 { Q_ASSERT(lhs.v == rhs.v); return Qt::compareThreeWay(lhs.i, rhs.i); }
208 // macro variant does not exist: Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE_NON_NOEXCEPT(It)
209 friend constexpr bool operator==(It lhs, It rhs) {
210 return comparesEqual(lhs, rhs);
211 }
212#ifdef __cpp_lib_three_way_comparison
213 friend constexpr std::strong_ordering operator<=>(It lhs, It rhs) {
214 return compareThreeWay(lhs, rhs);
215 }
216#else
217 friend constexpr bool operator!=(It lhs, It rhs) {
218 return !operator==(lhs, rhs);
219 }
220 friend constexpr bool operator<(It lhs, It rhs) {
221 return is_lt(compareThreeWay(lhs, rhs));
222 }
223 friend constexpr bool operator<=(It lhs, It rhs) {
224 return is_lteq(compareThreeWay(lhs, rhs));
225 }
226 friend constexpr bool operator>(It lhs, It rhs) {
227 return is_gt(compareThreeWay(lhs, rhs));
228 }
229 friend constexpr bool operator>=(It lhs, It rhs) {
230 return is_gteq(compareThreeWay(lhs, rhs));
231 }
232#endif
233
234 public:
235 // Rule Of Zero applies
236 It() = default;
237
238 using iterator_category = std::random_access_iterator_tag;
239 using value_type = int;
240#ifdef QT_COMPILER_HAS_LWG3346
241 using element_type = const int;
242#endif
243 using difference_type = qptrdiff; // difference to container requirements
244 using size_type = qsizetype; // difference to container requirements
245 using reference = value_type; // difference to container requirements
246 using pointer = QtPrivate::ArrowProxy<reference>;
247
248 reference operator*() const { return v->segmentAt(i); }
249 pointer operator->() const { return {**this}; }
250
251 It &operator++() { ++i; return *this; }
252 It operator++(int) { auto copy = *this; ++*this; return copy; }
253
254 It &operator--() { --i; return *this; }
255 It operator--(int) { auto copy = *this; --*this; return copy; }
256
257 It &operator+=(difference_type n) { i += n; return *this; }
258 friend It operator+(It it, difference_type n) { it += n; return it; }
259 friend It operator+(difference_type n, It it) { return it + n; }
260
261 It &operator-=(difference_type n) { i -= n; return *this; }
262 friend It operator-(It it, difference_type n) { it -= n; return it; }
263
264 friend difference_type operator-(It lhs, It rhs)
265 { Q_ASSERT(lhs.v == rhs.v); return lhs.i - rhs.i; }
266
267 reference operator[](difference_type n) const { return *(*this + n); }
268 };
269
270public:
271 using const_iterator = It;
273
274 using value_type = It::value_type;
277 using reference = It::reference;
278 using const_reference = reference;
281
282 inline QVersionNumber() noexcept
283 : m_segments()
284 {}
286 inline explicit QVersionNumber(const QList<int> &seg) : m_segments(seg) { }
287
288 // compiler-generated copy/move ctor/assignment operators and the destructor are ok
289
291 explicit QVersionNumber(QList<int> &&seg) : m_segments(std::move(seg)) { }
292
293 inline QVersionNumber(std::initializer_list<int> args)
295 {}
296
297 explicit QVersionNumber(QSpan<const int> args)
299 {}
300
301 inline explicit QVersionNumber(int maj)
302 { m_segments.setSegments(1, maj); }
303
304 inline explicit QVersionNumber(int maj, int min)
305 { m_segments.setSegments(2, maj, min); }
306
307 inline explicit QVersionNumber(int maj, int min, int mic)
308 { m_segments.setSegments(3, maj, min, mic); }
309
310 [[nodiscard]] inline bool isNull() const noexcept
311 { return segmentCount() == 0; }
312
313 [[nodiscard]] inline bool isNormalized() const noexcept
314 { return isNull() || segmentAt(segmentCount() - 1) != 0; }
315
316 [[nodiscard]] inline int majorVersion() const noexcept
317 { return segmentAt(0); }
318
319 [[nodiscard]] inline int minorVersion() const noexcept
320 { return segmentAt(1); }
321
322 [[nodiscard]] inline int microVersion() const noexcept
323 { return segmentAt(2); }
324
326
327 [[nodiscard]] Q_CORE_EXPORT QList<int> segments() const;
328
329 [[nodiscard]] inline int segmentAt(qsizetype index) const noexcept
330 { return (m_segments.size() > index) ? m_segments.at(index) : 0; }
331
332 [[nodiscard]] inline qsizetype segmentCount() const noexcept
333 { return m_segments.size(); }
334
335 [[nodiscard]] const_iterator begin() const noexcept { return const_iterator{this, 0}; }
336 [[nodiscard]] const_iterator end() const noexcept { return begin() + segmentCount(); }
337 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
338 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
339
340 [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator{end()}; }
341 [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator{begin()}; }
342 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
343 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
344
345 [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
346 [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
347
348 [[nodiscard]] Q_CORE_EXPORT bool isPrefixOf(const QVersionNumber &other) const noexcept;
349
350 [[nodiscard]] Q_CORE_EXPORT static int compare(const QVersionNumber &v1, const QVersionNumber &v2) noexcept;
351
352 [[nodiscard]] Q_CORE_EXPORT static QVersionNumber commonPrefix(const QVersionNumber &v1, const QVersionNumber &v2);
353
354 [[nodiscard]] Q_CORE_EXPORT QString toString() const;
355 [[nodiscard]] Q_CORE_EXPORT static QVersionNumber fromString(QAnyStringView string, qsizetype *suffixIndex = nullptr);
356
357#if QT_DEPRECATED_SINCE(6, 4) && QT_POINTER_SIZE != 4
359 QT_DEPRECATED_VERSION_X_6_4("Use the 'qsizetype *suffixIndex' overload.")
361 {
363 // fromString() writes to *n unconditionally, but GCC can't know that
364 QT_WARNING_DISABLE_GCC("-Wmaybe-uninitialized")
365 qsizetype n;
366 auto r = fromString(string, &n);
367 if (suffixIndex) {
368 Q_ASSERT(int(n) == n);
369 *suffixIndex = int(n);
370 }
371 return r;
373 }
374#endif
375
376
377#if QT_CORE_REMOVED_SINCE(6, 4)
381#endif
382
383private:
384 [[nodiscard]] friend bool comparesEqual(const QVersionNumber &lhs,
385 const QVersionNumber &rhs) noexcept
386 {
387 return lhs.segmentCount() == rhs.segmentCount() && compare(lhs, rhs) == 0;
388 }
390 const QVersionNumber &rhs) noexcept
391 {
392 int c = compare(lhs, rhs);
393 return Qt::compareThreeWay(c, 0);
394 }
396
397#ifndef QT_NO_DATASTREAM
399#endif
401};
402
404
405#ifndef QT_NO_DEBUG_STREAM
406Q_CORE_EXPORT QDebug operator<<(QDebug, const QVersionNumber &version);
407#endif
408
409QT_END_NAMESPACE
410
411QT_DECL_METATYPE_EXTERN(QVersionNumber, Q_CORE_EXPORT)
412
413#endif // QVERSIONNUMBER_H
\inmodule QtCore\reentrant
Definition qdatastream.h:47
friend bool operator<=(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs) noexcept
friend bool operator<(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs) noexcept
friend bool operator>=(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs) noexcept
constexpr QOperatingSystemVersionBase(OSType osType, int vmajor, int vminor=-1, int vmicro=-1)
bool isAnyOfType(std::initializer_list< OSType > types) const
static Q_CORE_EXPORT bool isAnyOfType(std::initializer_list< OSType > types, OSType type)
friend bool operator>(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs) noexcept
friend Qt::partial_ordering compareThreeWay(const QOperatingSystemVersionBase &lhs, const QOperatingSystemVersionBase &rhs) noexcept
static constexpr OSType currentType()
static Q_CORE_EXPORT int compare(QOperatingSystemVersionBase v1, QOperatingSystemVersionBase v2) noexcept
static constexpr QOperatingSystemVersionBase MacOSSequoia
static constexpr QOperatingSystemVersionBase Windows11_21H2
\variable QOperatingSystemVersion::Windows11_21H2
static constexpr QOperatingSystemVersionBase AndroidNougat
\variable QOperatingSystemVersion::AndroidNougat
static constexpr QOperatingSystemVersionBase Android10
\variable QOperatingSystemVersion::Android10
static constexpr QOperatingSystemVersionBase MacOSSierra
\variable QOperatingSystemVersion::MacOSSierra
static constexpr QOperatingSystemVersionBase OSXYosemite
\variable QOperatingSystemVersion::OSXYosemite
static constexpr QOperatingSystemVersionBase Android12L
\variable QOperatingSystemVersion::Android12L
static constexpr QOperatingSystemVersionBase Windows10_1909
\variable QOperatingSystemVersion::Windows10_1909
static constexpr QOperatingSystemVersionBase AndroidKitKat
\variable QOperatingSystemVersion::AndroidKitKat
static constexpr QOperatingSystemVersionBase AndroidJellyBean
\variable QOperatingSystemVersion::MacOSSonoma
static constexpr QOperatingSystemVersionBase AndroidOreo_MR1
\variable QOperatingSystemVersion::AndroidOreo_MR1
static constexpr QOperatingSystemVersionBase Android13
\variable QOperatingSystemVersion::Android13
static constexpr QOperatingSystemVersionBase MacOSCatalina
\variable QOperatingSystemVersion::MacOSCatalina
static constexpr QOperatingSystemVersionBase AndroidLollipop_MR1
\variable QOperatingSystemVersion::AndroidLollipop_MR1
static constexpr QOperatingSystemVersionBase Windows10
\variable QOperatingSystemVersion::Windows10
static constexpr QOperatingSystemVersionBase Windows10_1903
\variable QOperatingSystemVersion::Windows10_1903
static constexpr QOperatingSystemVersionBase Android12
\variable QOperatingSystemVersion::Android12
static constexpr OSType currentType()
Returns the current OS type without constructing a QOperatingSystemVersion instance.
static constexpr QOperatingSystemVersionBase Windows11_23H2
static constexpr QOperatingSystemVersionBase OSXElCapitan
\variable QOperatingSystemVersion::OSXElCapitan
static constexpr QOperatingSystemVersionBase MacOSMojave
\variable QOperatingSystemVersion::MacOSMojave
static constexpr QOperatingSystemVersionBase Windows8
\variable QOperatingSystemVersion::Windows8
static constexpr QOperatingSystemVersionBase AndroidNougat_MR1
\variable QOperatingSystemVersion::AndroidNougat_MR1
static constexpr QOperatingSystemVersionBase Android14
static constexpr QOperatingSystemVersionBase Windows10_2004
\variable QOperatingSystemVersion::Windows10_2004
static constexpr QOperatingSystemVersionBase MacOSHighSierra
\variable QOperatingSystemVersion::MacOSHighSierra
static constexpr QOperatingSystemVersionBase Windows10_21H2
\variable QOperatingSystemVersion::Windows10_21H2
constexpr QOperatingSystemVersion(const QOperatingSystemVersionBase &osversion)
static constexpr QOperatingSystemVersionBase AndroidMarshmallow
\variable QOperatingSystemVersion::AndroidMarshmallow
constexpr QOperatingSystemVersion(OSType osType, int vmajor, int vminor=-1, int vmicro=-1)
Constructs a QOperatingSystemVersion consisting of the OS type osType, and major, minor,...
static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR2
\variable QOperatingSystemVersion::AndroidJellyBean_MR2
static constexpr QOperatingSystemVersionBase Windows10_20H2
\variable QOperatingSystemVersion::Windows10_20H2
static constexpr QOperatingSystemVersionBase AndroidPie
\variable QOperatingSystemVersion::AndroidPie
static constexpr QOperatingSystemVersionBase AndroidOreo
\variable QOperatingSystemVersion::AndroidOreo
static constexpr QOperatingSystemVersionBase OSXMavericks
\variable QOperatingSystemVersion::Windows11_23H2
static constexpr QOperatingSystemVersionBase MacOSMonterey
\variable QOperatingSystemVersion::MacOSMonterey
static constexpr QOperatingSystemVersionBase Windows7
\variable QOperatingSystemVersion::Windows7
static constexpr QOperatingSystemVersionBase MacOSVentura
\variable QOperatingSystemVersion::MacOSVentura
static constexpr QOperatingSystemVersionBase Windows10_1809
\variable QOperatingSystemVersion::Windows10_1809
static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR1
\variable QOperatingSystemVersion::AndroidJellyBean_MR1
static constexpr QOperatingSystemVersionBase MacOSBigSur
\variable QOperatingSystemVersion::MacOSBigSur
constexpr OSType type() const
Returns the OS type identified by the QOperatingSystemVersion.
static constexpr QOperatingSystemVersionBase AndroidLollipop
\variable QOperatingSystemVersion::AndroidLollipop
static constexpr QOperatingSystemVersionBase Windows8_1
\variable QOperatingSystemVersion::Windows8_1
static constexpr QOperatingSystemVersionBase Windows11_24H2
static constexpr QOperatingSystemVersionBase Windows10_21H1
\variable QOperatingSystemVersion::Windows10_21H1
static constexpr QOperatingSystemVersionBase Windows11_22H2
\variable QOperatingSystemVersion::Windows11_22H2
static constexpr QOperatingSystemVersionBase Windows10_22H2
\variable QOperatingSystemVersion::Windows10_22H2
static constexpr QOperatingSystemVersionBase Android11
\variable QOperatingSystemVersion::Android11
static constexpr QOperatingSystemVersionBase MacOSSonoma
static constexpr QOperatingSystemVersionBase Windows11
\variable QOperatingSystemVersion::Windows11
\inmodule QtCore
QVersionNumber(std::initializer_list< int > args)
Constructs a version number from the std::initializer_list specified by args.
Q_CORE_EXPORT bool isPrefixOf(const QVersionNumber &other) const noexcept
Returns true if the current version number is contained in the other version number,...
const_reverse_iterator crbegin() const noexcept
const_iterator cbegin() const noexcept
friend Qt::strong_ordering compareThreeWay(const QVersionNumber &lhs, const QVersionNumber &rhs) noexcept
const_iterator end() const noexcept
const_iterator constEnd() const noexcept
QVersionNumber() noexcept
Produces a null version.
friend bool comparesEqual(const QVersionNumber &lhs, const QVersionNumber &rhs) noexcept
const_reverse_iterator crend() const noexcept
const_reverse_iterator rbegin() const noexcept
qsizetype segmentCount() const noexcept
Returns the number of integers stored in segments().
const_reverse_iterator rend() const noexcept
const_iterator constBegin() const noexcept
const_iterator cend() const noexcept
int segmentAt(qsizetype index) const noexcept
Returns the segment value at index.
static Q_CORE_EXPORT int compare(const QVersionNumber &v1, const QVersionNumber &v2) noexcept
Compares v1 with v2 and returns an integer less than, equal to, or greater than zero,...
const_iterator begin() const noexcept
Combined button and popup list for selecting options.
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2439
static int compareVersionComponents(int lhs, int rhs) noexcept
Q_DECLARE_TYPEINFO(QOperatingSystemVersion, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QVersionNumber, Q_RELOCATABLE_TYPE)