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.

115 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. namespace
  22. {
  23. int64 getRandomSeedFromMACAddresses()
  24. {
  25. Array<MACAddress> result;
  26. MACAddress::findAllAddresses (result);
  27. Random r;
  28. for (int i = 0; i < result.size(); ++i)
  29. r.combineSeed (result[i].toInt64());
  30. return r.nextInt64();
  31. }
  32. }
  33. //==============================================================================
  34. Uuid::Uuid()
  35. {
  36. // The normal random seeding is pretty good, but we'll throw some MAC addresses
  37. // into the mix too, to make it very very unlikely that two UUIDs will ever be the same..
  38. static Random r1 (getRandomSeedFromMACAddresses());
  39. Random r2;
  40. for (size_t i = 0; i < sizeof (uuid); ++i)
  41. uuid[i] = (uint8) (r1.nextInt() ^ r2.nextInt());
  42. }
  43. Uuid::~Uuid() noexcept {}
  44. Uuid::Uuid (const Uuid& other) noexcept
  45. {
  46. memcpy (uuid, other.uuid, sizeof (uuid));
  47. }
  48. Uuid& Uuid::operator= (const Uuid& other) noexcept
  49. {
  50. memcpy (uuid, other.uuid, sizeof (uuid));
  51. return *this;
  52. }
  53. bool Uuid::operator== (const Uuid& other) const noexcept { return memcmp (uuid, other.uuid, sizeof (uuid)) == 0; }
  54. bool Uuid::operator!= (const Uuid& other) const noexcept { return ! operator== (other); }
  55. bool Uuid::isNull() const noexcept
  56. {
  57. for (size_t i = 0; i < sizeof (uuid); ++i)
  58. if (uuid[i] != 0)
  59. return false;
  60. return true;
  61. }
  62. String Uuid::toString() const
  63. {
  64. return String::toHexString (uuid, sizeof (uuid), 0);
  65. }
  66. Uuid::Uuid (const String& uuidString)
  67. {
  68. operator= (uuidString);
  69. }
  70. Uuid& Uuid::operator= (const String& uuidString)
  71. {
  72. MemoryBlock mb;
  73. mb.loadFromHexString (uuidString);
  74. mb.ensureSize (sizeof (uuid), true);
  75. mb.copyTo (uuid, 0, sizeof (uuid));
  76. return *this;
  77. }
  78. Uuid::Uuid (const uint8* const rawData)
  79. {
  80. operator= (rawData);
  81. }
  82. Uuid& Uuid::operator= (const uint8* const rawData) noexcept
  83. {
  84. if (rawData != nullptr)
  85. memcpy (uuid, rawData, sizeof (uuid));
  86. else
  87. zeromem (uuid, sizeof (uuid));
  88. return *this;
  89. }