SerializableGuid.cs
4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
namespace UnityEngine.XR.ARSubsystems
{
/// <summary>
/// A <c>Guid</c> that can be serialized by Unity. The 128-bit <c>Guid</c>
/// is stored as two 64-bit <c>ulong</c>s. See also the creation utility at
/// <c>UnityEditor.XR.ARSubsystems.SerializableGuidUtil</c>.
/// </summary>
[Serializable]
public struct SerializableGuid : IEquatable<SerializableGuid>
{
/// <summary>
/// Constructs a <see cref="SerializableGuid"/> from two 64-bit <c>ulong</c>s.
/// </summary>
/// <param name="guidLow">The low 8 bytes of the <c>Guid</c>.</param>
/// <param name="guidHigh">The high 8 bytes of the <c>Guid</c>.</param>
public SerializableGuid(ulong guidLow, ulong guidHigh)
{
m_GuidLow = guidLow;
m_GuidHigh = guidHigh;
}
static readonly SerializableGuid k_Empty = new SerializableGuid(0, 0);
/// <summary>
/// Used to represent <c>System.Guid.Empty</c>, e.g., a GUID whose value is all zeros.
/// </summary>
public static SerializableGuid empty => k_Empty;
/// <summary>
/// Reconstructs the <c>Guid</c> from the serialized data.
/// </summary>
public Guid guid => GuidUtil.Compose(m_GuidLow, m_GuidHigh);
public override int GetHashCode()
{
unchecked
{
var hash = m_GuidLow.GetHashCode();
return hash * 486187739 + m_GuidHigh.GetHashCode();
}
}
public override bool Equals(object obj)
{
if (!(obj is SerializableGuid))
return false;
return Equals((SerializableGuid)obj);
}
/// <summary>
/// Generates a string representation of the <c>Guid</c>. Same as <see cref="guid"/><c>.ToString()</c>.
/// See <a href="https://docs.microsoft.com/en-us/dotnet/api/system.guid.tostring?view=netframework-4.7.2#System_Guid_ToString">Microsoft's documentation</a>
/// for more details.
/// </summary>
/// <returns>A string representation of the <c>Guid</c>.</returns>
public override string ToString() => guid.ToString();
/// <summary>
/// Generates a string representation of the <c>Guid</c>. Same as <see cref="guid"/><c>.ToString(format)</c>.
/// </summary>
/// <param name="format">A single format specifier that indicates how to format the value of the <c>Guid</c>.
/// See <a href="https://docs.microsoft.com/en-us/dotnet/api/system.guid.tostring?view=netframework-4.7.2#System_Guid_ToString_System_String_">Microsoft's documentation</a>
/// for more details.</param>
/// <returns>A string representation of the <c>Guid</c>.</returns>
public string ToString(string format) => guid.ToString(format);
/// <summary>
/// Generates a string representation of the <c>Guid</c>. Same as <see cref="guid"/><c>.ToString(format, provider)</c>.
/// </summary>
/// <param name="format">A single format specifier that indicates how to format the value of the <c>Guid</c>.
/// See <a href="https://docs.microsoft.com/en-us/dotnet/api/system.guid.tostring?view=netframework-4.7.2#System_Guid_ToString_System_String_System_IFormatProvider_">Microsoft's documentation</a>
/// for more details.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>A string representation of the <c>Guid</c>.</returns>
public string ToString(string format, IFormatProvider provider) => guid.ToString(format, provider);
public bool Equals(SerializableGuid other)
{
return
(m_GuidLow == other.m_GuidLow) &&
(m_GuidHigh == other.m_GuidHigh);
}
public static bool operator ==(SerializableGuid lhs, SerializableGuid rhs) => lhs.Equals(rhs);
public static bool operator !=(SerializableGuid lhs, SerializableGuid rhs) => !lhs.Equals(rhs);
[SerializeField]
ulong m_GuidLow;
[SerializeField]
ulong m_GuidHigh;
}
}