Skip to content

Commit 19b2176

Browse files
committed
Add direct bool comparisons to InterlockedFlag
1 parent 16a2ffe commit 19b2176

File tree

9 files changed

+49
-2
lines changed

9 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- `AsyncServiceSetupResult.Faulted` means that `SetupAsync` threw an exception.
2121
- Two new boolean properties in `AsyncHostedService` lets subclasses decide whether `StartAsync` should fail when the service is stopped before starting (`FailOnSetupNotStarted`) or `SetupAsync` completes with `false` (`FailOnSetupUnsuccessful`).
2222
The default value is `true` for both properties.
23+
- Struct `Louis.Threading.InterlockedFlag` now implements `IEquatable<bool>`, as well as equality and inequality operators with `bool`.
2324

2425
### Changes to existing features
2526

src/Louis/PublicAPI/net462/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ Louis.Threading.AsyncServiceSetupResult.Faulted = 4 -> Louis.Threading.AsyncServ
1313
Louis.Threading.AsyncServiceSetupResult.NotStarted = 1 -> Louis.Threading.AsyncServiceSetupResult
1414
Louis.Threading.AsyncServiceSetupResult.Successful = 0 -> Louis.Threading.AsyncServiceSetupResult
1515
Louis.Threading.AsyncServiceSetupResult.Unsuccessful = 2 -> Louis.Threading.AsyncServiceSetupResult
16+
Louis.Threading.InterlockedFlag.Equals(bool other) -> bool
17+
static Louis.Threading.InterlockedFlag.operator !=(Louis.Threading.InterlockedFlag a, bool b) -> bool
18+
static Louis.Threading.InterlockedFlag.operator ==(Louis.Threading.InterlockedFlag a, bool b) -> bool
1619
virtual Louis.Threading.AsyncService.LogSetupCompleted(bool success) -> void
1720
virtual Louis.Threading.AsyncService.SetupAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<bool>

src/Louis/PublicAPI/net47/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ Louis.Threading.AsyncServiceSetupResult.Faulted = 4 -> Louis.Threading.AsyncServ
1313
Louis.Threading.AsyncServiceSetupResult.NotStarted = 1 -> Louis.Threading.AsyncServiceSetupResult
1414
Louis.Threading.AsyncServiceSetupResult.Successful = 0 -> Louis.Threading.AsyncServiceSetupResult
1515
Louis.Threading.AsyncServiceSetupResult.Unsuccessful = 2 -> Louis.Threading.AsyncServiceSetupResult
16+
Louis.Threading.InterlockedFlag.Equals(bool other) -> bool
17+
static Louis.Threading.InterlockedFlag.operator !=(Louis.Threading.InterlockedFlag a, bool b) -> bool
18+
static Louis.Threading.InterlockedFlag.operator ==(Louis.Threading.InterlockedFlag a, bool b) -> bool
1619
virtual Louis.Threading.AsyncService.LogSetupCompleted(bool success) -> void
1720
virtual Louis.Threading.AsyncService.SetupAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<bool>

src/Louis/PublicAPI/net6.0/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ Louis.Threading.AsyncServiceSetupResult.Faulted = 4 -> Louis.Threading.AsyncServ
1313
Louis.Threading.AsyncServiceSetupResult.NotStarted = 1 -> Louis.Threading.AsyncServiceSetupResult
1414
Louis.Threading.AsyncServiceSetupResult.Successful = 0 -> Louis.Threading.AsyncServiceSetupResult
1515
Louis.Threading.AsyncServiceSetupResult.Unsuccessful = 2 -> Louis.Threading.AsyncServiceSetupResult
16+
Louis.Threading.InterlockedFlag.Equals(bool other) -> bool
17+
static Louis.Threading.InterlockedFlag.operator !=(Louis.Threading.InterlockedFlag a, bool b) -> bool
18+
static Louis.Threading.InterlockedFlag.operator ==(Louis.Threading.InterlockedFlag a, bool b) -> bool
1619
virtual Louis.Threading.AsyncService.LogSetupCompleted(bool success) -> void
1720
virtual Louis.Threading.AsyncService.SetupAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<bool>

src/Louis/PublicAPI/net7.0/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Louis.Threading.AsyncServiceSetupResult.Faulted = 4 -> Louis.Threading.AsyncServ
1414
Louis.Threading.AsyncServiceSetupResult.NotStarted = 1 -> Louis.Threading.AsyncServiceSetupResult
1515
Louis.Threading.AsyncServiceSetupResult.Successful = 0 -> Louis.Threading.AsyncServiceSetupResult
1616
Louis.Threading.AsyncServiceSetupResult.Unsuccessful = 2 -> Louis.Threading.AsyncServiceSetupResult
17+
Louis.Threading.InterlockedFlag.Equals(bool other) -> bool
1718
static Louis.ComponentModel.SimpleStringConverter.AddToTypeDescriptor<T>() -> void
1819
Louis.Threading.AsyncService.StopAndWaitAsync() -> System.Threading.Tasks.Task<bool>!
20+
static Louis.Threading.InterlockedFlag.operator !=(Louis.Threading.InterlockedFlag a, bool b) -> bool
21+
static Louis.Threading.InterlockedFlag.operator ==(Louis.Threading.InterlockedFlag a, bool b) -> bool
1922
virtual Louis.Threading.AsyncService.LogSetupCompleted(bool success) -> void
2023
virtual Louis.Threading.AsyncService.SetupAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<bool>

src/Louis/PublicAPI/net8.0/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Louis.Threading.AsyncServiceSetupResult.Faulted = 4 -> Louis.Threading.AsyncServ
1414
Louis.Threading.AsyncServiceSetupResult.NotStarted = 1 -> Louis.Threading.AsyncServiceSetupResult
1515
Louis.Threading.AsyncServiceSetupResult.Successful = 0 -> Louis.Threading.AsyncServiceSetupResult
1616
Louis.Threading.AsyncServiceSetupResult.Unsuccessful = 2 -> Louis.Threading.AsyncServiceSetupResult
17+
Louis.Threading.InterlockedFlag.Equals(bool other) -> bool
1718
static Louis.ComponentModel.SimpleStringConverter.AddToTypeDescriptor<T>() -> void
1819
Louis.Threading.AsyncService.StopAndWaitAsync() -> System.Threading.Tasks.Task<bool>!
20+
static Louis.Threading.InterlockedFlag.operator !=(Louis.Threading.InterlockedFlag a, bool b) -> bool
21+
static Louis.Threading.InterlockedFlag.operator ==(Louis.Threading.InterlockedFlag a, bool b) -> bool
1922
virtual Louis.Threading.AsyncService.LogSetupCompleted(bool success) -> void
2023
virtual Louis.Threading.AsyncService.SetupAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<bool>

src/Louis/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ Louis.Threading.AsyncServiceSetupResult.Faulted = 4 -> Louis.Threading.AsyncServ
1313
Louis.Threading.AsyncServiceSetupResult.NotStarted = 1 -> Louis.Threading.AsyncServiceSetupResult
1414
Louis.Threading.AsyncServiceSetupResult.Successful = 0 -> Louis.Threading.AsyncServiceSetupResult
1515
Louis.Threading.AsyncServiceSetupResult.Unsuccessful = 2 -> Louis.Threading.AsyncServiceSetupResult
16+
Louis.Threading.InterlockedFlag.Equals(bool other) -> bool
17+
static Louis.Threading.InterlockedFlag.operator !=(Louis.Threading.InterlockedFlag a, bool b) -> bool
18+
static Louis.Threading.InterlockedFlag.operator ==(Louis.Threading.InterlockedFlag a, bool b) -> bool
1619
virtual Louis.Threading.AsyncService.LogSetupCompleted(bool success) -> void
1720
virtual Louis.Threading.AsyncService.SetupAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<bool>

src/Louis/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ Louis.Threading.AsyncServiceSetupResult.Faulted = 4 -> Louis.Threading.AsyncServ
1313
Louis.Threading.AsyncServiceSetupResult.NotStarted = 1 -> Louis.Threading.AsyncServiceSetupResult
1414
Louis.Threading.AsyncServiceSetupResult.Successful = 0 -> Louis.Threading.AsyncServiceSetupResult
1515
Louis.Threading.AsyncServiceSetupResult.Unsuccessful = 2 -> Louis.Threading.AsyncServiceSetupResult
16+
Louis.Threading.InterlockedFlag.Equals(bool other) -> bool
17+
static Louis.Threading.InterlockedFlag.operator !=(Louis.Threading.InterlockedFlag a, bool b) -> bool
18+
static Louis.Threading.InterlockedFlag.operator ==(Louis.Threading.InterlockedFlag a, bool b) -> bool
1619
virtual Louis.Threading.AsyncService.LogSetupCompleted(bool success) -> void
1720
virtual Louis.Threading.AsyncService.SetupAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<bool>

src/Louis/Threading/InterlockedFlag.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Louis.Threading;
99
/// <summary>
1010
/// Implements thread-safe management of a boolean value.
1111
/// </summary>
12-
public struct InterlockedFlag : IEquatable<InterlockedFlag>
12+
public struct InterlockedFlag : IEquatable<InterlockedFlag>, IEquatable<bool>
1313
{
1414
private int _value;
1515

@@ -53,12 +53,37 @@ public bool Value
5353
/// otherwise, <see langword="false"/>.</returns>
5454
public static bool operator !=(InterlockedFlag a, InterlockedFlag b) => a.Value != b.Value;
5555

56+
/// <summary>
57+
/// Determines whether a specified interlocked flag has the specified value.
58+
/// </summary>
59+
/// <param name="a">The interlocked flag to compare.</param>
60+
/// <param name="b">The value to compare.</param>
61+
/// <returns><see langword="true"/> if the value of <paramref name="a"/> is the same as <paramref name="b"/>;
62+
/// otherwise, <see langword="false"/>.</returns>
63+
public static bool operator ==(InterlockedFlag a, bool b) => a.Value == b;
64+
65+
/// <summary>
66+
/// Determines whether a specified interlocked flags has a value different from a specified value.
67+
/// </summary>
68+
/// <param name="a">The interlocked flag to compare.</param>
69+
/// <param name="b">The value to compare.</param>
70+
/// <returns><see langword="true"/> if the value of <paramref name="a"/> is different from <paramref name="b"/>;
71+
/// otherwise, <see langword="false"/>.</returns>
72+
public static bool operator !=(InterlockedFlag a, bool b) => a.Value != b;
73+
5674
/// <inheritdoc/>
57-
public override bool Equals(object? obj) => obj is InterlockedFlag other && Equals(other);
75+
public override bool Equals(object? obj) => obj switch {
76+
bool value => value == Value,
77+
InterlockedFlag other => other.Value == Value,
78+
_ => false,
79+
};
5880

5981
/// <inheritdoc/>
6082
public bool Equals(InterlockedFlag other) => other.Value == Value;
6183

84+
/// <inheritdoc/>
85+
public bool Equals(bool other) => other == Value;
86+
6287
/// <inheritdoc/>
6388
public override int GetHashCode() => Value.GetHashCode();
6489

0 commit comments

Comments
 (0)