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.

125 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. namespace
  19. {
  20. int64 getRandomSeedFromMACAddresses()
  21. {
  22. Array<MACAddress> result;
  23. MACAddress::findAllAddresses (result);
  24. Random r;
  25. for (int i = 0; i < result.size(); ++i)
  26. r.combineSeed (result[i].toInt64());
  27. return r.nextInt64();
  28. }
  29. }
  30. //==============================================================================
  31. Uuid::Uuid()
  32. {
  33. // The normal random seeding is pretty good, but we'll throw some MAC addresses
  34. // into the mix too, to make it very very unlikely that two UUIDs will ever be the same..
  35. static Random r1 (getRandomSeedFromMACAddresses());
  36. value.asInt64[0] = r1.nextInt64();
  37. value.asInt64[1] = r1.nextInt64();
  38. Random r2;
  39. for (int i = 4; --i >= 0;)
  40. value.asInt[i] ^= r2.nextInt();
  41. }
  42. Uuid::~Uuid() noexcept
  43. {
  44. }
  45. Uuid::Uuid (const Uuid& other)
  46. : value (other.value)
  47. {
  48. }
  49. Uuid& Uuid::operator= (const Uuid& other)
  50. {
  51. value = other.value;
  52. return *this;
  53. }
  54. bool Uuid::operator== (const Uuid& other) const
  55. {
  56. return value.asInt64[0] == other.value.asInt64[0]
  57. && value.asInt64[1] == other.value.asInt64[1];
  58. }
  59. bool Uuid::operator!= (const Uuid& other) const
  60. {
  61. return ! operator== (other);
  62. }
  63. bool Uuid::isNull() const noexcept
  64. {
  65. return (value.asInt64 [0] == 0) && (value.asInt64 [1] == 0);
  66. }
  67. //==============================================================================
  68. String Uuid::toString() const
  69. {
  70. return String::toHexString (value.asBytes, sizeof (value.asBytes), 0);
  71. }
  72. Uuid::Uuid (const String& uuidString)
  73. {
  74. operator= (uuidString);
  75. }
  76. Uuid& Uuid::operator= (const String& uuidString)
  77. {
  78. MemoryBlock mb;
  79. mb.loadFromHexString (uuidString);
  80. mb.ensureSize (sizeof (value.asBytes), true);
  81. mb.copyTo (value.asBytes, 0, sizeof (value.asBytes));
  82. return *this;
  83. }
  84. //==============================================================================
  85. Uuid::Uuid (const uint8* const rawData)
  86. {
  87. operator= (rawData);
  88. }
  89. Uuid& Uuid::operator= (const uint8* const rawData)
  90. {
  91. if (rawData != nullptr)
  92. memcpy (value.asBytes, rawData, sizeof (value.asBytes));
  93. else
  94. zeromem (value.asBytes, sizeof (value.asBytes));
  95. return *this;
  96. }