yuzu/src/common/uuid.h

72 lines
2.0 KiB
C
Raw Normal View History

2020-12-28 19:15:37 +04:00
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <string>
2021-08-06 01:45:14 +04:00
#include <string_view>
2020-12-28 19:15:37 +04:00
#include "common/common_types.h"
namespace Common {
constexpr u128 INVALID_UUID{{0, 0}};
2021-08-06 01:45:14 +04:00
/**
* Converts a hex string to a 128-bit unsigned integer.
*
* The hex string can be formatted in lowercase or uppercase, with or without the "0x" prefix.
*
* This function will assert and return INVALID_UUID under the following conditions:
* - If the hex string is more than 32 characters long
* - If the hex string contains non-hexadecimal characters
*
* @param hex_string Hexadecimal string
*
* @returns A 128-bit unsigned integer if successfully converted, INVALID_UUID otherwise.
*/
[[nodiscard]] u128 HexStringToU128(std::string_view hex_string);
2020-12-28 19:15:37 +04:00
struct UUID {
// UUIDs which are 0 are considered invalid!
2021-01-18 03:31:15 +04:00
u128 uuid;
UUID() = default;
2020-12-28 19:15:37 +04:00
constexpr explicit UUID(const u128& id) : uuid{id} {}
constexpr explicit UUID(const u64 lo, const u64 hi) : uuid{{lo, hi}} {}
2021-08-06 01:45:14 +04:00
explicit UUID(std::string_view hex_string) {
uuid = HexStringToU128(hex_string);
}
2020-12-28 19:15:37 +04:00
[[nodiscard]] constexpr explicit operator bool() const {
2021-07-20 22:54:52 +04:00
return uuid != INVALID_UUID;
2020-12-28 19:15:37 +04:00
}
[[nodiscard]] constexpr bool operator==(const UUID& rhs) const {
2021-07-20 22:54:52 +04:00
return uuid == rhs.uuid;
2020-12-28 19:15:37 +04:00
}
[[nodiscard]] constexpr bool operator!=(const UUID& rhs) const {
return !operator==(rhs);
}
// TODO(ogniK): Properly generate uuids based on RFC-4122
[[nodiscard]] static UUID Generate();
// Set the UUID to {0,0} to be considered an invalid user
constexpr void Invalidate() {
uuid = INVALID_UUID;
}
// TODO(ogniK): Properly generate a Nintendo ID
[[nodiscard]] constexpr u64 GetNintendoID() const {
return uuid[0];
}
[[nodiscard]] std::string Format() const;
[[nodiscard]] std::string FormatSwitch() const;
};
static_assert(sizeof(UUID) == 16, "UUID is an invalid size!");
} // namespace Common