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.

112 lines
3.2KB

  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. Random r2;
  37. for (size_t i = 0; i < sizeof (uuid); ++i)
  38. uuid[i] = (uint8) (r1.nextInt() ^ r2.nextInt());
  39. }
  40. Uuid::~Uuid() noexcept {}
  41. Uuid::Uuid (const Uuid& other) noexcept
  42. {
  43. memcpy (uuid, other.uuid, sizeof (uuid));
  44. }
  45. Uuid& Uuid::operator= (const Uuid& other) noexcept
  46. {
  47. memcpy (uuid, other.uuid, sizeof (uuid));
  48. return *this;
  49. }
  50. bool Uuid::operator== (const Uuid& other) const noexcept { return memcmp (uuid, other.uuid, sizeof (uuid)) == 0; }
  51. bool Uuid::operator!= (const Uuid& other) const noexcept { return ! operator== (other); }
  52. bool Uuid::isNull() const noexcept
  53. {
  54. for (size_t i = 0; i < sizeof (uuid); ++i)
  55. if (uuid[i] != 0)
  56. return false;
  57. return true;
  58. }
  59. String Uuid::toString() const
  60. {
  61. return String::toHexString (uuid, sizeof (uuid), 0);
  62. }
  63. Uuid::Uuid (const String& uuidString)
  64. {
  65. operator= (uuidString);
  66. }
  67. Uuid& Uuid::operator= (const String& uuidString)
  68. {
  69. MemoryBlock mb;
  70. mb.loadFromHexString (uuidString);
  71. mb.ensureSize (sizeof (uuid), true);
  72. mb.copyTo (uuid, 0, sizeof (uuid));
  73. return *this;
  74. }
  75. Uuid::Uuid (const uint8* const rawData)
  76. {
  77. operator= (rawData);
  78. }
  79. Uuid& Uuid::operator= (const uint8* const rawData) noexcept
  80. {
  81. if (rawData != nullptr)
  82. memcpy (uuid, rawData, sizeof (uuid));
  83. else
  84. zeromem (uuid, sizeof (uuid));
  85. return *this;
  86. }