forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddedObjectKey.cs
More file actions
161 lines (124 loc) · 3.67 KB
/
EmbeddedObjectKey.cs
File metadata and controls
161 lines (124 loc) · 3.67 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections;
using System.Collections.Generic;
using JavaScriptEngineSwitcher.ChakraCore.Resources;
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt.Embedding
{
/// <summary>
/// Key for storage of embedded objects
/// </summary>
internal struct EmbeddedObjectKey : IEquatable<EmbeddedObjectKey>, IStructuralEquatable,
IComparable, IComparable<EmbeddedObjectKey>, IStructuralComparable
{
/// <summary>
/// Name of host type
/// </summary>
public readonly string HostTypeName;
/// <summary>
/// Instance of host type
/// </summary>
public readonly object HostObject;
/// <summary>
/// Constructs an instance of the key for storage of embedded objects
/// </summary>
/// <param name="hostObject">Instance of host type</param>
public EmbeddedObjectKey(object hostObject)
{
HostTypeName = hostObject.GetType().AssemblyQualifiedName;
HostObject = hostObject;
}
private static int CombineHashCodes(int h1, int h2)
{
return ((h1 << 5) + h1) ^ h2;
}
#region IEquatable<EmbeddedObjectKey> implementation
public bool Equals(EmbeddedObjectKey other)
{
return EqualityComparer<string>.Default.Equals(HostTypeName, other.HostTypeName)
&& EqualityComparer<object>.Default.Equals(HostObject, other.HostObject);
}
#endregion
#region IStructuralEquatable implementation
bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer)
{
if (other == null || !(other is EmbeddedObjectKey))
{
return false;
}
var embeddedObjectKey = (EmbeddedObjectKey)other;
return comparer.Equals(HostTypeName, embeddedObjectKey.HostTypeName)
&& comparer.Equals(HostObject, embeddedObjectKey.HostObject);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return CombineHashCodes(comparer.GetHashCode(HostTypeName), comparer.GetHashCode(HostObject));
}
#endregion
#region IComparable implementation
int IComparable.CompareTo(object other)
{
if (other == null)
{
return 1;
}
if (!(other is EmbeddedObjectKey))
{
throw new ArgumentException(
string.Format(Strings.Common_ArgumentHasIncorrectType, nameof(other), other.GetType().Name),
nameof(other)
);
}
return CompareTo((EmbeddedObjectKey)other);
}
#endregion
#region IComparable<EmbeddedObjectKey> implementation
public int CompareTo(EmbeddedObjectKey other)
{
int c = Comparer<string>.Default.Compare(HostTypeName, other.HostTypeName);
if (c != 0)
{
return c;
}
return Comparer<object>.Default.Compare(HostObject, other.HostObject);
}
#endregion
#region IStructuralComparable implementation
int IStructuralComparable.CompareTo(object other, IComparer comparer)
{
if (other == null)
{
return 1;
}
if (!(other is EmbeddedObjectKey))
{
throw new ArgumentException(
string.Format(Strings.Common_ArgumentHasIncorrectType, nameof(other), other.GetType().Name),
nameof(other)
);
}
var embeddedObjectKey = (EmbeddedObjectKey)other;
int c = comparer.Compare(HostTypeName, embeddedObjectKey.HostTypeName);
if (c != 0)
{
return c;
}
return comparer.Compare(HostObject, embeddedObjectKey.HostObject);
}
#endregion
#region Object overrides
public override bool Equals(object obj)
{
return obj is EmbeddedObjectKey && Equals((EmbeddedObjectKey)obj);
}
public override int GetHashCode()
{
return CombineHashCodes(EqualityComparer<string>.Default.GetHashCode(HostTypeName),
EqualityComparer<object>.Default.GetHashCode(HostObject));
}
public override string ToString()
{
return "(" + HostTypeName?.ToString() + ", " + HostObject?.ToString() + ")";
}
#endregion
}
}