Skip to content

Commit

Permalink
Merge pull request #640 from elachlan/NullFieldOnHandles
Browse files Browse the repository at this point in the history
Add Null static field to handles
  • Loading branch information
AArnott committed Aug 9, 2022
2 parents 2b98859 + bbaa6e1 commit 2dc756c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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 @@ namespace Foundation
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 @@ namespace Graphics.Gdi
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 @@ namespace Foundation
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 @@ namespace Foundation
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

0 comments on commit 2dc756c

Please sign in to comment.