| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // Copyright (C) 2022 Intel Corporation. |
| 3 | // Copyright (C) 2015 Keith Gardner <[email protected]> |
| 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 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | class QVersionNumber; |
| 24 | Q_CORE_EXPORT size_t qHash(const QVersionNumber &key, size_t seed = 0); |
| 25 | |
| 26 | #ifndef QT_NO_DATASTREAM |
| 27 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QVersionNumber &version); |
| 28 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QVersionNumber &version); |
| 29 | #endif |
| 30 | |
| 31 | class QVersionNumber |
| 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(data: seg.data(), len: seg.size())) |
| 66 | setInlineData(data: seg.data(), len: 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(a&: dummy, b&: other.dummy); |
| 106 | } |
| 107 | |
| 108 | explicit SegmentStorage(QList<int> &&seg) |
| 109 | { |
| 110 | if (dataFitsInline(data: std::as_const(t&: seg).data(), len: seg.size())) |
| 111 | setInlineData(data: std::as_const(t&: seg).data(), len: 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(data: first, len: last - first)) { |
| 124 | setInlineData(data: first, len: 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(i: 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: lhs.i, rhs: 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(o: compareThreeWay(lhs, rhs)); |
| 222 | } |
| 223 | friend constexpr bool operator<=(It lhs, It rhs) { |
| 224 | return is_lteq(o: compareThreeWay(lhs, rhs)); |
| 225 | } |
| 226 | friend constexpr bool operator>(It lhs, It rhs) { |
| 227 | return is_gt(o: compareThreeWay(lhs, rhs)); |
| 228 | } |
| 229 | friend constexpr bool operator>=(It lhs, It rhs) { |
| 230 | return is_gteq(o: 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(index: i); } |
| 249 | pointer operator->() const { return {.t: **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 | |
| 270 | public: |
| 271 | using const_iterator = It; |
| 272 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 273 | |
| 274 | using value_type = It::value_type; |
| 275 | using difference_type = It::difference_type; |
| 276 | using size_type = It::size_type; |
| 277 | using reference = It::reference; |
| 278 | using const_reference = reference; |
| 279 | using pointer = It::pointer; |
| 280 | using const_pointer = pointer; |
| 281 | |
| 282 | inline QVersionNumber() noexcept |
| 283 | : m_segments() |
| 284 | {} |
| 285 | Q_WEAK_OVERLOAD |
| 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 | |
| 290 | Q_WEAK_OVERLOAD |
| 291 | explicit QVersionNumber(QList<int> &&seg) : m_segments(std::move(seg)) { } |
| 292 | |
| 293 | inline QVersionNumber(std::initializer_list<int> args) |
| 294 | : m_segments(QSpan{args}) |
| 295 | {} |
| 296 | |
| 297 | explicit QVersionNumber(QSpan<const int> args) |
| 298 | : m_segments(args) |
| 299 | {} |
| 300 | |
| 301 | inline explicit QVersionNumber(int maj) |
| 302 | { m_segments.setSegments(len: 1, maj); } |
| 303 | |
| 304 | inline explicit QVersionNumber(int maj, int min) |
| 305 | { m_segments.setSegments(len: 2, maj, min); } |
| 306 | |
| 307 | inline explicit QVersionNumber(int maj, int min, int mic) |
| 308 | { m_segments.setSegments(len: 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(index: segmentCount() - 1) != 0; } |
| 315 | |
| 316 | [[nodiscard]] inline int majorVersion() const noexcept |
| 317 | { return segmentAt(index: 0); } |
| 318 | |
| 319 | [[nodiscard]] inline int minorVersion() const noexcept |
| 320 | { return segmentAt(index: 1); } |
| 321 | |
| 322 | [[nodiscard]] inline int microVersion() const noexcept |
| 323 | { return segmentAt(index: 2); } |
| 324 | |
| 325 | [[nodiscard]] Q_CORE_EXPORT QVersionNumber normalized() const; |
| 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 |
| 358 | Q_WEAK_OVERLOAD |
| 359 | QT_DEPRECATED_VERSION_X_6_4("Use the 'qsizetype *suffixIndex' overload." ) |
| 360 | [[nodiscard]] static QVersionNumber fromString(QAnyStringView string, int *suffixIndex) |
| 361 | { |
| 362 | QT_WARNING_PUSH |
| 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, suffixIndex: &n); |
| 367 | if (suffixIndex) { |
| 368 | Q_ASSERT(int(n) == n); |
| 369 | *suffixIndex = int(n); |
| 370 | } |
| 371 | return r; |
| 372 | QT_WARNING_POP |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | |
| 377 | #if QT_CORE_REMOVED_SINCE(6, 4) |
| 378 | [[nodiscard]] Q_CORE_EXPORT static QVersionNumber fromString(const QString &string, int *suffixIndex); |
| 379 | [[nodiscard]] Q_CORE_EXPORT static QVersionNumber fromString(QLatin1StringView string, int *suffixIndex); |
| 380 | [[nodiscard]] Q_CORE_EXPORT static QVersionNumber fromString(QStringView string, int *suffixIndex); |
| 381 | #endif |
| 382 | |
| 383 | private: |
| 384 | [[nodiscard]] friend bool comparesEqual(const QVersionNumber &lhs, |
| 385 | const QVersionNumber &rhs) noexcept |
| 386 | { |
| 387 | return lhs.segmentCount() == rhs.segmentCount() && compare(v1: lhs, v2: rhs) == 0; |
| 388 | } |
| 389 | [[nodiscard]] friend Qt::strong_ordering compareThreeWay(const QVersionNumber &lhs, |
| 390 | const QVersionNumber &rhs) noexcept |
| 391 | { |
| 392 | int c = compare(v1: lhs, v2: rhs); |
| 393 | return Qt::compareThreeWay(lhs: c, rhs: 0); |
| 394 | } |
| 395 | Q_DECLARE_STRONGLY_ORDERED(QVersionNumber) |
| 396 | |
| 397 | #ifndef QT_NO_DATASTREAM |
| 398 | friend Q_CORE_EXPORT QDataStream& operator>>(QDataStream &in, QVersionNumber &version); |
| 399 | #endif |
| 400 | friend Q_CORE_EXPORT size_t qHash(const QVersionNumber &key, size_t seed); |
| 401 | }; |
| 402 | |
| 403 | Q_DECLARE_TYPEINFO(QVersionNumber, Q_RELOCATABLE_TYPE); |
| 404 | |
| 405 | #ifndef QT_NO_DEBUG_STREAM |
| 406 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QVersionNumber &version); |
| 407 | #endif |
| 408 | |
| 409 | QT_END_NAMESPACE |
| 410 | |
| 411 | QT_DECL_METATYPE_EXTERN(QVersionNumber, Q_CORE_EXPORT) |
| 412 | |
| 413 | #endif // QVERSIONNUMBER_H |
| 414 | |