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.

148 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. Uuid::Uuid()
  20. {
  21. Random r;
  22. for (size_t i = 0; i < sizeof (uuid); ++i)
  23. uuid[i] = (uint8) (r.nextInt (256));
  24. // To make it RFC 4122 compliant, need to force a few bits...
  25. uuid[6] = (uuid[6] & 0x0f) | 0x40;
  26. uuid[8] = (uuid[8] & 0x3f) | 0x80;
  27. }
  28. Uuid::~Uuid() noexcept {}
  29. Uuid::Uuid (const Uuid& other) noexcept
  30. {
  31. memcpy (uuid, other.uuid, sizeof (uuid));
  32. }
  33. Uuid& Uuid::operator= (const Uuid& other) noexcept
  34. {
  35. memcpy (uuid, other.uuid, sizeof (uuid));
  36. return *this;
  37. }
  38. bool Uuid::operator== (const Uuid& other) const noexcept { return memcmp (uuid, other.uuid, sizeof (uuid)) == 0; }
  39. bool Uuid::operator!= (const Uuid& other) const noexcept { return ! operator== (other); }
  40. bool Uuid::operator< (const Uuid& other) const noexcept { return compare (other) < 0; }
  41. bool Uuid::operator> (const Uuid& other) const noexcept { return compare (other) > 0; }
  42. bool Uuid::operator<= (const Uuid& other) const noexcept { return compare (other) <= 0; }
  43. bool Uuid::operator>= (const Uuid& other) const noexcept { return compare (other) >= 0; }
  44. int Uuid::compare (Uuid other) const noexcept
  45. {
  46. for (size_t i = 0; i < sizeof (uuid); ++i)
  47. if (int diff = uuid[i] - (int) other.uuid[i])
  48. return diff > 0 ? 1 : -1;
  49. return 0;
  50. }
  51. Uuid Uuid::null() noexcept
  52. {
  53. return Uuid ((const uint8*) nullptr);
  54. }
  55. bool Uuid::isNull() const noexcept
  56. {
  57. for (auto i : uuid)
  58. if (i != 0)
  59. return false;
  60. return true;
  61. }
  62. String Uuid::getHexRegion (int start, int length) const
  63. {
  64. return String::toHexString (uuid + start, length, 0);
  65. }
  66. String Uuid::toString() const
  67. {
  68. return getHexRegion (0, 16);
  69. }
  70. String Uuid::toDashedString() const
  71. {
  72. return getHexRegion (0, 4)
  73. + "-" + getHexRegion (4, 2)
  74. + "-" + getHexRegion (6, 2)
  75. + "-" + getHexRegion (8, 2)
  76. + "-" + getHexRegion (10, 6);
  77. }
  78. Uuid::Uuid (const String& uuidString)
  79. {
  80. operator= (uuidString);
  81. }
  82. Uuid& Uuid::operator= (const String& uuidString)
  83. {
  84. MemoryBlock mb;
  85. mb.loadFromHexString (uuidString);
  86. mb.ensureSize (sizeof (uuid), true);
  87. mb.copyTo (uuid, 0, sizeof (uuid));
  88. return *this;
  89. }
  90. Uuid::Uuid (const uint8* const rawData) noexcept
  91. {
  92. operator= (rawData);
  93. }
  94. Uuid& Uuid::operator= (const uint8* const rawData) noexcept
  95. {
  96. if (rawData != nullptr)
  97. memcpy (uuid, rawData, sizeof (uuid));
  98. else
  99. zeromem (uuid, sizeof (uuid));
  100. return *this;
  101. }
  102. uint32 Uuid::getTimeLow() const noexcept { return ByteOrder::bigEndianInt (uuid); }
  103. uint16 Uuid::getTimeMid() const noexcept { return ByteOrder::bigEndianShort (uuid + 4); }
  104. uint16 Uuid::getTimeHighAndVersion() const noexcept { return ByteOrder::bigEndianShort (uuid + 6); }
  105. uint8 Uuid::getClockSeqAndReserved() const noexcept { return uuid[8]; }
  106. uint8 Uuid::getClockSeqLow() const noexcept { return uuid[9]; }
  107. uint64 Uuid::getNode() const noexcept { return (((uint64) ByteOrder::bigEndianShort (uuid + 10)) << 32) + ByteOrder::bigEndianInt (uuid + 12); }
  108. uint64 Uuid::hash() const noexcept
  109. {
  110. uint64 result = 0;
  111. for (auto n : uuid)
  112. result = ((uint64) 101) * result + n;
  113. return result;
  114. }
  115. } // namespace juce