SerializableGuid.cs
5.57 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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);
/// <summary>
/// Generates a hash suitable for use with containers like `HashSet` and `Dictionary`.
/// </summary>
/// <returns>A hash code generated from this object's fields.</returns>
public override int GetHashCode() => HashCode.Combine(m_GuidLow.GetHashCode(), m_GuidHigh.GetHashCode());
/// <summary>
/// Tests for equality.
/// </summary>
/// <param name="obj">The `object` to compare against.</param>
/// <returns>`True` if <paramref name="obj"/> is of type <see cref="SerializableGuid"/> and
/// <see cref="Equals(SerializableGuid)"/> also returns `true`; otherwise `false`.</returns>
public override bool Equals(object obj) => (obj is SerializableGuid) && 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);
/// <summary>
/// Tests for equality.
/// </summary>
/// <param name="other">The other <see cref="SerializableGuid"/> to compare against.</param>
/// <returns>`True` if every field in <paramref name="other"/> is equal to this <see cref="SerializableGuid"/>, otherwise false.</returns>
public bool Equals(SerializableGuid other)
{
return
(m_GuidLow == other.m_GuidLow) &&
(m_GuidHigh == other.m_GuidHigh);
}
/// <summary>
/// Tests for equality. Same as <see cref="Equals(SerializableGuid)"/>.
/// </summary>
/// <param name="lhs">The left-hand side of the comparison.</param>
/// <param name="rhs">The right-hand side of the comparison.</param>
/// <returns>`True` if <paramref name="lhs"/> is equal to <paramref name="rhs"/>, otherwise `false`.</returns>
public static bool operator ==(SerializableGuid lhs, SerializableGuid rhs) => lhs.Equals(rhs);
/// <summary>
/// Tests for inequality. Same as `!`<see cref="Equals(SerializableGuid)"/>.
/// </summary>
/// <param name="lhs">The left-hand side of the comparison.</param>
/// <param name="rhs">The right-hand side of the comparison.</param>
/// <returns>`True` if <paramref name="lhs"/> is not equal to <paramref name="rhs"/>, otherwise `false`.</returns>
public static bool operator !=(SerializableGuid lhs, SerializableGuid rhs) => !lhs.Equals(rhs);
[SerializeField]
ulong m_GuidLow;
[SerializeField]
ulong m_GuidHigh;
}
}