uuid.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /// Constructs an invalid UUID.
  12. constexpr UUID() = default;
  13. /// Constructs a UUID from a reference to a 128 bit array.
  14. constexpr explicit UUID(const std::array<u8, 16>& uuid_) : uuid{uuid_} {}
  15. /**
  16. * Constructs a UUID from either:
  17. * 1. A 32 hexadecimal character string representing the bytes of the UUID
  18. * 2. A RFC 4122 formatted UUID string, in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  19. *
  20. * The input string may contain uppercase or lowercase characters, but they must:
  21. * 1. Contain valid hexadecimal characters (0-9, a-f, A-F)
  22. * 2. Not contain the "0x" hexadecimal prefix
  23. *
  24. * Should the input string not meet the above requirements,
  25. * an assert will be triggered and an invalid UUID is set instead.
  26. */
  27. explicit UUID(std::string_view uuid_string);
  28. ~UUID() = default;
  29. constexpr UUID(const UUID&) noexcept = default;
  30. constexpr UUID(UUID&&) noexcept = default;
  31. constexpr UUID& operator=(const UUID&) noexcept = default;
  32. constexpr UUID& operator=(UUID&&) noexcept = default;
  33. /**
  34. * Returns whether the stored UUID is valid or not.
  35. *
  36. * @returns True if the stored UUID is valid, false otherwise.
  37. */
  38. constexpr bool IsValid() const {
  39. return uuid != std::array<u8, 0x10>{};
  40. }
  41. /**
  42. * Returns whether the stored UUID is invalid or not.
  43. *
  44. * @returns True if the stored UUID is invalid, false otherwise.
  45. */
  46. constexpr bool IsInvalid() const {
  47. return !IsValid();
  48. }
  49. /**
  50. * Returns a 32 hexadecimal character string representing the bytes of the UUID.
  51. *
  52. * @returns A 32 hexadecimal character string of the UUID.
  53. */
  54. std::string RawString() const;
  55. /**
  56. * Returns a RFC 4122 formatted UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
  57. *
  58. * @returns A RFC 4122 formatted UUID string.
  59. */
  60. std::string FormattedString() const;
  61. /**
  62. * Returns a 64-bit hash of the UUID for use in hash table data structures.
  63. *
  64. * @returns A 64-bit hash of the UUID.
  65. */
  66. size_t Hash() const noexcept;
  67. /// DO NOT USE. Copies the contents of the UUID into a u128.
  68. u128 AsU128() const;
  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 UUID MakeDefault() {
  75. return UUID{
  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 UUID 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 UUID 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 UUID MakeRandomRFC4122V4();
  99. friend constexpr bool operator==(const UUID& lhs, const UUID& rhs) = default;
  100. };
  101. static_assert(sizeof(UUID) == 0x10, "UUID has incorrect size.");
  102. /// An invalid UUID. This UUID has all its bytes set to 0.
  103. constexpr UUID InvalidUUID = {};
  104. } // namespace Common
  105. namespace std {
  106. template <>
  107. struct hash<Common::UUID> {
  108. size_t operator()(const Common::UUID& uuid) const noexcept {
  109. return uuid.Hash();
  110. }
  111. };
  112. } // namespace std