uuid.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <functional>
  6. #include <string>
  7. #include "common/common_types.h"
  8. namespace Common {
  9. struct UUID {
  10. std::array<u8, 0x10> uuid;
  11. constexpr UUID() = default;
  12. /// Constructs a UUID from a reference to a 128 bit array.
  13. constexpr explicit UUID(const std::array<u8, 16>& uuid_) : uuid{uuid_} {}
  14. /**
  15. * Constructs a UUID from either:
  16. * 1. A 32 hexadecimal character string representing the bytes of the UUID
  17. * 2. A RFC 4122 formatted UUID string, in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  18. *
  19. * The input string may contain uppercase or lowercase characters, but they must:
  20. * 1. Contain valid hexadecimal characters (0-9, a-f, A-F)
  21. * 2. Not contain the "0x" hexadecimal prefix
  22. *
  23. * Should the input string not meet the above requirements,
  24. * an assert will be triggered and an invalid UUID is set instead.
  25. */
  26. explicit UUID(std::string_view uuid_string);
  27. /**
  28. * Returns whether the stored UUID is valid or not.
  29. *
  30. * @returns True if the stored UUID is valid, false otherwise.
  31. */
  32. constexpr bool IsValid() const {
  33. return uuid != std::array<u8, 0x10>{};
  34. }
  35. /**
  36. * Returns whether the stored UUID is invalid or not.
  37. *
  38. * @returns True if the stored UUID is invalid, false otherwise.
  39. */
  40. constexpr bool IsInvalid() const {
  41. return !IsValid();
  42. }
  43. /**
  44. * Returns a 32 hexadecimal character string representing the bytes of the UUID.
  45. *
  46. * @returns A 32 hexadecimal character string of the UUID.
  47. */
  48. std::string RawString() const;
  49. /**
  50. * Returns a RFC 4122 formatted UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
  51. *
  52. * @returns A RFC 4122 formatted UUID string.
  53. */
  54. std::string FormattedString() const;
  55. /**
  56. * Returns a 64-bit hash of the UUID for use in hash table data structures.
  57. *
  58. * @returns A 64-bit hash of the UUID.
  59. */
  60. size_t Hash() const noexcept;
  61. /// DO NOT USE. Copies the contents of the UUID into a u128.
  62. u128 AsU128() const;
  63. /**
  64. * Creates a default UUID "yuzu Default UID".
  65. *
  66. * @returns A UUID with its bytes set to the ASCII values of "yuzu Default UID".
  67. */
  68. static constexpr UUID MakeDefault() {
  69. return UUID{
  70. {'y', 'u', 'z', 'u', ' ', 'D', 'e', 'f', 'a', 'u', 'l', 't', ' ', 'U', 'I', 'D'},
  71. };
  72. }
  73. /**
  74. * Creates a random UUID.
  75. *
  76. * @returns A random UUID.
  77. */
  78. static UUID MakeRandom();
  79. /**
  80. * Creates a random UUID with a seed.
  81. *
  82. * @param seed A seed to initialize the Mersenne-Twister RNG
  83. *
  84. * @returns A random UUID.
  85. */
  86. static UUID MakeRandomWithSeed(u32 seed);
  87. /**
  88. * Creates a random UUID. The generated UUID is RFC 4122 Version 4 compliant.
  89. *
  90. * @returns A random UUID that is RFC 4122 Version 4 compliant.
  91. */
  92. static UUID MakeRandomRFC4122V4();
  93. friend constexpr bool operator==(const UUID& lhs, const UUID& rhs) = default;
  94. };
  95. static_assert(sizeof(UUID) == 0x10, "UUID has incorrect size.");
  96. static_assert(std::is_trivial_v<UUID>);
  97. /// An invalid UUID. This UUID has all its bytes set to 0.
  98. constexpr UUID InvalidUUID = {};
  99. } // namespace Common
  100. namespace std {
  101. template <>
  102. struct hash<Common::UUID> {
  103. size_t operator()(const Common::UUID& uuid) const noexcept {
  104. return uuid.Hash();
  105. }
  106. };
  107. } // namespace std