-rw-r--r-- | qmake/tools/quuid.cpp | 234 |
1 files changed, 211 insertions, 23 deletions
diff --git a/qmake/tools/quuid.cpp b/qmake/tools/quuid.cpp index fd99abf..199bfa5 100644 --- a/qmake/tools/quuid.cpp +++ b/qmake/tools/quuid.cpp | |||
@@ -1,3 +1,3 @@ | |||
1 | /**************************************************************************** | 1 | /**************************************************************************** |
2 | ** $Id$ | 2 | ** |
3 | ** | 3 | ** |
@@ -37,3 +37,3 @@ | |||
37 | 37 | ||
38 | #include <qdatastream.h> | 38 | #include "qdatastream.h" |
39 | 39 | ||
@@ -41,17 +41,23 @@ | |||
41 | \class QUuid quuid.h | 41 | \class QUuid quuid.h |
42 | \reentrant | ||
43 | \brief The QUuid class defines a Universally Unique Identifier (UUID). | 42 | \brief The QUuid class defines a Universally Unique Identifier (UUID). |
44 | 43 | ||
45 | \internal | 44 | \reentrant |
45 | |||
46 | For objects or declarations that must be uniquely identified, | ||
47 | UUIDs (also known as GUIDs) are widely used in order to assign a | ||
48 | fixed and easy to compare value to the object or declaration. The | ||
49 | 128-bit value of a UUID is generated by an algorithm that | ||
50 | guarantees that the value is unique. | ||
46 | 51 | ||
47 | For objects or declarations that need to be identified uniquely, UUIDs (also known as GUIDs) are widely | 52 | In Qt, UUIDs are wrapped by the QUuid struct which provides |
48 | used in order to assign a fixed and easy to compare value to this object or declaration. The 128bit value | 53 | convenience functions for handling UUIDs. Most platforms provide a |
49 | of an UUID is generated by an algorithm that guarantees a value that is unique in time and space. | 54 | tool to generate new UUIDs, for example, uuidgen and guidgen. |
50 | 55 | ||
51 | In Qt, UUIDs are wrapped by the QUuid struct which provides convenience functions for comparing and coping | 56 | UUIDs generated by QUuid, are based on the \c Random version of the |
52 | this value. The QUuid struct is used in Qt's component model to identify interfaces. Most platforms provide a tool to | 57 | \c DCE (Distributed Computing Environment) standard. |
53 | generate new UUIDs (uuidgen, guidgen), and the Qt distribution includes a graphical tool \e quuidgen that generates | ||
54 | the UUIDs in a programmer friendly format. | ||
55 | 58 | ||
56 | \sa QUnknownInterface | 59 | UUIDs can be constructed from numeric values or from strings, or |
60 | using the static createUuid() function. They can be converted to a | ||
61 | string with toString(). UUIDs have a variant() and a version(), | ||
62 | and null UUIDs return TRUE from isNull(). | ||
57 | */ | 63 | */ |
@@ -67,4 +73,5 @@ | |||
67 | 73 | ||
68 | Creates an UUID with the value specified by the parameters, \a l, \a | 74 | Creates a UUID with the value specified by the parameters, \a l, |
69 | w1, \a w2, \a b1, \a b2, \a b3, \a b4, \a b5, \a b6, \a b7, \a b8. | 75 | \a w1, \a w2, \a b1, \a b2, \a b3, \a b4, \a b5, \a b6, \a b7, \a |
76 | b8. | ||
70 | 77 | ||
@@ -84,5 +91,6 @@ | |||
84 | /*! | 91 | /*! |
85 | Creates a QUuid object from the string \a text. Right now, the function | 92 | Creates a QUuid object from the string \a text. The function can |
86 | can only convert the format {12345678-1234-1234-1234-123456789ABC} and | 93 | only convert a string in the format |
87 | will create the null UUID when the conversion fails. | 94 | {HHHHHHHH-HHHH-HHHH-HHHH-HHHHHHHHHHHH} (where 'H' stands for a hex |
95 | digit). If the conversion fails a null UUID is created. | ||
88 | */ | 96 | */ |
@@ -91,3 +99,11 @@ QUuid::QUuid( const QString &text ) | |||
91 | bool ok; | 99 | bool ok; |
100 | if ( text.isEmpty() ) { | ||
101 | *this = QUuid(); | ||
102 | return; | ||
103 | } | ||
92 | QString temp = text.upper(); | 104 | QString temp = text.upper(); |
105 | if ( temp[0] != '{' ) | ||
106 | temp = "{" + text; | ||
107 | if ( text[(int)text.length()-1] != '}' ) | ||
108 | temp += "}"; | ||
93 | 109 | ||
@@ -129,3 +145,3 @@ QUuid::QUuid( const QString &text ) | |||
129 | /*! | 145 | /*! |
130 | \overload | 146 | \internal |
131 | */ | 147 | */ |
@@ -145,3 +161,4 @@ QUuid::QUuid( const char *text ) | |||
145 | 161 | ||
146 | Returns TRUE if this QUuid and the \a other QUuid are identical, otherwise returns FALSE. | 162 | Returns TRUE if this QUuid and the \a other QUuid are identical; |
163 | otherwise returns FALSE. | ||
147 | */ | 164 | */ |
@@ -151,3 +168,4 @@ QUuid::QUuid( const char *text ) | |||
151 | 168 | ||
152 | Returns TRUE if this QUuid and the \a other QUuid are different, otherwise returns FALSE. | 169 | Returns TRUE if this QUuid and the \a other QUuid are different; |
170 | otherwise returns FALSE. | ||
153 | */ | 171 | */ |
@@ -186,3 +204,3 @@ QString QUuid::toString() const | |||
186 | \relates QUuid | 204 | \relates QUuid |
187 | Writes the \a id to the datastream \a s. | 205 | Writes the uuid \a id to the datastream \a s. |
188 | */ | 206 | */ |
@@ -200,3 +218,3 @@ QDataStream &operator<<( QDataStream &s, const QUuid &id ) | |||
200 | \relates QUuid | 218 | \relates QUuid |
201 | Reads a universally unique id from from the stream \a s into \a id. | 219 | Reads uuid from from the stream \a s into \a id. |
202 | */ | 220 | */ |
@@ -222,3 +240,4 @@ QDataStream &operator>>( QDataStream &s, QUuid &id ) | |||
222 | /*! | 240 | /*! |
223 | Returns TRUE if this is the null UUID {00000000-0000-0000-0000-000000000000}, otherwise returns FALSE. | 241 | Returns TRUE if this is the null UUID |
242 | {00000000-0000-0000-0000-000000000000}; otherwise returns FALSE. | ||
224 | */ | 243 | */ |
@@ -230 +249,170 @@ bool QUuid::isNull() const | |||
230 | } | 249 | } |
250 | |||
251 | /*! | ||
252 | \enum QUuid::Variant | ||
253 | |||
254 | This enum defines the variant of the UUID, which is the scheme | ||
255 | which defines the layout of the 128-bits value. | ||
256 | |||
257 | \value VarUnknown Variant is unknown | ||
258 | \value NCS Reserved for NCS (Network Computing System) backward compatibility | ||
259 | \value DCE Distributed Computing Environment, the scheme used by QUuid | ||
260 | \value Microsoft Reserved for Microsoft backward compatibility (GUID) | ||
261 | \value Reserved Reserved for future definition | ||
262 | */ | ||
263 | |||
264 | /*! | ||
265 | \enum QUuid::Version | ||
266 | |||
267 | This enum defines the version of the UUID. | ||
268 | |||
269 | \value VerUnknown Version is unknown | ||
270 | \value Time Time-based, by using timestamp, clock sequence, and | ||
271 | MAC network card address (if available) for the node sections | ||
272 | \value EmbeddedPOSIX DCE Security version, with embedded POSIX UUIDs | ||
273 | \value Name Name-based, by using values from a name for all sections | ||
274 | \value Random Random-based, by using random numbers for all sections | ||
275 | */ | ||
276 | |||
277 | /*! | ||
278 | \fn QUuid::Variant QUuid::variant() const | ||
279 | |||
280 | Returns the variant of the UUID. | ||
281 | The null UUID is considered to be of an unknown variant. | ||
282 | |||
283 | \sa version() | ||
284 | */ | ||
285 | QUuid::Variant QUuid::variant() const | ||
286 | { | ||
287 | if ( isNull() ) | ||
288 | return VarUnknown; | ||
289 | // Check the 3 MSB of data4[0] | ||
290 | if ( (data4[0] & 0x80) == 0x00 ) return NCS; | ||
291 | else if ( (data4[0] & 0xC0) == 0x80 ) return DCE; | ||
292 | else if ( (data4[0] & 0xE0) == 0xC0 ) return Microsoft; | ||
293 | else if ( (data4[0] & 0xE0) == 0xE0 ) return Reserved; | ||
294 | return VarUnknown; | ||
295 | } | ||
296 | |||
297 | /*! | ||
298 | \fn QUuid::Version QUuid::version() const | ||
299 | |||
300 | Returns the version of the UUID, if the UUID is of the DCE | ||
301 | variant; otherwise returns VerUnknown. | ||
302 | |||
303 | \sa variant() | ||
304 | */ | ||
305 | QUuid::Version QUuid::version() const | ||
306 | { | ||
307 | // Check the 4 MSB of data3 | ||
308 | Version ver = (Version)(data3>>12); | ||
309 | if ( isNull() | ||
310 | || (variant() != DCE) | ||
311 | || ver < Time | ||
312 | || ver > Random ) | ||
313 | return VerUnknown; | ||
314 | return ver; | ||
315 | } | ||
316 | |||
317 | /*! | ||
318 | \fn bool QUuid::operator<(const QUuid &other) const | ||
319 | |||
320 | Returns TRUE if this QUuid is of the same variant, | ||
321 | and lexicographically before the \a other QUuid; | ||
322 | otherwise returns FALSE. | ||
323 | |||
324 | \sa variant() | ||
325 | */ | ||
326 | #define ISLESS(f1, f2) if (f1!=f2) return (f1<f2); | ||
327 | bool QUuid::operator<(const QUuid &other ) const | ||
328 | { | ||
329 | if ( variant() != other.variant() ) | ||
330 | return FALSE; | ||
331 | |||
332 | ISLESS( data1, other.data1 ); | ||
333 | ISLESS( data2, other.data2 ); | ||
334 | ISLESS( data3, other.data3 ); | ||
335 | for ( int n = 0; n < 8; n++ ) { | ||
336 | ISLESS( data4[n], other.data4[n] ); | ||
337 | } | ||
338 | return FALSE; | ||
339 | } | ||
340 | |||
341 | /*! | ||
342 | \fn bool QUuid::operator>(const QUuid &other) const | ||
343 | |||
344 | Returns TRUE if this QUuid is of the same variant, | ||
345 | and lexicographically after the \a other QUuid; | ||
346 | otherwise returns FALSE. | ||
347 | |||
348 | \sa variant() | ||
349 | */ | ||
350 | #define ISMORE(f1, f2) if (f1!=f2) return (f1>f2); | ||
351 | bool QUuid::operator>(const QUuid &other ) const | ||
352 | { | ||
353 | if ( variant() != other.variant() ) | ||
354 | return FALSE; | ||
355 | |||
356 | ISMORE( data1, other.data1 ); | ||
357 | ISMORE( data2, other.data2 ); | ||
358 | ISMORE( data3, other.data3 ); | ||
359 | for ( int n = 0; n < 8; n++ ) { | ||
360 | ISMORE( data4[n], other.data4[n] ); | ||
361 | } | ||
362 | return FALSE; | ||
363 | } | ||
364 | |||
365 | /*! | ||
366 | \fn QUuid QUuid::createUuid() | ||
367 | |||
368 | Returns a new UUID of \c DCE variant, and \c Random type. The | ||
369 | UUIDs generated are based on the platform specific pseudo-random | ||
370 | generator, which is usually not a cryptographic-quality random | ||
371 | number generator. Therefore, a UUID is not guaranteed to be unique | ||
372 | cross application instances. | ||
373 | |||
374 | On Windows, the new UUID is extremely likely to be unique on the | ||
375 | same or any other system, networked or not. | ||
376 | |||
377 | \sa variant(), version() | ||
378 | */ | ||
379 | #if defined(Q_OS_WIN32) | ||
380 | #include <objbase.h> // For CoCreateGuid | ||
381 | QUuid QUuid::createUuid() | ||
382 | { | ||
383 | GUID guid; | ||
384 | CoCreateGuid( &guid ); | ||
385 | QUuid result = guid; | ||
386 | return result; | ||
387 | } | ||
388 | #else // !Q_OS_WIN32 | ||
389 | #include "qdatetime.h" | ||
390 | #include "stdlib.h" // For srand/rand | ||
391 | QUuid QUuid::createUuid() | ||
392 | { | ||
393 | static const int intbits = sizeof(int)*8; | ||
394 | static int randbits = 0; | ||
395 | if ( !randbits ) { | ||
396 | int max = RAND_MAX; | ||
397 | do { ++randbits; } while ( (max=max>>1) ); | ||
398 | srand( (uint)QDateTime::currentDateTime().toTime_t() ); | ||
399 | rand(); // Skip first | ||
400 | } | ||
401 | |||
402 | QUuid result; | ||
403 | uint *data = &(result.data1); | ||
404 | int chunks = 16 / sizeof(uint); | ||
405 | while ( chunks-- ) { | ||
406 | uint randNumber = 0; | ||
407 | for ( int filled = 0; filled < intbits; filled += randbits ) | ||
408 | randNumber |= rand()<<filled; | ||
409 | *(data+chunks) = randNumber; | ||
410 | } | ||
411 | |||
412 | result.data4[0] = (result.data4[0] & 0x3F) | 0x80;// UV_DCE | ||
413 | result.data3 = (result.data3 & 0x0FFF) | 0x4000;// UV_Random | ||
414 | |||
415 | return result; | ||
416 | } | ||
417 | #endif // !Q_OS_WIN32 | ||
418 | |||