diff options
| author | 2022-02-05 12:35:39 -0500 | |
|---|---|---|
| committer | 2022-02-05 13:56:21 -0500 | |
| commit | 25db62ce1534cbd8b93b4284869229e4bd7de54d (patch) | |
| tree | dd74d3fc6ba14a0de5e88778cad5b5c65fcba248 /src/common/uuid.h | |
| parent | profile: Migrate to the new UUID implementation (diff) | |
| download | yuzu-25db62ce1534cbd8b93b4284869229e4bd7de54d.tar.gz yuzu-25db62ce1534cbd8b93b4284869229e4bd7de54d.tar.xz yuzu-25db62ce1534cbd8b93b4284869229e4bd7de54d.zip | |
general: Rename NewUUID to UUID, and remove the previous UUID impl
This completes the removal of the old UUID implementation.
Diffstat (limited to 'src/common/uuid.h')
| -rw-r--r-- | src/common/uuid.h | 166 |
1 files changed, 109 insertions, 57 deletions
diff --git a/src/common/uuid.h b/src/common/uuid.h index 8ea01f8da..fe31e64e6 100644 --- a/src/common/uuid.h +++ b/src/common/uuid.h | |||
| @@ -1,9 +1,11 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | 1 | // Copyright 2022 yuzu Emulator Project |
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | ||
| 8 | #include <functional> | ||
| 7 | #include <string> | 9 | #include <string> |
| 8 | #include <string_view> | 10 | #include <string_view> |
| 9 | 11 | ||
| @@ -11,69 +13,119 @@ | |||
| 11 | 13 | ||
| 12 | namespace Common { | 14 | namespace Common { |
| 13 | 15 | ||
| 14 | constexpr u128 INVALID_UUID{{0, 0}}; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Converts a hex string to a 128-bit unsigned integer. | ||
| 18 | * | ||
| 19 | * The hex string can be formatted in lowercase or uppercase, with or without the "0x" prefix. | ||
| 20 | * | ||
| 21 | * This function will assert and return INVALID_UUID under the following conditions: | ||
| 22 | * - If the hex string is more than 32 characters long | ||
| 23 | * - If the hex string contains non-hexadecimal characters | ||
| 24 | * | ||
| 25 | * @param hex_string Hexadecimal string | ||
| 26 | * | ||
| 27 | * @returns A 128-bit unsigned integer if successfully converted, INVALID_UUID otherwise. | ||
| 28 | */ | ||
| 29 | [[nodiscard]] u128 HexStringToU128(std::string_view hex_string); | ||
| 30 | |||
| 31 | struct UUID { | 16 | struct UUID { |
| 32 | // UUIDs which are 0 are considered invalid! | 17 | std::array<u8, 0x10> uuid{}; |
| 33 | u128 uuid; | 18 | |
| 34 | UUID() = default; | 19 | /// Constructs an invalid UUID. |
| 35 | constexpr explicit UUID(const u128& id) : uuid{id} {} | 20 | constexpr UUID() = default; |
| 36 | constexpr explicit UUID(const u64 lo, const u64 hi) : uuid{{lo, hi}} {} | 21 | |
| 37 | explicit UUID(std::string_view hex_string) { | 22 | /// Constructs a UUID from a reference to a 128 bit array. |
| 38 | uuid = HexStringToU128(hex_string); | 23 | constexpr explicit UUID(const std::array<u8, 16>& uuid_) : uuid{uuid_} {} |
| 39 | } | 24 | |
| 40 | 25 | /** | |
| 41 | [[nodiscard]] constexpr explicit operator bool() const { | 26 | * Constructs a UUID from either: |
| 42 | return uuid != INVALID_UUID; | 27 | * 1. A 32 hexadecimal character string representing the bytes of the UUID |
| 43 | } | 28 | * 2. A RFC 4122 formatted UUID string, in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 44 | 29 | * | |
| 45 | [[nodiscard]] constexpr bool operator==(const UUID& rhs) const { | 30 | * The input string may contain uppercase or lowercase characters, but they must: |
| 46 | return uuid == rhs.uuid; | 31 | * 1. Contain valid hexadecimal characters (0-9, a-f, A-F) |
| 47 | } | 32 | * 2. Not contain the "0x" hexadecimal prefix |
| 48 | 33 | * | |
| 49 | [[nodiscard]] constexpr bool operator!=(const UUID& rhs) const { | 34 | * Should the input string not meet the above requirements, |
| 50 | return !operator==(rhs); | 35 | * an assert will be triggered and an invalid UUID is set instead. |
| 51 | } | 36 | */ |
| 52 | 37 | explicit UUID(std::string_view uuid_string); | |
| 53 | // TODO(ogniK): Properly generate uuids based on RFC-4122 | 38 | |
| 54 | [[nodiscard]] static UUID Generate(); | 39 | ~UUID() = default; |
| 55 | 40 | ||
| 56 | // Set the UUID to {0,0} to be considered an invalid user | 41 | constexpr UUID(const UUID&) noexcept = default; |
| 57 | constexpr void Invalidate() { | 42 | constexpr UUID(UUID&&) noexcept = default; |
| 58 | uuid = INVALID_UUID; | 43 | |
| 44 | constexpr UUID& operator=(const UUID&) noexcept = default; | ||
| 45 | constexpr UUID& operator=(UUID&&) noexcept = default; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Returns whether the stored UUID is valid or not. | ||
| 49 | * | ||
| 50 | * @returns True if the stored UUID is valid, false otherwise. | ||
| 51 | */ | ||
| 52 | constexpr bool IsValid() const { | ||
| 53 | return uuid != std::array<u8, 0x10>{}; | ||
| 59 | } | 54 | } |
| 60 | 55 | ||
| 61 | [[nodiscard]] constexpr bool IsInvalid() const { | 56 | /** |
| 62 | return uuid == INVALID_UUID; | 57 | * Returns whether the stored UUID is invalid or not. |
| 63 | } | 58 | * |
| 64 | [[nodiscard]] constexpr bool IsValid() const { | 59 | * @returns True if the stored UUID is invalid, false otherwise. |
| 65 | return !IsInvalid(); | 60 | */ |
| 61 | constexpr bool IsInvalid() const { | ||
| 62 | return !IsValid(); | ||
| 66 | } | 63 | } |
| 67 | 64 | ||
| 68 | // TODO(ogniK): Properly generate a Nintendo ID | 65 | /** |
| 69 | [[nodiscard]] constexpr u64 GetNintendoID() const { | 66 | * Returns a 32 hexadecimal character string representing the bytes of the UUID. |
| 70 | return uuid[0]; | 67 | * |
| 68 | * @returns A 32 hexadecimal character string of the UUID. | ||
| 69 | */ | ||
| 70 | std::string RawString() const; | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Returns a RFC 4122 formatted UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. | ||
| 74 | * | ||
| 75 | * @returns A RFC 4122 formatted UUID string. | ||
| 76 | */ | ||
| 77 | std::string FormattedString() const; | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Returns a 64-bit hash of the UUID for use in hash table data structures. | ||
| 81 | * | ||
| 82 | * @returns A 64-bit hash of the UUID. | ||
| 83 | */ | ||
| 84 | size_t Hash() const noexcept; | ||
| 85 | |||
| 86 | /// DO NOT USE. Copies the contents of the UUID into a u128. | ||
| 87 | u128 AsU128() const; | ||
| 88 | |||
| 89 | /** | ||
| 90 | * Creates a default UUID "yuzu Default UID". | ||
| 91 | * | ||
| 92 | * @returns A UUID with its bytes set to the ASCII values of "yuzu Default UID". | ||
| 93 | */ | ||
| 94 | static constexpr UUID MakeDefault() { | ||
| 95 | return UUID{ | ||
| 96 | {'y', 'u', 'z', 'u', ' ', 'D', 'e', 'f', 'a', 'u', 'l', 't', ' ', 'U', 'I', 'D'}, | ||
| 97 | }; | ||
| 71 | } | 98 | } |
| 72 | 99 | ||
| 73 | [[nodiscard]] std::string Format() const; | 100 | /** |
| 74 | [[nodiscard]] std::string FormatSwitch() const; | 101 | * Creates a random UUID. |
| 102 | * | ||
| 103 | * @returns A random UUID. | ||
| 104 | */ | ||
| 105 | static UUID MakeRandom(); | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Creates a random UUID with a seed. | ||
| 109 | * | ||
| 110 | * @param seed A seed to initialize the Mersenne-Twister RNG | ||
| 111 | * | ||
| 112 | * @returns A random UUID. | ||
| 113 | */ | ||
| 114 | static UUID MakeRandomWithSeed(u32 seed); | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Creates a random UUID. The generated UUID is RFC 4122 Version 4 compliant. | ||
| 118 | * | ||
| 119 | * @returns A random UUID that is RFC 4122 Version 4 compliant. | ||
| 120 | */ | ||
| 121 | static UUID MakeRandomRFC4122V4(); | ||
| 122 | |||
| 123 | friend constexpr bool operator==(const UUID& lhs, const UUID& rhs) = default; | ||
| 75 | }; | 124 | }; |
| 76 | static_assert(sizeof(UUID) == 16, "UUID is an invalid size!"); | 125 | static_assert(sizeof(UUID) == 0x10, "UUID has incorrect size."); |
| 126 | |||
| 127 | /// An invalid UUID. This UUID has all its bytes set to 0. | ||
| 128 | constexpr UUID InvalidUUID = {}; | ||
| 77 | 129 | ||
| 78 | } // namespace Common | 130 | } // namespace Common |
| 79 | 131 | ||
| @@ -82,7 +134,7 @@ namespace std { | |||
| 82 | template <> | 134 | template <> |
| 83 | struct hash<Common::UUID> { | 135 | struct hash<Common::UUID> { |
| 84 | size_t operator()(const Common::UUID& uuid) const noexcept { | 136 | size_t operator()(const Common::UUID& uuid) const noexcept { |
| 85 | return uuid.uuid[1] ^ uuid.uuid[0]; | 137 | return uuid.Hash(); |
| 86 | } | 138 | } |
| 87 | }; | 139 | }; |
| 88 | 140 | ||