juce_Uuid.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Uuid::Uuid()
  22. {
  23. Random r;
  24. for (size_t i = 0; i < sizeof (uuid); ++i)
  25. uuid[i] = (uint8) (r.nextInt (256));
  26. // To make it RFC 4122 compliant, need to force a few bits...
  27. uuid[6] = (uuid[6] & 0x0f) | 0x40;
  28. uuid[8] = (uuid[8] & 0x3f) | 0x80;
  29. }
  30. Uuid::~Uuid() noexcept {}
  31. Uuid::Uuid (const Uuid& other) noexcept
  32. {
  33. memcpy (uuid, other.uuid, sizeof (uuid));
  34. }
  35. Uuid& Uuid::operator= (const Uuid& other) noexcept
  36. {
  37. memcpy (uuid, other.uuid, sizeof (uuid));
  38. return *this;
  39. }
  40. bool Uuid::operator== (const Uuid& other) const noexcept { return memcmp (uuid, other.uuid, sizeof (uuid)) == 0; }
  41. bool Uuid::operator!= (const Uuid& other) const noexcept { return ! operator== (other); }
  42. Uuid Uuid::null() noexcept
  43. {
  44. return Uuid ((const uint8*) nullptr);
  45. }
  46. bool Uuid::isNull() const noexcept
  47. {
  48. for (size_t i = 0; i < sizeof (uuid); ++i)
  49. if (uuid[i] != 0)
  50. return false;
  51. return true;
  52. }
  53. String Uuid::getHexRegion (int start, int length) const
  54. {
  55. return String::toHexString (uuid + start, length, 0);
  56. }
  57. String Uuid::toString() const
  58. {
  59. return getHexRegion (0, 16);
  60. }
  61. String Uuid::toDashedString() const
  62. {
  63. return getHexRegion (0, 4)
  64. + "-" + getHexRegion (4, 2)
  65. + "-" + getHexRegion (6, 2)
  66. + "-" + getHexRegion (8, 2)
  67. + "-" + getHexRegion (10, 6);
  68. }
  69. Uuid::Uuid (const String& uuidString)
  70. {
  71. operator= (uuidString);
  72. }
  73. Uuid& Uuid::operator= (const String& uuidString)
  74. {
  75. MemoryBlock mb;
  76. mb.loadFromHexString (uuidString);
  77. mb.ensureSize (sizeof (uuid), true);
  78. mb.copyTo (uuid, 0, sizeof (uuid));
  79. return *this;
  80. }
  81. Uuid::Uuid (const uint8* const rawData) noexcept
  82. {
  83. operator= (rawData);
  84. }
  85. Uuid& Uuid::operator= (const uint8* const rawData) noexcept
  86. {
  87. if (rawData != nullptr)
  88. memcpy (uuid, rawData, sizeof (uuid));
  89. else
  90. zeromem (uuid, sizeof (uuid));
  91. return *this;
  92. }