The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. namespace
  20. {
  21. int64 getRandomSeedFromMACAddresses()
  22. {
  23. Array<MACAddress> result;
  24. MACAddress::findAllAddresses (result);
  25. Random r;
  26. for (int i = 0; i < result.size(); ++i)
  27. r.combineSeed (result[i].toInt64());
  28. return r.nextInt64();
  29. }
  30. }
  31. //==============================================================================
  32. Uuid::Uuid()
  33. {
  34. // The normal random seeding is pretty good, but we'll throw some MAC addresses
  35. // into the mix too, to make it very very unlikely that two UUIDs will ever be the same..
  36. static Random r1 (getRandomSeedFromMACAddresses());
  37. value.asInt64[0] = r1.nextInt64();
  38. value.asInt64[1] = r1.nextInt64();
  39. Random r2;
  40. for (int i = 4; --i >= 0;)
  41. value.asInt[i] ^= r2.nextInt();
  42. }
  43. Uuid::~Uuid() noexcept
  44. {
  45. }
  46. Uuid::Uuid (const Uuid& other)
  47. : value (other.value)
  48. {
  49. }
  50. Uuid& Uuid::operator= (const Uuid& other)
  51. {
  52. value = other.value;
  53. return *this;
  54. }
  55. bool Uuid::operator== (const Uuid& other) const
  56. {
  57. return value.asInt64[0] == other.value.asInt64[0]
  58. && value.asInt64[1] == other.value.asInt64[1];
  59. }
  60. bool Uuid::operator!= (const Uuid& other) const
  61. {
  62. return ! operator== (other);
  63. }
  64. bool Uuid::isNull() const noexcept
  65. {
  66. return (value.asInt64 [0] == 0) && (value.asInt64 [1] == 0);
  67. }
  68. //==============================================================================
  69. String Uuid::toString() const
  70. {
  71. return String::toHexString (value.asBytes, sizeof (value.asBytes), 0);
  72. }
  73. Uuid::Uuid (const String& uuidString)
  74. {
  75. operator= (uuidString);
  76. }
  77. Uuid& Uuid::operator= (const String& uuidString)
  78. {
  79. MemoryBlock mb;
  80. mb.loadFromHexString (uuidString);
  81. mb.ensureSize (sizeof (value.asBytes), true);
  82. mb.copyTo (value.asBytes, 0, sizeof (value.asBytes));
  83. return *this;
  84. }
  85. //==============================================================================
  86. Uuid::Uuid (const uint8* const rawData)
  87. {
  88. operator= (rawData);
  89. }
  90. Uuid& Uuid::operator= (const uint8* const rawData)
  91. {
  92. if (rawData != nullptr)
  93. memcpy (value.asBytes, rawData, sizeof (value.asBytes));
  94. else
  95. zeromem (value.asBytes, sizeof (value.asBytes));
  96. return *this;
  97. }
  98. END_JUCE_NAMESPACE