1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 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
48 typedef 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)
59 Q_FORWARD_DECLARE_CF_TYPE(CFUUID);
60 Q_FORWARD_DECLARE_OBJC_CLASS(NSUUID);
61 #endif
62 
63 QT_BEGIN_NAMESPACE
64 
65 
66 class Q_CORE_EXPORT QUuid
67 {
QUuid(Qt::Initialization)68     QUuid(Qt::Initialization) {}
69 public:
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 
QUuid()96     Q_DECL_CONSTEXPR QUuid() noexcept : data1(0), data2(0), data3(0), data4{0,0,0,0,0,0,0,0} {}
97 
QUuid(uint l,ushort w1,ushort w2,uchar b1,uchar b2,uchar b3,uchar b4,uchar b5,uchar b6,uchar b7,uchar b8)98     Q_DECL_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
QUuid()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     }
QUuid(uint l,ushort w1,ushort w2,uchar b1,uchar b2,uchar b3,uchar b4,uchar b5,uchar b6,uchar b7,uchar b8)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     QUuid(const QString &);
127     static QUuid fromString(QStringView string) noexcept;
128     static QUuid fromString(QLatin1String string) noexcept;
129     QUuid(const char *);
130     QString toString() const;
131     QString toString(StringFormat mode) const; // ### Qt6: merge with previous
132     QUuid(const QByteArray &);
133     QByteArray toByteArray() const;
134     QByteArray toByteArray(StringFormat mode) const; // ### Qt6: merge with previous
135     QByteArray toRfc4122() const;
136     static QUuid fromRfc4122(const QByteArray &);
137     bool isNull() const noexcept;
138 
139     Q_DECL_RELAXED_CONSTEXPR bool operator==(const QUuid &orig) const noexcept
140     {
141         if (data1 != orig.data1 || data2 != orig.data2 ||
142              data3 != orig.data3)
143             return false;
144 
145         for (uint i = 0; i < 8; i++)
146             if (data4[i] != orig.data4[i])
147                 return false;
148 
149         return true;
150     }
151 
152     Q_DECL_RELAXED_CONSTEXPR bool operator!=(const QUuid &orig) const noexcept
153     {
154         return !(*this == orig);
155     }
156 
157     bool operator<(const QUuid &other) const noexcept;
158     bool operator>(const QUuid &other) const noexcept;
159 
160 #if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC)
161     // On Windows we have a type GUID that is used by the platform API, so we
162     // provide convenience operators to cast from and to this type.
163 #if defined(Q_COMPILER_UNIFORM_INIT) && !defined(Q_CLANG_QDOC)
QUuid(const GUID & guid)164     Q_DECL_CONSTEXPR QUuid(const GUID &guid) noexcept
165         : data1(guid.Data1), data2(guid.Data2), data3(guid.Data3),
166           data4{guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
167                 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]} {}
168 #else
QUuid(const GUID & guid)169     QUuid(const GUID &guid) noexcept
170     {
171         data1 = guid.Data1;
172         data2 = guid.Data2;
173         data3 = guid.Data3;
174         for(int i = 0; i < 8; i++)
175             data4[i] = guid.Data4[i];
176     }
177 #endif
178 
179     Q_DECL_RELAXED_CONSTEXPR QUuid &operator=(const GUID &guid) noexcept
180     {
181         *this = QUuid(guid);
182         return *this;
183     }
184 
GUID()185     Q_DECL_RELAXED_CONSTEXPR operator GUID() const noexcept
186     {
187         GUID guid = { data1, data2, data3, { data4[0], data4[1], data4[2], data4[3], data4[4], data4[5], data4[6], data4[7] } };
188         return guid;
189     }
190 
191     Q_DECL_RELAXED_CONSTEXPR bool operator==(const GUID &guid) const noexcept
192     {
193         return *this == QUuid(guid);
194     }
195 
196     Q_DECL_RELAXED_CONSTEXPR bool operator!=(const GUID &guid) const noexcept
197     {
198         return !(*this == guid);
199     }
200 #endif
201     static QUuid createUuid();
202 #ifndef QT_BOOTSTRAPPED
203     static QUuid createUuidV3(const QUuid &ns, const QByteArray &baseData);
204 #endif
205     static QUuid createUuidV5(const QUuid &ns, const QByteArray &baseData);
206 #ifndef QT_BOOTSTRAPPED
createUuidV3(const QUuid & ns,const QString & baseData)207     static inline QUuid createUuidV3(const QUuid &ns, const QString &baseData)
208     {
209         return QUuid::createUuidV3(ns, baseData.toUtf8());
210     }
211 #endif
212 
createUuidV5(const QUuid & ns,const QString & baseData)213     static inline QUuid createUuidV5(const QUuid &ns, const QString &baseData)
214     {
215         return QUuid::createUuidV5(ns, baseData.toUtf8());
216     }
217 
218 
219     QUuid::Variant variant() const noexcept;
220     QUuid::Version version() const noexcept;
221 
222 #if defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
223     static QUuid fromCFUUID(CFUUIDRef uuid);
224     CFUUIDRef toCFUUID() const Q_DECL_CF_RETURNS_RETAINED;
225     static QUuid fromNSUUID(const NSUUID *uuid);
226     NSUUID *toNSUUID() const Q_DECL_NS_RETURNS_AUTORELEASED;
227 #endif
228 
229     uint    data1;
230     ushort  data2;
231     ushort  data3;
232     uchar   data4[8];
233 };
234 
235 Q_DECLARE_TYPEINFO(QUuid, Q_PRIMITIVE_TYPE);
236 
237 #ifndef QT_NO_DATASTREAM
238 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUuid &);
239 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUuid &);
240 #endif
241 
242 #ifndef QT_NO_DEBUG_STREAM
243 Q_CORE_EXPORT QDebug operator<<(QDebug, const QUuid &);
244 #endif
245 
246 Q_CORE_EXPORT uint qHash(const QUuid &uuid, uint seed = 0) noexcept;
247 
248 inline bool operator<=(const QUuid &lhs, const QUuid &rhs) noexcept
249 { return !(rhs < lhs); }
250 inline bool operator>=(const QUuid &lhs, const QUuid &rhs) noexcept
251 { return !(lhs < rhs); }
252 
253 QT_END_NAMESPACE
254 
255 #endif // QUUID_H
256