Skip to content

Commit eb0cc23

Browse files
committed
Expose int value behind BOOL
Also adds supports for BOOL constants. Closes #186
1 parent 37417b5 commit eb0cc23

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Microsoft.Windows.CsWin32/Generator.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,8 +3048,8 @@ private StructDeclarationSyntax DeclareTypeDefBOOLStruct(TypeDefinition typeDef)
30483048
MemberAccessExpressionSyntax fieldAccessExpression = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("value"));
30493049

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

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

3064-
// public static implicit operator bool(BOOL value) => value.Value;
3064+
// BOOL(int value) => this.value = value;
3065+
members = members.Add(ConstructorDeclaration(name.Identifier)
3066+
.AddModifiers(Token(this.Visibility))
3067+
.AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
3068+
.WithExpressionBody(ArrowExpressionClause(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, fieldAccessExpression, valueParameter)))
3069+
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
3070+
3071+
// public static implicit operator bool(BOOL value) => value.value != 0 ? true : false;
30653072
members = members.Add(ConversionOperatorDeclaration(Token(SyntaxKind.ImplicitKeyword), PredefinedType(Token(SyntaxKind.BoolKeyword)))
30663073
.AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(name))
3067-
.WithExpressionBody(ArrowExpressionClause(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, valueParameter, IdentifierName(fieldName))))
3074+
.WithExpressionBody(ArrowExpressionClause(BinaryExpression(SyntaxKind.NotEqualsExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, valueParameter, IdentifierName(fieldName)), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))))
30683075
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) // operators MUST be public
30693076
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
30703077

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

3085+
// public static explicit operator BOOL(int value) => new BOOL(value);
3086+
members = members.Add(ConversionOperatorDeclaration(Token(SyntaxKind.ExplicitKeyword), name)
3087+
.AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
3088+
.WithExpressionBody(ArrowExpressionClause(ObjectCreationExpression(name).AddArgumentListArguments(Argument(valueParameter))))
3089+
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) // operators MUST be public
3090+
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
3091+
30783092
StructDeclarationSyntax result = StructDeclaration(name.Identifier)
30793093
.WithMembers(members)
30803094
.WithModifiers(TokenList(Token(this.Visibility), Token(SyntaxKind.ReadOnlyKeyword), Token(SyntaxKind.PartialKeyword)));

test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public void InterestingAPIs(
134134
"WIA_CATEGORY_FINISHED_FILE", // GUID constant
135135
"DEVPKEY_MTPBTH_IsConnected", // PROPERTYKEY constant
136136
"RT_CURSOR", // PCWSTR constant
137+
"TRUE", // BOOL constant
137138
"IOleUILinkContainerW", // An IUnknown-derived interface with no GUID
138139
"RTM_ENTITY_EXPORT_METHODS",
139140
"FILE_TYPE_NOTIFICATION_INPUT",

0 commit comments

Comments
 (0)