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// Qt-Security score:critical reason:data-parser
6
7#ifndef QVERSIONNUMBER_H
8#define QVERSIONNUMBER_H
9
10#include <QtCore/qcompare.h>
11#include <QtCore/qcontainertools_impl.h>
12#include <QtCore/qlist.h>
13#include <QtCore/qmetatype.h>
14#include <QtCore/qnamespace.h>
15#include <QtCore/qspan.h>
16#include <QtCore/qstring.h>
17#include <QtCore/qtypeinfo.h>
18#if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 2
19#include <QtCore/qtyperevision.h>
20#endif // lean headers level 2
21
22QT_BEGIN_NAMESPACE
23
24class QVersionNumber;
25Q_CORE_EXPORT size_t qHash(const QVersionNumber &key, size_t seed = 0);
26
27#ifndef QT_NO_DATASTREAM
28Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QVersionNumber &version);
29Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QVersionNumber &version);
30#endif
31
33{
34 /*
35 * QVersionNumber stores small values inline, without memory allocation.
36 * We do that by setting the LSB in the pointer that would otherwise hold
37 * the longer form of the segments.
38 * The constants below help us deal with the permutations for 32- and 64-bit,
39 * little- and big-endian architectures.
40 */
41 enum {
42 // in little-endian, inline_segments[0] is shared with the pointer's LSB, while
43 // in big-endian, it's inline_segments[7]
44 InlineSegmentMarker = Q_BYTE_ORDER == Q_LITTLE_ENDIAN ? 0 : sizeof(void *) - 1,
45 InlineSegmentStartIdx = !InlineSegmentMarker, // 0 for BE, 1 for LE
46 InlineSegmentCount = sizeof(void *) - 1
47 };
48 static_assert(InlineSegmentCount >= 3); // at least major, minor, micro
49
50 struct SegmentStorage
51 {
52 // Note: we alias the use of dummy and inline_segments in the use of the
53 // union below. This is undefined behavior in C++98, but most compilers implement
54 // the C++11 behavior. The one known exception is older versions of Sun Studio.
55 union {
56 quintptr dummy;
57 qint8 inline_segments[sizeof(void *)];
58 QList<int> *pointer_segments;
59 };
60
61 // set the InlineSegmentMarker and set length to zero
62 SegmentStorage() noexcept : dummy(1) {}
63
64 SegmentStorage(const QList<int> &seg)
65 {
66 if (dataFitsInline(seg.data(), seg.size()))
67 setInlineData(seg.data(), seg.size());
68 else
69 setListData(seg);
70 }
71
72 Q_CORE_EXPORT void setListData(const QList<int> &seg);
73
74 SegmentStorage(const SegmentStorage &other)
75 {
76 if (other.isUsingPointer())
77 setListData(*other.pointer_segments);
78 else
79 dummy = other.dummy;
80 }
81
82 SegmentStorage &operator=(const SegmentStorage &other)
83 {
84 if (isUsingPointer() && other.isUsingPointer()) {
85 *pointer_segments = *other.pointer_segments;
86 } else if (other.isUsingPointer()) {
87 setListData(*other.pointer_segments);
88 } else {
89 if (isUsingPointer())
90 delete pointer_segments;
91 dummy = other.dummy;
92 }
93 return *this;
94 }
95
96 SegmentStorage(SegmentStorage &&other) noexcept
97 : dummy(other.dummy)
98 {
99 other.dummy = 1;
100 }
101
102 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(SegmentStorage)
103
104 void swap(SegmentStorage &other) noexcept
105 {
106 std::swap(dummy, other.dummy);
107 }
108
109 explicit SegmentStorage(QList<int> &&seg)
110 {
111 if (dataFitsInline(std::as_const(seg).data(), seg.size()))
112 setInlineData(std::as_const(seg).data(), seg.size());
113 else
114 setListData(std::move(seg));
115 }
116
117 Q_CORE_EXPORT void setListData(QList<int> &&seg);
118
119 explicit SegmentStorage(QSpan<const int> args)
120 : SegmentStorage(args.begin(), args.end()) {}
121
122 explicit SegmentStorage(const int *first, const int *last)
123 {
124 if (dataFitsInline(first, last - first)) {
125 setInlineData(first, last - first);
126 } else {
127 setListData(first, last);
128 }
129 }
130
131 Q_CORE_EXPORT void setListData(const int *first, const int *last);
132
133 ~SegmentStorage() { if (isUsingPointer()) delete pointer_segments; }
134
135 bool isUsingPointer() const noexcept
136 { return (inline_segments[InlineSegmentMarker] & 1) == 0; }
137
138 qsizetype size() const noexcept
139 { return isUsingPointer() ? pointer_segments->size() : (inline_segments[InlineSegmentMarker] >> 1); }
140
141 void setInlineSize(qsizetype len)
142 {
143 Q_ASSERT(len <= InlineSegmentCount);
144 inline_segments[InlineSegmentMarker] = qint8(1 + 2 * len);
145 }
146
147 Q_CORE_EXPORT void resize(qsizetype len);
148
149 int at(qsizetype index) const
150 {
151 return isUsingPointer() ?
152 pointer_segments->at(index) :
153 inline_segments[InlineSegmentStartIdx + index];
154 }
155
156 void setSegments(int len, int maj, int min = 0, int mic = 0)
157 {
158 if (maj == qint8(maj) && min == qint8(min) && mic == qint8(mic)) {
159 int data[] = { maj, min, mic };
160 setInlineData(data, len);
161 } else {
162 setVector(len, maj, min, mic);
163 }
164 }
165
166 private:
167 static bool dataFitsInline(const int *data, qsizetype len)
168 {
169 if (len > InlineSegmentCount)
170 return false;
171 for (qsizetype i = 0; i < len; ++i)
172 if (data[i] != qint8(data[i]))
173 return false;
174 return true;
175 }
176 void setInlineData(const int *data, qsizetype len)
177 {
178 Q_ASSERT(len <= InlineSegmentCount);
179 dummy = 1 + len * 2;
180#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
181 for (qsizetype i = 0; i < len; ++i)
182 dummy |= quintptr(data[i] & 0xFF) << (8 * (i + 1));
183#elif Q_BYTE_ORDER == Q_BIG_ENDIAN
184 for (qsizetype i = 0; i < len; ++i)
185 dummy |= quintptr(data[i] & 0xFF) << (8 * (sizeof(void *) - i - 1));
186#else
187 // the code above is equivalent to:
188 setInlineSize(len);
189 for (qsizetype i = 0; i < len; ++i)
190 inline_segments[InlineSegmentStartIdx + i] = data[i] & 0xFF;
191#endif
192 }
193
194 Q_CORE_EXPORT void setVector(int len, int maj, int min, int mic);
195 } m_segments;
196
197 class It
198 {
199 const QVersionNumber *v;
200 qsizetype i;
201
202 friend class QVersionNumber;
203 explicit constexpr It(const QVersionNumber *vn, qsizetype idx) noexcept : v(vn), i(idx) {}
204
205 friend constexpr bool comparesEqual(const It &lhs, const It &rhs)
206 { Q_ASSERT(lhs.v == rhs.v); return lhs.i == rhs.i; }
207 friend constexpr Qt::strong_ordering compareThreeWay(const It &lhs, const It &rhs)
208 { Q_ASSERT(lhs.v == rhs.v); return Qt::compareThreeWay(lhs.i, rhs.i); }
209 // macro variant does not exist: Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE_NON_NOEXCEPT(It)
210 friend constexpr bool operator==(It lhs, It rhs) {
211 return comparesEqual(lhs, rhs);
212 }
213#ifdef __cpp_lib_three_way_comparison
214 friend constexpr std::strong_ordering operator<=>(It lhs, It rhs) {
215 return compareThreeWay(lhs, rhs);
216 }
217#else
218 friend constexpr bool operator!=(It lhs, It rhs) {
219 return !operator==(lhs, rhs);
220 }
221 friend constexpr bool operator<(It lhs, It rhs) {
222 return is_lt(compareThreeWay(lhs, rhs));
223 }
224 friend constexpr bool operator<=(It lhs, It rhs) {
225 return is_lteq(compareThreeWay(lhs, rhs));
226 }
227 friend constexpr bool operator>(It lhs, It rhs) {
228 return is_gt(compareThreeWay(lhs, rhs));
229 }
230 friend constexpr bool operator>=(It lhs, It rhs) {
231 return is_gteq(compareThreeWay(lhs, rhs));
232 }
233#endif
234
235 public:
236 // Rule Of Zero applies
237 It() = default;
238
239 using iterator_category = std::random_access_iterator_tag;
240 using value_type = int;
241#ifdef QT_COMPILER_HAS_LWG3346
242 using element_type = const int;
243#endif
244 using difference_type = qptrdiff; // difference to container requirements
245 using size_type = qsizetype; // difference to container requirements
246 using reference = value_type; // difference to container requirements
247 using pointer = QtPrivate::ArrowProxy<reference>;
248
249 reference operator*() const { return v->segmentAt(i); }
250 pointer operator->() const { return {**this}; }
251
252 It &operator++() { ++i; return *this; }
253 It operator++(int) { auto copy = *this; ++*this; return copy; }
254
255 It &operator--() { --i; return *this; }
256 It operator--(int) { auto copy = *this; --*this; return copy; }
257
258 It &operator+=(difference_type n) { i += n; return *this; }
259 friend It operator+(It it, difference_type n) { it += n; return it; }
260 friend It operator+(difference_type n, It it) { return it + n; }
261
262 It &operator-=(difference_type n) { i -= n; return *this; }
263 friend It operator-(It it, difference_type n) { it -= n; return it; }
264
265 friend difference_type operator-(It lhs, It rhs)
266 { Q_ASSERT(lhs.v == rhs.v); return lhs.i - rhs.i; }
267
268 reference operator[](difference_type n) const { return *(*this + n); }
269 };
270
271public:
272 using const_iterator = It;
274
275 using value_type = It::value_type;
278 using reference = It::reference;
282
283 inline QVersionNumber() noexcept
284 : m_segments()
285 {}
287 inline explicit QVersionNumber(const QList<int> &seg) : m_segments(seg) { }
288
289 // compiler-generated copy/move ctor/assignment operators and the destructor are ok
290
292 explicit QVersionNumber(QList<int> &&seg) : m_segments(std::move(seg)) { }
293
294 inline QVersionNumber(std::initializer_list<int> args)
296 {}
297
298 explicit QVersionNumber(QSpan<const int> args)
300 {}
301
302 inline explicit QVersionNumber(int maj)
303 { m_segments.setSegments(1, maj); }
304
305 inline explicit QVersionNumber(int maj, int min)
306 { m_segments.setSegments(2, maj, min); }
307
308 inline explicit QVersionNumber(int maj, int min, int mic)
309 { m_segments.setSegments(3, maj, min, mic); }
310
311 [[nodiscard]] inline bool isNull() const noexcept
312 { return segmentCount() == 0; }
313
314 [[nodiscard]] inline bool isNormalized() const noexcept
315 { return isNull() || segmentAt(segmentCount() - 1) != 0; }
316
317 [[nodiscard]] inline int majorVersion() const noexcept
318 { return segmentAt(0); }
319
320 [[nodiscard]] inline int minorVersion() const noexcept
321 { return segmentAt(1); }
322
323 [[nodiscard]] inline int microVersion() const noexcept
324 { return segmentAt(2); }
325
327
328 [[nodiscard]] Q_CORE_EXPORT QList<int> segments() const;
329
330 [[nodiscard]] inline int segmentAt(qsizetype index) const noexcept
331 { return (m_segments.size() > index) ? m_segments.at(index) : 0; }
332
333 [[nodiscard]] inline qsizetype segmentCount() const noexcept
334 { return m_segments.size(); }
335
336 [[nodiscard]] const_iterator begin() const noexcept { return const_iterator{this, 0}; }
337 [[nodiscard]] const_iterator end() const noexcept { return begin() + segmentCount(); }
338 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
339 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
340
341 [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator{end()}; }
342 [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator{begin()}; }
343 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
344 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
345
346 [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
347 [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
348
349 [[nodiscard]] Q_CORE_EXPORT bool isPrefixOf(const QVersionNumber &other) const noexcept;
350
351 [[nodiscard]] Q_CORE_EXPORT static int compare(const QVersionNumber &v1, const QVersionNumber &v2) noexcept;
352
353 [[nodiscard]] Q_CORE_EXPORT static QVersionNumber commonPrefix(const QVersionNumber &v1, const QVersionNumber &v2);
354
355 [[nodiscard]] Q_CORE_EXPORT QString toString() const;
356 [[nodiscard]] Q_CORE_EXPORT static QVersionNumber fromString(QAnyStringView string, qsizetype *suffixIndex = nullptr);
357
358#if QT_DEPRECATED_SINCE(6, 4) && QT_POINTER_SIZE != 4
360 QT_DEPRECATED_VERSION_X_6_4("Use the 'qsizetype *suffixIndex' overload.")
362 {
364 // fromString() writes to *n unconditionally, but GCC can't know that
365 QT_WARNING_DISABLE_GCC("-Wmaybe-uninitialized")
366 qsizetype n;
367 auto r = fromString(string, &n);
368 if (suffixIndex) {
369 Q_ASSERT(int(n) == n);
370 *suffixIndex = int(n);
371 }
372 return r;
374 }
375#endif
376
377
378#if QT_CORE_REMOVED_SINCE(6, 4)
382#endif
383
384private:
385 [[nodiscard]] friend bool comparesEqual(const QVersionNumber &lhs,
386 const QVersionNumber &rhs) noexcept
387 {
388 return lhs.segmentCount() == rhs.segmentCount() && compare(lhs, rhs) == 0;
389 }
390 [[nodiscard]] friend Qt::strong_ordering compareThreeWay(const QVersionNumber &lhs,
391 const QVersionNumber &rhs) noexcept
392 {
393 int c = compare(lhs, rhs);
394 return Qt::compareThreeWay(c, 0);
395 }
397
398#ifndef QT_NO_DATASTREAM
400#endif
402};
403
405
406#ifndef QT_NO_DEBUG_STREAM
407Q_CORE_EXPORT QDebug operator<<(QDebug, const QVersionNumber &version);
408#endif
409
410QT_END_NAMESPACE
411
412QT_DECL_METATYPE_EXTERN(QVersionNumber, Q_CORE_EXPORT)
413
414#endif // QVERSIONNUMBER_H
\inmodule QtCore\reentrant
Definition qdatastream.h:50
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 MacOSTahoe
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 Windows11_25H2
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
It::reference reference
const_iterator end() const noexcept
const_iterator constEnd() const noexcept
QVersionNumber() noexcept
Produces a null version.
It::value_type value_type
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
reference const_reference
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:2582
static int compareVersionComponents(int lhs, int rhs) noexcept
Q_DECLARE_TYPEINFO(QOperatingSystemVersion, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QVersionNumber, Q_RELOCATABLE_TYPE)