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.

120 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. Uuid::Uuid()
  24. {
  25. Random r;
  26. for (size_t i = 0; i < sizeof (uuid); ++i)
  27. uuid[i] = (uint8) (r.nextInt (256));
  28. // To make it RFC 4122 compliant, need to force a few bits...
  29. uuid[6] = (uuid[6] & 0x0f) | 0x40;
  30. uuid[8] = (uuid[8] & 0x3f) | 0x80;
  31. }
  32. Uuid::~Uuid() noexcept {}
  33. Uuid::Uuid (const Uuid& other) noexcept
  34. {
  35. memcpy (uuid, other.uuid, sizeof (uuid));
  36. }
  37. Uuid& Uuid::operator= (const Uuid& other) noexcept
  38. {
  39. memcpy (uuid, other.uuid, sizeof (uuid));
  40. return *this;
  41. }
  42. bool Uuid::operator== (const Uuid& other) const noexcept { return memcmp (uuid, other.uuid, sizeof (uuid)) == 0; }
  43. bool Uuid::operator!= (const Uuid& other) const noexcept { return ! operator== (other); }
  44. Uuid Uuid::null() noexcept
  45. {
  46. return Uuid ((const uint8*) nullptr);
  47. }
  48. bool Uuid::isNull() const noexcept
  49. {
  50. for (size_t i = 0; i < sizeof (uuid); ++i)
  51. if (uuid[i] != 0)
  52. return false;
  53. return true;
  54. }
  55. String Uuid::getHexRegion (int start, int length) const
  56. {
  57. return String::toHexString (uuid + start, length, 0);
  58. }
  59. String Uuid::toString() const
  60. {
  61. return getHexRegion (0, 16);
  62. }
  63. String Uuid::toDashedString() const
  64. {
  65. return getHexRegion (0, 4)
  66. + "-" + getHexRegion (4, 2)
  67. + "-" + getHexRegion (6, 2)
  68. + "-" + getHexRegion (8, 2)
  69. + "-" + getHexRegion (10, 6);
  70. }
  71. Uuid::Uuid (const String& uuidString)
  72. {
  73. operator= (uuidString);
  74. }
  75. Uuid& Uuid::operator= (const String& uuidString)
  76. {
  77. MemoryBlock mb;
  78. mb.loadFromHexString (uuidString);
  79. mb.ensureSize (sizeof (uuid), true);
  80. mb.copyTo (uuid, 0, sizeof (uuid));
  81. return *this;
  82. }
  83. Uuid::Uuid (const uint8* const rawData) noexcept
  84. {
  85. operator= (rawData);
  86. }
  87. Uuid& Uuid::operator= (const uint8* const rawData) noexcept
  88. {
  89. if (rawData != nullptr)
  90. memcpy (uuid, rawData, sizeof (uuid));
  91. else
  92. zeromem (uuid, sizeof (uuid));
  93. return *this;
  94. }