-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAtom.cs
More file actions
140 lines (130 loc) · 4.53 KB
/
Atom.cs
File metadata and controls
140 lines (130 loc) · 4.53 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
using System.Runtime.InteropServices;
using System.Text;
namespace BytecodeApi.Win32.AtomTable;
/// <summary>
/// Represents an entry in the local atom table.
/// </summary>
public readonly struct Atom : IAtom, IEquatable<Atom>
{
/// <summary>
/// Represents the atom as a 16-bit integer value.
/// </summary>
public ushort Value { get; }
/// <summary>
/// Gets a <see cref="string" /> with the name of the atom.
/// </summary>
public string Name { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Atom" /> structure with the specified atom value.
/// </summary>
/// <param name="value">A <see cref="ushort" /> representing the atom value.</param>
public Atom(ushort value)
{
Value = value;
StringBuilder stringBuilder = new(256);
Native.GetAtomName(Value, stringBuilder, 256);
Name = stringBuilder.ToString();
}
/// <summary>
/// Finds an atom by its name. If not found, a new <see cref="Atom" /> value is returned with a value of 0.
/// </summary>
/// <param name="name">A <see cref="string" /> with the case insensitive name of the atom to be searched.</param>
/// <returns>
/// The <see cref="Atom" /> this method creates.
/// </returns>
public static Atom Find(string name)
{
Check.ArgumentNull(name);
return new(Native.FindAtom(name));
}
/// <summary>
/// Creates a new atom using the specified name.
/// </summary>
/// <param name="name">A <see cref="string" /> with the name for the new atom.</param>
/// <returns>
/// The <see cref="Atom" /> this method creates.
/// </returns>
public static Atom Add(string name)
{
Check.ArgumentNull(name);
return new(Native.AddAtom(name));
}
/// <summary>
/// Decrements the reference count of this atom. If the atom's reference count is reduced to zero, it is removed from the atom table.
/// </summary>
public void Delete()
{
Native.DeleteAtom(Value);
}
/// <summary>
/// Determines whether the specified <see cref="object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <see langword="true" />, if the specified <see cref="object" /> is equal to this instance;
/// otherwise, <see langword="false" />.
/// </returns>
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is Atom atom && Equals(atom);
}
/// <summary>
/// Determines whether this instance is equal to another <see cref="Atom" />.
/// </summary>
/// <param name="other">The <see cref="Atom" /> to compare to this instance.</param>
/// <returns>
/// <see langword="true" />, if this instance is equal to the <paramref name="other" /> parameter;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Equals(Atom other)
{
return Value == other.Value && Name == other.Name;
}
/// <summary>
/// Returns a hash code for this <see cref="Atom" />.
/// </summary>
/// <returns>
/// The hash code for this <see cref="Atom" /> instance.
/// </returns>
public override int GetHashCode()
{
return HashCode.Combine(Value, Name);
}
/// <summary>
/// Compares two <see cref="Atom" /> values for equality.
/// </summary>
/// <param name="a">The first <see cref="Atom" /> to compare.</param>
/// <param name="b">The second <see cref="Atom" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="Atom" /> values are equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator ==(Atom a, Atom b)
{
return Equals(a, b);
}
/// <summary>
/// Compares two <see cref="Atom" /> values for inequality.
/// </summary>
/// <param name="a">The first <see cref="Atom" /> to compare.</param>
/// <param name="b">The second <see cref="Atom" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="Atom" /> values are not equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator !=(Atom a, Atom b)
{
return !Equals(a, b);
}
}
file static class Native
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern uint GetAtomName(ushort atom, StringBuilder buffer, int size);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern ushort FindAtom(string name);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern ushort AddAtom(string name);
[DllImport("kernel32.dll", ExactSpelling = true)]
public static extern ushort DeleteAtom(ushort atom);
}