Skip to content

Commit

Permalink
Expose int value behind BOOL
Browse files Browse the repository at this point in the history
Also adds supports for BOOL constants.

Closes #186
  • Loading branch information
AArnott committed Mar 16, 2021
1 parent 545a9e3 commit 8f3aa91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3048,8 +3048,8 @@ private StructDeclarationSyntax DeclareTypeDefBOOLStruct(TypeDefinition typeDef)
MemberAccessExpressionSyntax fieldAccessExpression = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("value"));

// Add property accessor
members = members.Add(PropertyDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), "Value")
.WithExpressionBody(ArrowExpressionClause(BinaryExpression(SyntaxKind.NotEqualsExpression, fieldAccessExpression, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))))).WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
members = members.Add(PropertyDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), "Value")
.WithExpressionBody(ArrowExpressionClause(fieldAccessExpression)).WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
.AddModifiers(Token(this.Visibility)));

// BOOL(bool value) => this.value = value ? 1 : 0;
Expand All @@ -3061,10 +3061,17 @@ private StructDeclarationSyntax DeclareTypeDefBOOLStruct(TypeDefinition typeDef)
.WithExpressionBody(ArrowExpressionClause(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, fieldAccessExpression, boolToInt)))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));

// public static implicit operator bool(BOOL value) => value.Value;
// BOOL(int value) => this.value = value;
members = members.Add(ConstructorDeclaration(name.Identifier)
.AddModifiers(Token(this.Visibility))
.AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
.WithExpressionBody(ArrowExpressionClause(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, fieldAccessExpression, valueParameter)))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));

// public static implicit operator bool(BOOL value) => value.value != 0 ? true : false;
members = members.Add(ConversionOperatorDeclaration(Token(SyntaxKind.ImplicitKeyword), PredefinedType(Token(SyntaxKind.BoolKeyword)))
.AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(name))
.WithExpressionBody(ArrowExpressionClause(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, valueParameter, IdentifierName(fieldName))))
.WithExpressionBody(ArrowExpressionClause(BinaryExpression(SyntaxKind.NotEqualsExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, valueParameter, IdentifierName(fieldName)), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))))
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) // operators MUST be public
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));

Expand All @@ -3075,6 +3082,13 @@ private StructDeclarationSyntax DeclareTypeDefBOOLStruct(TypeDefinition typeDef)
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) // operators MUST be public
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));

// public static explicit operator BOOL(int value) => new BOOL(value);
members = members.Add(ConversionOperatorDeclaration(Token(SyntaxKind.ExplicitKeyword), name)
.AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
.WithExpressionBody(ArrowExpressionClause(ObjectCreationExpression(name).AddArgumentListArguments(Argument(valueParameter))))
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) // operators MUST be public
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));

StructDeclarationSyntax result = StructDeclaration(name.Identifier)
.WithMembers(members)
.WithModifiers(TokenList(Token(this.Visibility), Token(SyntaxKind.ReadOnlyKeyword), Token(SyntaxKind.PartialKeyword)));
Expand Down
1 change: 1 addition & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public void SimplestMethod()
"WIA_CATEGORY_FINISHED_FILE", // GUID constant
"DEVPKEY_MTPBTH_IsConnected", // PROPERTYKEY constant
"RT_CURSOR", // PCWSTR constant
"TRUE", // BOOL constant
"IOleUILinkContainerW", // An IUnknown-derived interface with no GUID
"RTM_ENTITY_EXPORT_METHODS",
"FILE_TYPE_NOTIFICATION_INPUT",
Expand Down

0 comments on commit 8f3aa91

Please sign in to comment.