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.

133 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. Uuid::Uuid()
  18. {
  19. Random r;
  20. for (size_t i = 0; i < sizeof (uuid); ++i)
  21. uuid[i] = (uint8) (r.nextInt (256));
  22. // To make it RFC 4122 compliant, need to force a few bits...
  23. uuid[6] = (uuid[6] & 0x0f) | 0x40;
  24. uuid[8] = (uuid[8] & 0x3f) | 0x80;
  25. }
  26. Uuid::~Uuid() noexcept {}
  27. Uuid::Uuid (const Uuid& other) noexcept
  28. {
  29. memcpy (uuid, other.uuid, sizeof (uuid));
  30. }
  31. Uuid& Uuid::operator= (const Uuid& other) noexcept
  32. {
  33. memcpy (uuid, other.uuid, sizeof (uuid));
  34. return *this;
  35. }
  36. bool Uuid::operator== (const Uuid& other) const noexcept { return memcmp (uuid, other.uuid, sizeof (uuid)) == 0; }
  37. bool Uuid::operator!= (const Uuid& other) const noexcept { return ! operator== (other); }
  38. bool Uuid::operator< (const Uuid& other) const noexcept { return compare (other) < 0; }
  39. bool Uuid::operator> (const Uuid& other) const noexcept { return compare (other) > 0; }
  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. int Uuid::compare (Uuid other) const noexcept
  43. {
  44. for (size_t i = 0; i < sizeof (uuid); ++i)
  45. if (int diff = uuid[i] - (int) other.uuid[i])
  46. return diff > 0 ? 1 : -1;
  47. return 0;
  48. }
  49. Uuid Uuid::null() noexcept
  50. {
  51. return Uuid ((const uint8*) nullptr);
  52. }
  53. bool Uuid::isNull() const noexcept
  54. {
  55. for (auto i : uuid)
  56. if (i != 0)
  57. return false;
  58. return true;
  59. }
  60. String Uuid::getHexRegion (int start, int length) const
  61. {
  62. return String::toHexString (uuid + start, length, 0);
  63. }
  64. String Uuid::toString() const
  65. {
  66. return getHexRegion (0, 16);
  67. }
  68. String Uuid::toDashedString() const
  69. {
  70. return getHexRegion (0, 4)
  71. + "-" + getHexRegion (4, 2)
  72. + "-" + getHexRegion (6, 2)
  73. + "-" + getHexRegion (8, 2)
  74. + "-" + getHexRegion (10, 6);
  75. }
  76. Uuid::Uuid (const String& uuidString)
  77. {
  78. operator= (uuidString);
  79. }
  80. Uuid& Uuid::operator= (const String& uuidString)
  81. {
  82. MemoryBlock mb;
  83. mb.loadFromHexString (uuidString);
  84. mb.ensureSize (sizeof (uuid), true);
  85. mb.copyTo (uuid, 0, sizeof (uuid));
  86. return *this;
  87. }
  88. Uuid::Uuid (const uint8* const rawData) noexcept
  89. {
  90. operator= (rawData);
  91. }
  92. Uuid& Uuid::operator= (const uint8* const rawData) noexcept
  93. {
  94. if (rawData != nullptr)
  95. memcpy (uuid, rawData, sizeof (uuid));
  96. else
  97. zeromem (uuid, sizeof (uuid));
  98. return *this;
  99. }
  100. uint32 Uuid::getTimeLow() const noexcept { return ByteOrder::bigEndianInt (uuid); }
  101. uint16 Uuid::getTimeMid() const noexcept { return ByteOrder::bigEndianShort (uuid + 4); }
  102. uint16 Uuid::getTimeHighAndVersion() const noexcept { return ByteOrder::bigEndianShort (uuid + 6); }
  103. uint8 Uuid::getClockSeqAndReserved() const noexcept { return uuid[8]; }
  104. uint8 Uuid::getClockSeqLow() const noexcept { return uuid[9]; }
  105. uint64 Uuid::getNode() const noexcept { return (((uint64) ByteOrder::bigEndianShort (uuid + 10)) << 32) + ByteOrder::bigEndianInt (uuid + 12); }