Skip to content

Commit

Permalink
Enable use of APIs using PCSTR/PCWSTR on net35
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Oct 9, 2021
1 parent c9df035 commit 3236f79
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
38 changes: 38 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,10 +1611,48 @@ internal void GetBaseTypeInfo(TypeDefinition typeDef, out StringHandle baseTypeN
{
case "PCWSTR":
specialDeclaration = this.FetchTemplate($"{specialName}");

if (this.canUseSpan)
{
TypeSyntax readOnlySpanOfChar = MakeReadOnlySpanOfT(PredefinedType(Token(SyntaxKind.CharKeyword)));
ExpressionSyntax thisValue = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Value"));
ExpressionSyntax thisValueIsNull = IsPatternExpression(thisValue, ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)));
ExpressionSyntax thisLength = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Length"));

// internal ReadOnlySpan<char> AsSpan() => this.Value is null ? default(ReadOnlySpan<char>) : new ReadOnlySpan<char>(this.Value, this.Length);
specialDeclaration = ((TypeDeclarationSyntax)specialDeclaration).AddMembers(
MethodDeclaration(readOnlySpanOfChar, Identifier("AsSpan"))
.AddModifiers(TokenWithSpace(this.Visibility))
.WithExpressionBody(ArrowExpressionClause(ConditionalExpression(
condition: thisValueIsNull,
whenTrue: DefaultExpression(readOnlySpanOfChar),
whenFalse: ObjectCreationExpression(readOnlySpanOfChar).AddArgumentListArguments(Argument(thisValue), Argument(thisLength)))))
.WithSemicolonToken(SemicolonWithLineFeed));
}

this.TryGenerateType("PWSTR"); // the template references this type
break;
case "PCSTR":
specialDeclaration = this.FetchTemplate($"{specialName}");

if (this.canUseSpan)
{
TypeSyntax readOnlySpanOfByte = MakeReadOnlySpanOfT(PredefinedType(Token(SyntaxKind.ByteKeyword)));
ExpressionSyntax thisValue = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Value"));
ExpressionSyntax thisValueIsNull = IsPatternExpression(thisValue, ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)));
ExpressionSyntax thisLength = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Length"));

// internal ReadOnlySpan<byte> AsSpan() => this.Value is null ? default(ReadOnlySpan<byte>) : new ReadOnlySpan<byte>(this.Value, this.Length);
specialDeclaration = ((TypeDeclarationSyntax)specialDeclaration).AddMembers(
MethodDeclaration(readOnlySpanOfByte, Identifier("AsSpan"))
.AddModifiers(TokenWithSpace(this.Visibility))
.WithExpressionBody(ArrowExpressionClause(ConditionalExpression(
condition: thisValueIsNull,
whenTrue: DefaultExpression(readOnlySpanOfByte),
whenFalse: ObjectCreationExpression(readOnlySpanOfByte).AddArgumentListArguments(Argument(thisValue), Argument(thisLength)))))
.WithSemicolonToken(SemicolonWithLineFeed));
}

this.TryGenerateType("PSTR"); // the template references this type
break;
default:
Expand Down
5 changes: 0 additions & 5 deletions src/Microsoft.Windows.CsWin32/templates/PCSTR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,5 @@ internal int Length
/// <returns>A <see langword="string"/>, or <see langword="null"/> if <see cref="Value"/> is <see langword="null"/>.</returns>
public override string ToString() => this.Value is null ? null : new string((sbyte*)this.Value, 0, this.Length, global::System.Text.Encoding.UTF8);

/// <summary>
/// Returns a span of the characters in this string.
/// </summary>
internal ReadOnlySpan<byte> AsSpan() => this.Value is null ? default(ReadOnlySpan<byte>) : new ReadOnlySpan<byte>(this.Value, this.Length);

private string DebuggerDisplay => this.ToString();
}
5 changes: 0 additions & 5 deletions src/Microsoft.Windows.CsWin32/templates/PCWSTR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,5 @@ internal int Length
/// <returns>A <see langword="string"/>, or <see langword="null"/> if <see cref="Value"/> is <see langword="null"/>.</returns>
public override string ToString() => this.Value is null ? null : new string(this.Value);

/// <summary>
/// Returns a span of the characters in this string.
/// </summary>
internal ReadOnlySpan<char> AsSpan() => this.Value is null ? default(ReadOnlySpan<char>) : new ReadOnlySpan<char>(this.Value, this.Length);

private string DebuggerDisplay => this.ToString();
}

0 comments on commit 3236f79

Please sign in to comment.