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
qtyperevision.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtCore/qtyperevision.h>
6#include <QtCore/qhashfunctions.h>
7
8#ifndef QT_NO_DATASTREAM
9# include <QtCore/qdatastream.h>
10#endif
11
12#ifndef QT_NO_DEBUG_STREAM
13# include <QtCore/qdebug.h>
14#endif
15
16#include <algorithm>
17#include <limits>
18
19QT_BEGIN_NAMESPACE
20
21QT_IMPL_METATYPE_EXTERN(QTypeRevision)
22
23/*!
24 \class QTypeRevision
25 \inmodule QtCore
26 \since 6.0
27 \brief The QTypeRevision class contains a lightweight representation of
28 a version number with two 8-bit segments, major and minor, either
29 of which can be unknown.
30 \compares strong
31
32 Use this class to describe revisions of a type. Compatible revisions can be
33 expressed as increments of the minor version. Breaking changes can be
34 expressed as increments of the major version. The return values of
35 \l QMetaMethod::revision() and \l QMetaProperty::revision() can be passed to
36 \l QTypeRevision::fromEncodedVersion(). The resulting major and minor versions
37 specify in which Qt versions the properties and methods were added.
38
39 \sa QMetaMethod::revision(), QMetaProperty::revision()
40*/
41
42/*!
43 \fn template<typename Integer, QTypeRevision::if_valid_segment_type<Integer> = true> static bool QTypeRevision::isValidSegment(Integer segment)
44
45 Returns true if the given number can be used as either major or minor
46 version in a QTypeRevision. The valid range for \a segment is \c {>= 0} and \c {< 255}.
47*/
48
49/*!
50 \fn QTypeRevision::QTypeRevision()
51
52 Produces an invalid revision.
53
54 \sa isValid()
55*/
56
57/*!
58 \fn template<typename Major, typename Minor, QTypeRevision::if_valid_segment_type<Major> = true, QTypeRevision::if_valid_segment_type<Minor> = true> static QTypeRevision QTypeRevision::fromVersion(Major majorVersion, Minor minorVersion)
59
60 Produces a QTypeRevision from the given \a majorVersion and \a minorVersion,
61 both of which need to be a valid segments.
62
63 \sa isValidSegment()
64*/
65
66/*!
67 \fn template<typename Major, QTypeRevision::if_valid_segment_type<Major> = true> static QTypeRevision QTypeRevision::fromMajorVersion(Major majorVersion)
68
69 Produces a QTypeRevision from the given \a majorVersion with an invalid minor
70 version. \a majorVersion needs to be a valid segment.
71
72 \sa isValidSegment()
73*/
74
75/*!
76 \fn template<typename Minor, QTypeRevision::if_valid_segment_type<Minor> = true> static QTypeRevision QTypeRevision::fromMinorVersion(Minor minorVersion)
77
78 Produces a QTypeRevision from the given \a minorVersion with an invalid major
79 version. \a minorVersion needs to be a valid segment.
80
81 \sa isValidSegment()
82*/
83
84/*!
85 \fn template<typename Integer, QTypeRevision::if_valid_value_type<Integer> = true> static QTypeRevision QTypeRevision::fromEncodedVersion(Integer value)
86
87 Produces a QTypeRevision from the given \a value. \a value encodes both the
88 minor and major versions in the least significant and second least
89 significant byte, respectively.
90
91 \a value must not have any bits outside the least significant two bytes set.
92 \c Integer needs to be at least 16 bits wide, and must not have a sign bit
93 in the least significant 16 bits.
94
95 \sa toEncodedVersion()
96*/
97
98/*!
99 \fn static QTypeRevision QTypeRevision::zero()
100
101 Produces a QTypeRevision with major and minor version \c{0}.
102*/
103
104/*!
105 \fn bool QTypeRevision::hasMajorVersion() const
106
107 Returns true if the major version is known, otherwise false.
108
109 \sa majorVersion(), hasMinorVersion()
110*/
111
112/*!
113 \fn quint8 QTypeRevision::majorVersion() const
114
115 Returns the major version encoded in the revision.
116
117 \sa hasMajorVersion(), minorVersion()
118*/
119
120/*!
121 \fn bool QTypeRevision::hasMinorVersion() const
122
123 Returns true if the minor version is known, otherwise false.
124
125 \sa minorVersion(), hasMajorVersion()
126*/
127
128/*!
129 \fn quint8 QTypeRevision::minorVersion() const
130
131 Returns the minor version encoded in the revision.
132
133 \sa hasMinorVersion(), majorVersion()
134*/
135
136/*!
137 \fn bool QTypeRevision::isValid() const
138
139 Returns true if the major version or the minor version is known,
140 otherwise false.
141
142 \sa hasMajorVersion(), hasMinorVersion()
143*/
144
145/*!
146 \fn template<typename Integer, QTypeRevision::if_valid_value_type<Integer> = true> Integer QTypeRevision::toEncodedVersion() const
147
148 Transforms the revision into an integer value, encoding the minor
149 version into the least significant byte, and the major version into
150 the second least significant byte.
151
152 \c Integer needs to be at least 16 bits wide, and must not have a sign bit
153 in the least significant 16 bits.
154
155 \sa fromEncodedVersion()
156*/
157
158#ifndef QT_NO_DATASTREAM
159/*!
160 \fn QDataStream& operator<<(QDataStream &out, const QTypeRevision &revision)
161 \relates QTypeRevision
162 \since 6.0
163
164 Writes the revision \a revision to stream \a out.
165 */
166QDataStream &operator<<(QDataStream &out, const QTypeRevision &revision)
167{
168 return out << revision.toEncodedVersion<quint16>();
169}
170
171/*!
172 \fn QDataStream& operator>>(QDataStream &in, QTypeRevision &revision)
173 \relates QTypeRevision
174 \since 6.0
175
176 Reads a revision from stream \a in and stores it in \a revision.
177 */
178QDataStream &operator>>(QDataStream &in, QTypeRevision &revision)
179{
180 quint16 value;
181 in >> value;
182 revision = QTypeRevision::fromEncodedVersion(value);
183 return in;
184}
185#endif
186
187#ifndef QT_NO_DEBUG_STREAM
188QDebug operator<<(QDebug debug, const QTypeRevision &revision)
189{
190 QDebugStateSaver saver(debug);
191 if (revision.hasMajorVersion()) {
192 if (revision.hasMinorVersion())
193 debug.nospace() << revision.majorVersion() << '.' << revision.minorVersion();
194 else
195 debug.nospace().noquote() << revision.majorVersion() << ".x";
196 } else {
197 if (revision.hasMinorVersion())
198 debug << revision.minorVersion();
199 else
200 debug.noquote() << "invalid";
201 }
202 return debug;
203}
204#endif
205
206/*!
207 \qhashold{QHash}
208 \since 6.0
209*/
210size_t qHash(const QTypeRevision &key, size_t seed)
211{
212 return qHash(key.toEncodedVersion<quint16>(), seed);
213}
214
215QT_END_NAMESPACE