2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "juce_StandardHeader.h"
30 #include "juce_Uuid.h"
31 #include "../maths/juce_Random.h"
32 #include "juce_Time.h"
33 #include "../io/network/juce_MACAddress.h"
34 #include "../memory/juce_MemoryBlock.h"
37 //==============================================================================
40 // Mix up any available MAC addresses with some time-based pseudo-random numbers
41 // to make it very very unlikely that two UUIDs will ever be the same..
43 static int64 macAddresses
[2];
44 static bool hasCheckedMacAddresses
= false;
46 if (! hasCheckedMacAddresses
)
48 hasCheckedMacAddresses
= true;
50 Array
<MACAddress
> result
;
51 MACAddress::findAllAddresses (result
);
53 for (int i
= 0; i
< numElementsInArray (macAddresses
); ++i
)
54 macAddresses
[i
] = result
[i
].toInt64();
57 value
.asInt64
[0] = macAddresses
[0];
58 value
.asInt64
[1] = macAddresses
[1];
60 // We'll use both a local RNG that is re-seeded, plus the shared RNG,
61 // whose seed will carry over between calls to this method.
63 Random
r (macAddresses
[0] ^ macAddresses
[1]
64 ^ Random::getSystemRandom().nextInt64());
66 for (int i
= 4; --i
>= 0;)
68 r
.setSeedRandomly(); // calling this repeatedly improves randomness
69 value
.asInt
[i
] ^= r
.nextInt();
70 value
.asInt
[i
] ^= Random::getSystemRandom().nextInt();
74 Uuid::~Uuid() noexcept
78 Uuid::Uuid (const Uuid
& other
)
83 Uuid
& Uuid::operator= (const Uuid
& other
)
89 bool Uuid::operator== (const Uuid
& other
) const
91 return value
.asInt64
[0] == other
.value
.asInt64
[0]
92 && value
.asInt64
[1] == other
.value
.asInt64
[1];
95 bool Uuid::operator!= (const Uuid
& other
) const
97 return ! operator== (other
);
100 bool Uuid::isNull() const noexcept
102 return (value
.asInt64
[0] == 0) && (value
.asInt64
[1] == 0);
105 //==============================================================================
106 String
Uuid::toString() const
108 return String::toHexString (value
.asBytes
, sizeof (value
.asBytes
), 0);
111 Uuid::Uuid (const String
& uuidString
)
113 operator= (uuidString
);
116 Uuid
& Uuid::operator= (const String
& uuidString
)
119 mb
.loadFromHexString (uuidString
);
120 mb
.ensureSize (sizeof (value
.asBytes
), true);
121 mb
.copyTo (value
.asBytes
, 0, sizeof (value
.asBytes
));
125 //==============================================================================
126 Uuid::Uuid (const uint8
* const rawData
)
131 Uuid
& Uuid::operator= (const uint8
* const rawData
)
133 if (rawData
!= nullptr)
134 memcpy (value
.asBytes
, rawData
, sizeof (value
.asBytes
));
136 zeromem (value
.asBytes
, sizeof (value
.asBytes
));