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
2.9KB

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