Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtyperevision.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 QTYPEREVISION_H
7#define QTYPEREVISION_H
8
9#include <QtCore/qassert.h>
10#include <QtCore/qcompare.h>
11#include <QtCore/qcontainertools_impl.h>
12#include <QtCore/qmetatype.h>
13#include <QtCore/qtypeinfo.h>
14
15#include <limits>
16
18
19class QDataStream;
20class QDebug;
21
22class QTypeRevision;
23Q_CORE_EXPORT size_t qHash(const QTypeRevision &key, size_t seed = 0);
24
25#ifndef QT_NO_DATASTREAM
26Q_CORE_EXPORT QDataStream& operator<<(QDataStream &out, const QTypeRevision &revision);
27Q_CORE_EXPORT QDataStream& operator>>(QDataStream &in, QTypeRevision &revision);
28#endif
29
31{
32public:
33 template<typename Integer>
34 using if_valid_segment_type = typename std::enable_if<
35 std::is_integral<Integer>::value, bool>::type;
36
37 template<typename Integer>
38 using if_valid_value_type = typename std::enable_if<
39 std::is_integral<Integer>::value
40 && (sizeof(Integer) > sizeof(quint16)
41 || (sizeof(Integer) == sizeof(quint16)
42 && !std::is_signed<Integer>::value)), bool>::type;
43
44 template<typename Integer, if_valid_segment_type<Integer> = true>
45 static constexpr bool isValidSegment(Integer segment)
46 {
47 // using extra parentheses around max to avoid expanding it if it is a macro
48 return segment >= Integer(0)
49 && ((std::numeric_limits<Integer>::max)() < Integer(SegmentUnknown)
50 || segment < Integer(SegmentUnknown));
51 }
52
53 template<typename Major, typename Minor,
54 if_valid_segment_type<Major> = true,
55 if_valid_segment_type<Minor> = true>
62
63 template<typename Major, if_valid_segment_type<Major> = true>
65 {
67 QTypeRevision(quint8(majorVersion), SegmentUnknown);
68 }
69
70 template<typename Minor, if_valid_segment_type<Minor> = true>
72 {
74 QTypeRevision(SegmentUnknown, quint8(minorVersion));
75 }
76
77 template<typename Integer, if_valid_value_type<Integer> = true>
78 static constexpr QTypeRevision fromEncodedVersion(Integer value)
79 {
80 return Q_ASSERT((value & ~Integer(0xffff)) == Integer(0)),
81 QTypeRevision((value & Integer(0xff00)) >> 8, value & Integer(0xff));
82 }
83
84 static constexpr QTypeRevision zero() { return QTypeRevision(0, 0); }
85
86 constexpr QTypeRevision() = default;
87
88 constexpr bool hasMajorVersion() const { return m_majorVersion != SegmentUnknown; }
89 constexpr quint8 majorVersion() const { return m_majorVersion; }
90
91 constexpr bool hasMinorVersion() const { return m_minorVersion != SegmentUnknown; }
92 constexpr quint8 minorVersion() const { return m_minorVersion; }
93
94 constexpr bool isValid() const { return hasMajorVersion() || hasMinorVersion(); }
95
96 template<typename Integer, if_valid_value_type<Integer> = true>
97 constexpr Integer toEncodedVersion() const
98 {
99 return Integer(m_majorVersion << 8) | Integer(m_minorVersion);
100 }
101
102private:
103 friend constexpr bool
104 comparesEqual(const QTypeRevision &lhs, const QTypeRevision &rhs) noexcept
105 { return lhs.toEncodedVersion<quint16>() == rhs.toEncodedVersion<quint16>(); }
106 friend constexpr Qt::strong_ordering
107 compareThreeWay(const QTypeRevision &lhs, const QTypeRevision &rhs) noexcept
108 {
109 // For both major and minor the following rule applies:
110 // non-0 ver > unspecified ver > 0 ver
111 auto cmpUnspecified = [](quint8 leftVer, quint8 rightVer) {
112 Q_ASSERT(leftVer != rightVer
113 && (leftVer == QTypeRevision::SegmentUnknown
114 || rightVer == QTypeRevision::SegmentUnknown));
115 if (leftVer != QTypeRevision::SegmentUnknown)
118 };
119
120 if (lhs.hasMajorVersion() != rhs.hasMajorVersion()) {
121 return cmpUnspecified(lhs.majorVersion(), rhs.majorVersion());
122 } else {
123 const auto majorRes = Qt::compareThreeWay(lhs.majorVersion(), rhs.majorVersion());
124 if (is_eq(majorRes)) {
125 if (lhs.hasMinorVersion() != rhs.hasMinorVersion())
126 return cmpUnspecified(lhs.minorVersion(), rhs.minorVersion());
127 return Qt::compareThreeWay(lhs.minorVersion(), rhs.minorVersion());
128 }
129 return majorRes;
130 }
131 }
133
134 enum { SegmentUnknown = 0xff };
135
136#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
137 constexpr QTypeRevision(quint8 major, quint8 minor)
138 : m_minorVersion(minor), m_majorVersion(major) {}
139
140 quint8 m_minorVersion = SegmentUnknown;
141 quint8 m_majorVersion = SegmentUnknown;
142#else
143 constexpr QTypeRevision(quint8 major, quint8 minor)
144 : m_majorVersion(major), m_minorVersion(minor) {}
145
146 quint8 m_majorVersion = SegmentUnknown;
147 quint8 m_minorVersion = SegmentUnknown;
148#endif
149};
150
151static_assert(sizeof(QTypeRevision) == 2);
153
154#ifndef QT_NO_DEBUG_STREAM
155Q_CORE_EXPORT QDebug operator<<(QDebug, const QTypeRevision &revision);
156#endif
157
159
161
162#endif // QTYPEREVISION_H
163
164#if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 2
165// make QVersionNumber available from <QTypeRevision>
166#include <QtCore/qversionnumber.h>
167#endif
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
static constexpr QTypeRevision fromMajorVersion(Major majorVersion)
Produces a QTypeRevision from the given majorVersion with an invalid minor version.
static constexpr QTypeRevision fromVersion(Major majorVersion, Minor minorVersion)
Produces a QTypeRevision from the given majorVersion and minorVersion, both of which need to be a val...
typename std::enable_if< std::is_integral< Integer >::value &&(sizeof(Integer) > sizeof(quint16)||(sizeof(Integer)==sizeof(quint16) &&!std::is_signed< Integer >::value)), bool >::type if_valid_value_type
static constexpr bool isValidSegment(Integer segment)
Returns true if the given number can be used as either major or minor version in a QTypeRevision.
static constexpr QTypeRevision fromMinorVersion(Minor minorVersion)
Produces a QTypeRevision from the given minorVersion with an invalid major version.
constexpr bool hasMinorVersion() const
Returns true if the minor version is known, otherwise false.
static constexpr QTypeRevision zero()
Produces a QTypeRevision with major and minor version {0}.
constexpr bool hasMajorVersion() const
Returns true if the major version is known, otherwise false.
constexpr quint8 minorVersion() const
Returns the minor version encoded in the revision.
friend constexpr Qt::strong_ordering compareThreeWay(const QTypeRevision &lhs, const QTypeRevision &rhs) noexcept
constexpr bool isValid() const
Returns true if the major version or the minor version is known, otherwise false.
typename std::enable_if< std::is_integral< Integer >::value, bool >::type if_valid_segment_type
static constexpr QTypeRevision fromEncodedVersion(Integer value)
Produces a QTypeRevision from the given value.
constexpr Integer toEncodedVersion() const
Transforms the revision into an integer value, encoding the minor version into the least significant ...
friend constexpr bool comparesEqual(const QTypeRevision &lhs, const QTypeRevision &rhs) noexcept
constexpr quint8 majorVersion() const
Returns the major version encoded in the revision.
constexpr QTypeRevision()=default
Produces an invalid revision.
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Definition qcompare.h:400
static const strong_ordering greater
Definition qcompare.h:405
static const strong_ordering less
Definition qcompare.h:402
Combined button and popup list for selecting options.
constexpr Qt::strong_ordering compareThreeWay(LeftInt lhs, RightInt rhs) noexcept
#define Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(...)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define QT_DECL_METATYPE_EXTERN(TYPE, EXPORT)
Definition qmetatype.h:1388
GLuint64 key
GLenum type
GLuint segment
GLuint in
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:158
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &in, QTypeRevision &revision)
Q_CORE_EXPORT size_t qHash(const QTypeRevision &key, size_t seed=0)
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QTypeRevision &revision)
unsigned short quint16
Definition qtypes.h:48
unsigned char quint8
Definition qtypes.h:46
QTextStream out(stdout)
[7]