uuid.h 3.8 KB

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