Skip to content

Commit 29248ab

Browse files
Use C# 14 extensions instead of extension methods
1 parent b6956e4 commit 29248ab

3 files changed

Lines changed: 275 additions & 255 deletions

File tree

SharpHook/Data/EventMaskExtensions.cs

Lines changed: 105 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -5,122 +5,120 @@ namespace SharpHook.Data;
55
/// </summary>
66
public static class EventMaskExtensions
77
{
8-
/// <summary>
9-
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftShift" /> or
10-
/// <see cref="EventMask.RightShift"/>. Otherwise, returns <see langword="false" />.
11-
/// </summary>
12-
/// <param name="mask">The event mask to check.</param>
13-
/// <returns>
14-
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftShift" /> or
15-
/// <see cref="EventMask.RightShift"/>. Otherwise, <see langword="false" />.
16-
/// </returns>
17-
public static bool HasShift(this EventMask mask) =>
18-
mask.HasAny(EventMask.Shift);
8+
extension(EventMask mask)
9+
{
10+
/// <summary>
11+
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftShift" /> or
12+
/// <see cref="EventMask.RightShift"/>. Otherwise, returns <see langword="false" />.
13+
/// </summary>
14+
/// <returns>
15+
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftShift" /> or
16+
/// <see cref="EventMask.RightShift"/>. Otherwise, <see langword="false" />.
17+
/// </returns>
18+
public bool HasShift() =>
19+
mask.HasAny(EventMask.Shift);
1920

20-
/// <summary>
21-
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftCtrl" /> or
22-
/// <see cref="EventMask.RightCtrl"/>. Otherwise, returns <see langword="false" />.
23-
/// </summary>
24-
/// <param name="mask">The event mask to check.</param>
25-
/// <returns>
26-
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftCtrl" /> or
27-
/// <see cref="EventMask.RightCtrl"/>. Otherwise, <see langword="false" />.
28-
/// </returns>
29-
public static bool HasCtrl(this EventMask mask) =>
30-
mask.HasAny(EventMask.Ctrl);
21+
/// <summary>
22+
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftCtrl" /> or
23+
/// <see cref="EventMask.RightCtrl"/>. Otherwise, returns <see langword="false" />.
24+
/// </summary>
25+
/// <returns>
26+
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftCtrl" /> or
27+
/// <see cref="EventMask.RightCtrl"/>. Otherwise, <see langword="false" />.
28+
/// </returns>
29+
public bool HasCtrl() =>
30+
mask.HasAny(EventMask.Ctrl);
3131

32-
/// <summary>
33-
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftAlt" /> or
34-
/// <see cref="EventMask.RightAlt"/>. Otherwise, returns <see langword="false" />.
35-
/// </summary>
36-
/// <param name="mask">The event mask to check.</param>
37-
/// <returns>
38-
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftAlt" /> or
39-
/// <see cref="EventMask.RightAlt"/>. Otherwise, <see langword="false" />.
40-
/// </returns>
41-
public static bool HasAlt(this EventMask mask) =>
42-
mask.HasAny(EventMask.Alt);
32+
/// <summary>
33+
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftAlt" /> or
34+
/// <see cref="EventMask.RightAlt"/>. Otherwise, returns <see langword="false" />.
35+
/// </summary>
36+
/// <returns>
37+
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftAlt" /> or
38+
/// <see cref="EventMask.RightAlt"/>. Otherwise, <see langword="false" />.
39+
/// </returns>
40+
public bool HasAlt() =>
41+
mask.HasAny(EventMask.Alt);
4342

44-
/// <summary>
45-
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftMeta" /> or
46-
/// <see cref="EventMask.RightMeta"/>. Otherwise, returns <see langword="false" />.
47-
/// </summary>
48-
/// <param name="mask">The event mask to check.</param>
49-
/// <returns>
50-
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftMeta" /> or
51-
/// <see cref="EventMask.RightMeta"/>. Otherwise, <see langword="false" />.
52-
/// </returns>
53-
public static bool HasMeta(this EventMask mask) =>
54-
mask.HasAny(EventMask.Meta);
43+
/// <summary>
44+
/// Returns <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftMeta" /> or
45+
/// <see cref="EventMask.RightMeta"/>. Otherwise, returns <see langword="false" />.
46+
/// </summary>
47+
/// <returns>
48+
/// <see langword="true" /> if the event mask contains either <see cref="EventMask.LeftMeta" /> or
49+
/// <see cref="EventMask.RightMeta"/>. Otherwise, <see langword="false" />.
50+
/// </returns>
51+
public bool HasMeta() =>
52+
mask.HasAny(EventMask.Meta);
5553

56-
/// <summary>
57-
/// Returns <see langword="true" /> if the event mask contains any of the specified flags.
58-
/// Otherwise, returns <see langword="false" />.
59-
/// </summary>
60-
/// <param name="mask">The event mask to check.</param>
61-
/// <param name="flagsToCheck">The mask flags to check against.</param>
62-
/// <returns>
63-
/// <see langword="true" /> if the event mask contains any of the specified flags.
64-
/// Otherwise, <see langword="false" />.
65-
/// </returns>
66-
/// <remarks>
67-
/// This method returns <see langword="false" /> if <paramref name="flagsToCheck" /> equals
68-
/// <see cref="EventMask.None" />.
69-
/// </remarks>
70-
public static bool HasAny(this EventMask mask, EventMask flagsToCheck) =>
71-
(mask & flagsToCheck) != EventMask.None;
54+
/// <summary>
55+
/// Returns <see langword="true" /> if the event mask contains any of the specified flags.
56+
/// Otherwise, returns <see langword="false" />.
57+
/// </summary>
58+
/// <param name="flagsToCheck">The mask flags to check against.</param>
59+
/// <returns>
60+
/// <see langword="true" /> if the event mask contains any of the specified flags.
61+
/// Otherwise, <see langword="false" />.
62+
/// </returns>
63+
/// <remarks>
64+
/// This method returns <see langword="false" /> if <paramref name="flagsToCheck" /> equals
65+
/// <see cref="EventMask.None" />.
66+
/// </remarks>
67+
public bool HasAny(EventMask flagsToCheck) =>
68+
(mask & flagsToCheck) != EventMask.None;
7269

73-
/// <summary>
74-
/// Returns <see langword="true" /> if the event mask contains all of the specified flags.
75-
/// Otherwise, returns <see langword="false" />.
76-
/// </summary>
77-
/// <param name="mask">The event mask to check.</param>
78-
/// <param name="flagsToCheck">The mask flags to check against.</param>
79-
/// <returns>
80-
/// <see langword="true" /> if the event mask contains all of the specified flags.
81-
/// Otherwise, <see langword="false" />.
82-
/// </returns>
83-
/// <remarks>
84-
/// This method returns <see langword="true" /> if <paramref name="flagsToCheck" /> equals
85-
/// <see cref="EventMask.None" />.
86-
/// </remarks>
87-
public static bool HasAll(this EventMask mask, EventMask flagsToCheck) =>
88-
(mask & flagsToCheck) == flagsToCheck;
70+
/// <summary>
71+
/// Returns <see langword="true" /> if the event mask contains all of the specified flags.
72+
/// Otherwise, returns <see langword="false" />.
73+
/// </summary>
74+
/// <param name="flagsToCheck">The mask flags to check against.</param>
75+
/// <returns>
76+
/// <see langword="true" /> if the event mask contains all of the specified flags.
77+
/// Otherwise, <see langword="false" />.
78+
/// </returns>
79+
/// <remarks>
80+
/// This method returns <see langword="true" /> if <paramref name="flagsToCheck" /> equals
81+
/// <see cref="EventMask.None" />.
82+
/// </remarks>
83+
public bool HasAll(EventMask flagsToCheck) =>
84+
(mask & flagsToCheck) == flagsToCheck;
8985

90-
/// <summary>
91-
/// Splits an event mask into an array of individual flags.
92-
/// </summary>
93-
/// <param name="mask">The event mask to split.</param>
94-
/// <returns>
95-
/// An array of individual flags contained in <paramref name="mask" />,
96-
/// or an empty array if <paramref name="mask" /> is <see cref="EventMask.None" />.
97-
/// </returns>
98-
public static EventMask[] Split(this EventMask mask) =>
86+
/// <summary>
87+
/// Splits an event mask into an array of individual flags.
88+
/// </summary>
89+
/// <returns>
90+
/// An array of individual flags contained in the mask, or an empty array if the mask is
91+
/// <see cref="EventMask.None" />.
92+
/// </returns>
93+
public EventMask[] Split() =>
9994
#if NET5_0_OR_GREATER
10095
Enum.GetValues<EventMask>()
10196
#else
102-
Enum.GetValues(typeof(EventMask))
103-
.Cast<EventMask>()
97+
Enum.GetValues(typeof(EventMask))
98+
.Cast<EventMask>()
10499
#endif
105-
.Where(m =>
106-
m != EventMask.None &&
107-
m != EventMask.Shift &&
108-
m != EventMask.Ctrl &&
109-
m != EventMask.Alt &&
110-
m != EventMask.Meta)
111-
.Where(m => mask.HasFlag(m))
112-
.ToArray();
100+
.Where(m =>
101+
m != EventMask.None &&
102+
m != EventMask.Shift &&
103+
m != EventMask.Ctrl &&
104+
m != EventMask.Alt &&
105+
m != EventMask.Meta)
106+
.Where(m => mask.HasFlag(m))
107+
.ToArray();
108+
}
113109

114-
/// <summary>
115-
/// Merges an array of event mask flags into a single event mask.
116-
/// </summary>
117-
/// <param name="flags">The event mask flags to merge.</param>
118-
/// <returns>An event mask which contains the specified mask flags.</returns>
119-
/// <exception cref="ArgumentNullException">
120-
/// <paramref name="flags" /> is <see langword="null" />.
121-
/// </exception>
122-
public static EventMask Merge(this EventMask[] flags) =>
123-
flags != null
124-
? flags.Aggregate(EventMask.None, (mask, flag) => mask | flag)
125-
: throw new ArgumentNullException(nameof(flags));
110+
extension(EventMask[] flags)
111+
{
112+
/// <summary>
113+
/// Merges an array of event mask flags into a single event mask.
114+
/// </summary>
115+
/// <returns>An event mask which contains the specified mask flags.</returns>
116+
/// <exception cref="ArgumentNullException">
117+
/// The flags array is <see langword="null" />.
118+
/// </exception>
119+
public EventMask Merge() =>
120+
flags != null
121+
? flags.Aggregate(EventMask.None, (mask, flag) => mask | flag)
122+
: throw new ArgumentNullException(nameof(flags));
123+
}
126124
}

0 commit comments

Comments
 (0)