Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
quuid.h
Go to the documentation of this file.
1// Copyright (C) 2020 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
4#ifndef QUUID_H
5#define QUUID_H
6
7#include <QtCore/qendian.h>
8#include <QtCore/qstring.h>
9
10#if defined(Q_OS_WIN) || defined(Q_QDOC)
11#ifndef GUID_DEFINED
12#define GUID_DEFINED
13typedef struct _GUID
14{
15 ulong Data1;
16 ushort Data2;
17 ushort Data3;
18 uchar Data4[8];
19} GUID, *REFGUID, *LPGUID;
20#endif
21#endif
22
23#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
26#endif
27
29
30class Q_CORE_EXPORT QUuid
31{
33public:
34 enum Variant {
35 VarUnknown =-1,
36 NCS = 0, // 0 - -
37 DCE = 2, // 1 0 -
38 Microsoft = 6, // 1 1 0
39 Reserved = 7 // 1 1 1
40 };
41
42 enum Version {
43 VerUnknown =-1,
44 Time = 1, // 0 0 0 1
45 EmbeddedPOSIX = 2, // 0 0 1 0
46 Md5 = 3, // 0 0 1 1
47 Name = Md5,
48 Random = 4, // 0 1 0 0
49 Sha1 = 5 // 0 1 0 1
50 };
51
53 WithBraces = 0,
54 WithoutBraces = 1,
55 Id128 = 3
56 };
57
58 union Id128Bytes {
60 quint16 data16[8];
61 quint32 data32[4];
62 quint64 data64[2];
63#ifdef __SIZEOF_INT128__
64 quint128 data128[1];
65#endif
66
67 constexpr explicit operator QByteArrayView() const noexcept
68 {
69 return QByteArrayView(data, sizeof(data));
70 }
71 };
72
73 constexpr QUuid() noexcept {}
74
75 constexpr QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3,
76 uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) noexcept
77 : data1(l), data2(w1), data3(w2), data4{b1, b2, b3, b4, b5, b6, b7, b8} {}
78 explicit QUuid(Id128Bytes id128, QSysInfo::Endian order = QSysInfo::BigEndian) noexcept;
79
80 explicit QUuid(QAnyStringView string) noexcept
81 : QUuid{fromString(string)} {}
82 static QUuid fromString(QAnyStringView string) noexcept;
83#if QT_CORE_REMOVED_SINCE(6, 3)
84 explicit QUuid(const QString &);
85 static QUuid fromString(QStringView string) noexcept;
86 static QUuid fromString(QLatin1StringView string) noexcept;
87 explicit QUuid(const char *);
88 explicit QUuid(const QByteArray &);
89#endif
90 QString toString(StringFormat mode = WithBraces) const;
91 QByteArray toByteArray(StringFormat mode = WithBraces) const;
92 Id128Bytes toBytes(QSysInfo::Endian order = QSysInfo::BigEndian) const noexcept;
93 QByteArray toRfc4122() const;
94
95 static QUuid fromBytes(const void *bytes, QSysInfo::Endian order = QSysInfo::BigEndian) noexcept;
96#if QT_CORE_REMOVED_SINCE(6, 3)
97 static QUuid fromRfc4122(const QByteArray &);
98#endif
99 static QUuid fromRfc4122(QByteArrayView) noexcept;
100
101 bool isNull() const noexcept;
102
103#ifdef __SIZEOF_INT128__
104 constexpr QUuid(quint128 uuid, QSysInfo::Endian order = QSysInfo::BigEndian) noexcept;
105 constexpr quint128 toUInt128(QSysInfo::Endian order = QSysInfo::BigEndian) const noexcept;
106#endif
107
108 constexpr bool operator==(const QUuid &orig) const noexcept
109 {
110 if (data1 != orig.data1 || data2 != orig.data2 ||
111 data3 != orig.data3)
112 return false;
113
114 for (uint i = 0; i < 8; i++)
115 if (data4[i] != orig.data4[i])
116 return false;
117
118 return true;
119 }
120
121 constexpr bool operator!=(const QUuid &orig) const noexcept
122 {
123 return !(*this == orig);
124 }
125
126 bool operator<(const QUuid &other) const noexcept;
127 bool operator>(const QUuid &other) const noexcept;
128
129#if defined(Q_OS_WIN) || defined(Q_QDOC)
130 // On Windows we have a type GUID that is used by the platform API, so we
131 // provide convenience operators to cast from and to this type.
132 constexpr QUuid(const GUID &guid) noexcept
133 : data1(guid.Data1), data2(guid.Data2), data3(guid.Data3),
134 data4{guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
135 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]} {}
136
137 constexpr QUuid &operator=(const GUID &guid) noexcept
138 {
139 *this = QUuid(guid);
140 return *this;
141 }
142
143 constexpr operator GUID() const noexcept
144 {
145 GUID guid = { data1, data2, data3, { data4[0], data4[1], data4[2], data4[3], data4[4], data4[5], data4[6], data4[7] } };
146 return guid;
147 }
148
149 constexpr bool operator==(const GUID &guid) const noexcept
150 {
151 return *this == QUuid(guid);
152 }
153
154 constexpr bool operator!=(const GUID &guid) const noexcept
155 {
156 return !(*this == guid);
157 }
158#endif
159 static QUuid createUuid();
160#ifndef QT_BOOTSTRAPPED
161 static QUuid createUuidV3(const QUuid &ns, const QByteArray &baseData);
162#endif
163 static QUuid createUuidV5(const QUuid &ns, const QByteArray &baseData);
164#ifndef QT_BOOTSTRAPPED
165 static inline QUuid createUuidV3(const QUuid &ns, const QString &baseData)
166 {
167 return QUuid::createUuidV3(ns, baseData.toUtf8());
168 }
169#endif
170
171 static inline QUuid createUuidV5(const QUuid &ns, const QString &baseData)
172 {
173 return QUuid::createUuidV5(ns, baseData.toUtf8());
174 }
175
176 QUuid::Variant variant() const noexcept;
177 QUuid::Version version() const noexcept;
178
179#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
180 static QUuid fromCFUUID(CFUUIDRef uuid);
181 CFUUIDRef toCFUUID() const Q_DECL_CF_RETURNS_RETAINED;
182 static QUuid fromNSUUID(const NSUUID *uuid);
183 NSUUID *toNSUUID() const Q_DECL_NS_RETURNS_AUTORELEASED;
184#endif
185
186 uint data1 = 0;
187 ushort data2 = 0;
188 ushort data3 = 0;
189 uchar data4[8] = {};
190
191private:
192 static constexpr Id128Bytes bswap(Id128Bytes b)
193 {
194 // 128-bit byte swap
195 auto b0 = qbswap(b.data64[0]);
196 auto b1 = qbswap(b.data64[1]);
197 b.data64[0] = b1;
198 b.data64[1] = b0;
199 return b;
200 }
201};
202
204
205#ifndef QT_NO_DATASTREAM
206Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUuid &);
208#endif
209
210#ifndef QT_NO_DEBUG_STREAM
211Q_CORE_EXPORT QDebug operator<<(QDebug, const QUuid &);
212#endif
213
214Q_CORE_EXPORT size_t qHash(const QUuid &uuid, size_t seed = 0) noexcept;
215
216inline QUuid::QUuid(Id128Bytes uuid, QSysInfo::Endian order) noexcept
217{
219 uuid = bswap(uuid);
220 data1 = qFromBigEndian<quint32>(&uuid.data[0]);
221 data2 = qFromBigEndian<quint16>(&uuid.data[4]);
222 data3 = qFromBigEndian<quint16>(&uuid.data[6]);
223 memcpy(data4, &uuid.data[8], sizeof(data4));
224}
225
227{
228 Id128Bytes result = {};
229 qToBigEndian(data1, &result.data[0]);
230 qToBigEndian(data2, &result.data[4]);
231 qToBigEndian(data3, &result.data[6]);
232 memcpy(&result.data[8], data4, sizeof(data4));
234 return bswap(result);
235 return result;
236}
237
238inline QUuid QUuid::fromBytes(const void *bytes, QSysInfo::Endian order) noexcept
239{
240 Id128Bytes result = {};
241 memcpy(result.data, bytes, sizeof(result));
242 return QUuid(result, order);
243}
244
245#ifdef __SIZEOF_INT128__
246constexpr inline QUuid::QUuid(quint128 uuid, QSysInfo::Endian order) noexcept
247{
249 uuid = qbswap(uuid);
250 data1 = uint(uuid >> 96);
251 data2 = ushort(uuid >> 80);
252 data3 = ushort(uuid >> 64);
253 for (int i = 0; i < 8; ++i)
254 data4[i] = uchar(uuid >> (56 - i * 8));
255}
256
257constexpr inline quint128 QUuid::toUInt128(QSysInfo::Endian order) const noexcept
258{
259 quint128 result = {};
260 result = data1;
261 result <<= 32;
262 result |= (data2 << 16) | uint(data3);
263 result <<= 64;
264 for (int i = 0; i < 8; ++i)
265 result |= quint64(data4[i]) << (56 - i * 8);
267 return qbswap(result);
268 return result;
269}
270#endif
271
272inline bool operator<=(const QUuid &lhs, const QUuid &rhs) noexcept
273{ return !(rhs < lhs); }
274inline bool operator>=(const QUuid &lhs, const QUuid &rhs) noexcept
275{ return !(lhs < rhs); }
276
278
279#endif // QUUID_H
\inmodule QtCore
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QByteArray toUtf8() const &
Definition qstring.h:563
\inmodule QtCore
Definition qsysinfo.h:22
Endian
\value BigEndian Big-endian byte order (also called Network byte order) \value LittleEndian Little-en...
Definition qsysinfo.h:28
@ BigEndian
Definition qsysinfo.h:29
@ LittleEndian
Definition qsysinfo.h:30
\inmodule QtCore
Definition quuid.h:31
static QUuid createUuidV3(const QUuid &ns, const QString &baseData)
Definition quuid.h:165
constexpr bool operator!=(const QUuid &orig) const noexcept
Returns true if this QUuid and the other QUuid are different; otherwise returns false.
Definition quuid.h:121
Version
This enum defines the values used in the \l{Version field} {version field} of the UUID.
Definition quuid.h:42
Variant
This enum defines the values used in the \l{Variant field} {variant field} of the UUID.
Definition quuid.h:34
StringFormat
Definition quuid.h:52
constexpr QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) noexcept
Creates a UUID with the value specified by the parameters, l, w1, w2, b1, b2, b3, b4,...
Definition quuid.h:75
QUuid(QAnyStringView string) noexcept
Creates a QUuid object from the string text, which must be formatted as five hex fields separated by ...
Definition quuid.h:80
static QUuid createUuidV5(const QUuid &ns, const QByteArray &baseData)
Definition quuid.cpp:526
Id128Bytes toBytes(QSysInfo::Endian order=QSysInfo::BigEndian) const noexcept
Definition quuid.h:226
static QUuid createUuidV3(const QUuid &ns, const QByteArray &baseData)
Definition quuid.cpp:520
static QUuid createUuidV5(const QUuid &ns, const QString &baseData)
Definition quuid.h:171
static QUuid fromBytes(const void *bytes, QSysInfo::Endian order=QSysInfo::BigEndian) noexcept
Definition quuid.h:238
constexpr bool operator==(const QUuid &orig) const noexcept
Returns true if this QUuid and the other QUuid are identical; otherwise returns false.
Definition quuid.h:108
constexpr QUuid() noexcept
Creates the null UUID.
Definition quuid.h:73
std::list< QString >::iterator Name
Definition lalr.h:29
Combined button and popup list for selecting options.
bool isNull(const T &t)
Initialization
void toByteArray(QByteArray &)
Definition qctf_p.h:78
#define Q_DECL_NS_RETURNS_AUTORELEASED
#define Q_DECL_CF_RETURNS_RETAINED
constexpr bool operator!=(const timespec &t1, const timespec &t2)
#define Q_FORWARD_DECLARE_CF_TYPE(type)
#define Q_FORWARD_DECLARE_OBJC_CLASS(classname)
constexpr T qbswap(T source)
Definition qendian.h:103
constexpr T qToBigEndian(T source)
Definition qendian.h:172
GLboolean GLboolean GLboolean b
GLenum mode
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint GLdouble GLdouble w2
GLuint64EXT * result
[6]
GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint GLdouble w1
GLfixed GLfixed GLint GLint order
bool operator>(const QPoint &a, const QPoint &b)
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1219
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:144
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:163
unsigned int quint32
Definition qtypes.h:45
unsigned char uchar
Definition qtypes.h:27
unsigned short quint16
Definition qtypes.h:43
unsigned long ulong
Definition qtypes.h:30
unsigned long long quint64
Definition qtypes.h:56
unsigned int uint
Definition qtypes.h:29
unsigned short ushort
Definition qtypes.h:28
unsigned char quint8
Definition qtypes.h:41
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QUuid &)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QUuid &)
bool operator<=(const QUuid &lhs, const QUuid &rhs) noexcept
Definition quuid.h:272
Q_CORE_EXPORT size_t qHash(const QUuid &uuid, size_t seed=0) noexcept
bool operator>=(const QUuid &lhs, const QUuid &rhs) noexcept
Definition quuid.h:274
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
char * toString(const MyType &t)
[31]
\inmodule QtCore
Definition quuid.h:58