new_uuid.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 NewUUID {
  12. std::array<u8, 0x10> uuid{};
  13. /// Constructs an invalid UUID.
  14. constexpr NewUUID() = default;
  15. /// Constructs a UUID from a reference to a 128 bit array.
  16. constexpr explicit NewUUID(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 NewUUID(std::string_view uuid_string);
  30. ~NewUUID() = default;
  31. constexpr NewUUID(const NewUUID&) noexcept = default;
  32. constexpr NewUUID(NewUUID&&) noexcept = default;
  33. constexpr NewUUID& operator=(const NewUUID&) noexcept = default;
  34. constexpr NewUUID& operator=(NewUUID&&) 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. /**
  70. * Creates a default UUID "yuzu Default UID".
  71. *
  72. * @returns A UUID with its bytes set to the ASCII values of "yuzu Default UID".
  73. */
  74. static constexpr NewUUID MakeDefault() {
  75. return NewUUID{
  76. {'y', 'u', 'z', 'u', ' ', 'D', 'e', 'f', 'a', 'u', 'l', 't', ' ', 'U', 'I', 'D'},
  77. };
  78. }
  79. /**
  80. * Creates a random UUID.
  81. *
  82. * @returns A random UUID.
  83. */
  84. static NewUUID MakeRandom();
  85. /**
  86. * Creates a random UUID with a seed.
  87. *
  88. * @param seed A seed to initialize the Mersenne-Twister RNG
  89. *
  90. * @returns A random UUID.
  91. */
  92. static NewUUID MakeRandomWithSeed(u32 seed);
  93. /**
  94. * Creates a random UUID. The generated UUID is RFC 4122 Version 4 compliant.
  95. *
  96. * @returns A random UUID that is RFC 4122 Version 4 compliant.
  97. */
  98. static NewUUID MakeRandomRFC4122V4();
  99. friend constexpr bool operator==(const NewUUID& lhs, const NewUUID& rhs) = default;
  100. };
  101. static_assert(sizeof(NewUUID) == 0x10, "UUID has incorrect size.");
  102. /// An invalid UUID. This UUID has all its bytes set to 0.
  103. constexpr NewUUID InvalidUUID = {};
  104. } // namespace Common
  105. namespace std {
  106. template <>
  107. struct hash<Common::NewUUID> {
  108. size_t operator()(const Common::NewUUID& uuid) const noexcept {
  109. return uuid.Hash();
  110. }
  111. };
  112. } // namespace std