Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Null static field to handles #640

Merged
merged 2 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3885,6 +3885,12 @@ private IEnumerable<MemberDeclarationSyntax> CreateCommonTypeDefMembers(Identifi
// If this typedef struct represents a pointer, add an IsNull property.
if (fieldType is IdentifierNameSyntax { Identifier: { Value: nameof(IntPtr) or nameof(UIntPtr) } })
{
// internal static HWND Null => default;
yield return PropertyDeclaration(structName.WithTrailingTrivia(TriviaList(Space)), "Null")
.WithExpressionBody(ArrowExpressionClause(LiteralExpression(SyntaxKind.DefaultLiteralExpression)))
.AddModifiers(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword))
.WithSemicolonToken(SemicolonWithLineFeed);

// internal static bool IsNull => value == default;
yield return PropertyDeclaration(PredefinedType(TokenWithSpace(SyntaxKind.BoolKeyword)), "IsNull")
.AddModifiers(TokenWithSpace(this.Visibility))
Expand Down
19 changes: 19 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,17 @@ public void HandleStructsHaveIsNullProperty(string handleName)
this.AssertGeneratedMember(handleName, "IsNull", "internal bool IsNull => Value == default;");
}

[Theory]
[InlineData("HANDLE")]
[InlineData("HGDIOBJ")]
[InlineData("HINSTANCE")]
public void HandleStructsHaveStaticNullMember(string handleName)
{
// A null HGDIOBJ has a specific meaning beyond just the concept of an invalid handle:
// https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-selectobject#return-value
this.AssertGeneratedMember(handleName, "Null", $"internal static {handleName} Null => default;");
}

[Theory]
[InlineData("HANDLE")]
[InlineData("HGDIOBJ")]
Expand Down Expand Up @@ -1970,6 +1981,8 @@ internal readonly partial struct HWND
internal readonly IntPtr Value;
internal HWND(IntPtr value) => this.Value = value;

internal static HWND Null => default;

internal bool IsNull => Value == default;
public static implicit operator IntPtr(HWND value) => value.Value;
public static explicit operator HWND(IntPtr value) => new HWND(value);
Expand Down Expand Up @@ -2076,6 +2089,8 @@ internal readonly partial struct HDC
internal readonly IntPtr Value;
internal HDC(IntPtr value) => this.Value = value;

internal static HDC Null => default;

internal bool IsNull => Value == default;
public static implicit operator IntPtr(HDC value) => value.Value;
public static explicit operator HDC(IntPtr value) => new HDC(value);
Expand Down Expand Up @@ -2118,6 +2133,8 @@ internal readonly partial struct HWND
internal readonly IntPtr Value;
internal HWND(IntPtr value) => this.Value = value;

internal static HWND Null => default;

internal bool IsNull => Value == default;
public static implicit operator IntPtr(HWND value) => value.Value;
public static explicit operator HWND(IntPtr value) => new HWND(value);
Expand Down Expand Up @@ -2459,6 +2476,8 @@ internal readonly partial struct HANDLE
internal readonly IntPtr Value;
internal HANDLE(IntPtr value) => this.Value = value;

internal static HANDLE Null => default;

internal bool IsNull => Value == default;
public static implicit operator IntPtr(HANDLE value) => value.Value;
public static explicit operator HANDLE(IntPtr value) => new HANDLE(value);
Expand Down