1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QUUID_H
41#define QUUID_H
42
43#include <QtCore/qstring.h>
44
45#if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC)
46#ifndef GUID_DEFINED
47#define GUID_DEFINED
48typedef struct _GUID
49{
50 ulong Data1;
51 ushort Data2;
52 ushort Data3;
53 uchar Data4[8];
54} GUID, *REFGUID, *LPGUID;
55#endif
56#endif
57
58#if defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
59Q_FORWARD_DECLARE_CF_TYPE(CFUUID);
60Q_FORWARD_DECLARE_OBJC_CLASS(NSUUID);
61#endif
62
63QT_BEGIN_NAMESPACE
64
65
66class Q_CORE_EXPORT QUuid
67{
68 QUuid(Qt::Initialization) {}
69public:
70 enum Variant {
71 VarUnknown =-1,
72 NCS = 0, // 0 - -
73 DCE = 2, // 1 0 -
74 Microsoft = 6, // 1 1 0
75 Reserved = 7 // 1 1 1
76 };
77
78 enum Version {
79 VerUnknown =-1,
80 Time = 1, // 0 0 0 1
81 EmbeddedPOSIX = 2, // 0 0 1 0
82 Md5 = 3, // 0 0 1 1
83 Name = Md5,
84 Random = 4, // 0 1 0 0
85 Sha1 = 5 // 0 1 0 1
86 };
87
88 enum StringFormat {
89 WithBraces = 0,
90 WithoutBraces = 1,
91 Id128 = 3
92 };
93
94#if defined(Q_COMPILER_UNIFORM_INIT) && !defined(Q_CLANG_QDOC)
95
96 constexpr QUuid() noexcept : data1(0), data2(0), data3(0), data4{0,0,0,0,0,0,0,0} {}
97
98 constexpr QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3,
99 uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) noexcept
100 : data1(l), data2(w1), data3(w2), data4{b1, b2, b3, b4, b5, b6, b7, b8} {}
101#else
102 QUuid() noexcept
103 {
104 data1 = 0;
105 data2 = 0;
106 data3 = 0;
107 for (int i = 0; i < 8; i++)
108 data4[i] = 0;
109 }
110 QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) noexcept
111 {
112 data1 = l;
113 data2 = w1;
114 data3 = w2;
115 data4[0] = b1;
116 data4[1] = b2;
117 data4[2] = b3;
118 data4[3] = b4;
119 data4[4] = b5;
120 data4[5] = b6;
121 data4[6] = b7;
122 data4[7] = b8;
123 }
124#endif
125
126 explicit QUuid(const QString &);
127 static QUuid fromString(QStringView string) noexcept;
128 static QUuid fromString(QLatin1String string) noexcept;
129 explicit QUuid(const char *);
130 QString toString(StringFormat mode = WithBraces) const;
131 explicit QUuid(const QByteArray &);
132 QByteArray toByteArray(StringFormat mode = WithBraces) const;
133 QByteArray toRfc4122() const;
134 static QUuid fromRfc4122(const QByteArray &);
135 bool isNull() const noexcept;
136
137 constexpr bool operator==(const QUuid &orig) const noexcept
138 {
139 if (data1 != orig.data1 || data2 != orig.data2 ||
140 data3 != orig.data3)
141 return false;
142
143 for (uint i = 0; i < 8; i++)
144 if (data4[i] != orig.data4[i])
145 return false;
146
147 return true;
148 }
149
150 constexpr bool operator!=(const QUuid &orig) const noexcept
151 {
152 return !(*this == orig);
153 }
154
155 bool operator<(const QUuid &other) const noexcept;
156 bool operator>(const QUuid &other) const noexcept;
157
158#if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC)
159 // On Windows we have a type GUID that is used by the platform API, so we
160 // provide convenience operators to cast from and to this type.
161#if defined(Q_COMPILER_UNIFORM_INIT) && !defined(Q_CLANG_QDOC)
162 constexpr QUuid(const GUID &guid) noexcept
163 : data1(guid.Data1), data2(guid.Data2), data3(guid.Data3),
164 data4{guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
165 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]} {}
166#else
167 QUuid(const GUID &guid) noexcept
168 {
169 data1 = guid.Data1;
170 data2 = guid.Data2;
171 data3 = guid.Data3;
172 for (int i = 0; i < 8; i++)
173 data4[i] = guid.Data4[i];
174 }
175#endif
176
177 constexpr QUuid &operator=(const GUID &guid) noexcept
178 {
179 *this = QUuid(guid);
180 return *this;
181 }
182
183 constexpr operator GUID() const noexcept
184 {
185 GUID guid = { data1, data2, data3, { data4[0], data4[1], data4[2], data4[3], data4[4], data4[5], data4[6], data4[7] } };
186 return guid;
187 }
188
189 constexpr bool operator==(const GUID &guid) const noexcept
190 {
191 return *this == QUuid(guid);
192 }
193
194 constexpr bool operator!=(const GUID &guid) const noexcept
195 {
196 return !(*this == guid);
197 }
198#endif
199 static QUuid createUuid();
200#ifndef QT_BOOTSTRAPPED
201 static QUuid createUuidV3(const QUuid &ns, const QByteArray &baseData);
202#endif
203 static QUuid createUuidV5(const QUuid &ns, const QByteArray &baseData);
204#ifndef QT_BOOTSTRAPPED
205 static inline QUuid createUuidV3(const QUuid &ns, const QString &baseData)
206 {
207 return QUuid::createUuidV3(ns, baseData.toUtf8());
208 }
209#endif
210
211 static inline QUuid createUuidV5(const QUuid &ns, const QString &baseData)
212 {
213 return QUuid::createUuidV5(ns, baseData.toUtf8());
214 }
215
216 QUuid::Variant variant() const noexcept;
217 QUuid::Version version() const noexcept;
218
219#if defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
220 static QUuid fromCFUUID(CFUUIDRef uuid);
221 CFUUIDRef toCFUUID() const Q_DECL_CF_RETURNS_RETAINED;
222 static QUuid fromNSUUID(const NSUUID *uuid);
223 NSUUID *toNSUUID() const Q_DECL_NS_RETURNS_AUTORELEASED;
224#endif
225
226 uint data1;
227 ushort data2;
228 ushort data3;
229 uchar data4[8];
230};
231
232Q_DECLARE_TYPEINFO(QUuid, Q_PRIMITIVE_TYPE);
233
234#ifndef QT_NO_DATASTREAM
235Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUuid &);
236Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUuid &);
237#endif
238
239#ifndef QT_NO_DEBUG_STREAM
240Q_CORE_EXPORT QDebug operator<<(QDebug, const QUuid &);
241#endif
242
243Q_CORE_EXPORT size_t qHash(const QUuid &uuid, size_t seed = 0) noexcept;
244
245inline bool operator<=(const QUuid &lhs, const QUuid &rhs) noexcept
246{ return !(rhs < lhs); }
247inline bool operator>=(const QUuid &lhs, const QUuid &rhs) noexcept
248{ return !(lhs < rhs); }
249
250QT_END_NAMESPACE
251
252#endif // QUUID_H
253