Skip to content

Commit

Permalink
[BGen] Simplify BindAs for smart enums. (#22222)
Browse files Browse the repository at this point in the history
This changes does two things:

1. Adds a GetValue internal helper for non value backend smart enums.
2. Uses the GetValue functions as the builder for the NSArray.

The new method (1) is only added to those smart enums that use a none
value type backing field that looks like the following:

```csharp
internal static ASAuthorizationProviderAuthorizationOperation GetValue (NativeHandle handle)
{
  using var str = Runtime.GetNSObject<NSString> (handle);
  return GetValue (str);
}
```

This is later used to retrieve the enum values from the handle in a
BindAs decorated method/property. This is very similar to what we did in
#22216 and has the same
performance considerations (there is no performance change after C# 11
and later).

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Feb 25, 2025
1 parent ed11d0e commit 82d877a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/bgen/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ void GenerateEnum (Type type)
indent--;
print ("}");

// create a helper method that will be only used by the binding when they encounter a bindas attr for a smart enum with a nsstring
if (!isBackingFieldValueType) {
if (BindingTouch.SupportsXmlDocumentation) {
print ($"/// <summary>Retrieves the <see cref=\"global::{type.FullName}\" /> value represented by the backing field value in <paramref name=\"handle\" />.</summary>");
print ($"/// <param name=\"handle\">The native handle with the name of the constant to retrieve.</param>");
}

print ("public static {0} GetValueFromHandle ({1} handle)", type.Name, NativeHandleType);
print ("{");
indent++;
print ("using var str = Runtime.GetNSObject<{0}> (handle){1};", backingFieldTypeName, !nullable ? "!" : "");
print ("return GetValue (str);");
indent--;
print ("}");
}

if (BindingTouch.SupportsXmlDocumentation) {
print ($"/// <summary>Converts an array of <see cref=\"global::{type.FullName}\" /> enum values into an array of their corresponding constants.</summary>");
print ($"/// <param name=\"values\">The array of enum values to convert.</param>");
Expand Down
2 changes: 1 addition & 1 deletion src/bgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ string GetFromBindAsWrapper (MemberInformation minfo, out string suffix)
var arrRetType = arrIsNullable ? nullableElementType : retType.GetElementType ();
var valueFetcher = string.Empty;
if (arrType == TypeCache.NSString && !arrIsNullable)
append = $"ptr => {{\n\tusing (var str = Runtime.GetNSObject<NSString> (ptr)!) {{\n\t\treturn {TypeManager.FormatType (arrRetType.DeclaringType, arrRetType)}Extensions.GetValue (str);\n\t}}\n}}";
append = $"{TypeManager.FormatType (arrRetType.DeclaringType, arrRetType)}Extensions.GetValueFromHandle";
else if (arrType == TypeCache.NSNumber && !arrIsNullable) {
if (arrRetType.IsEnum) {
// get the underlying type of the enum and use a callback with the appropiate one
Expand Down
4 changes: 4 additions & 0 deletions tests/generator/ExpectedXmlDocs.MacCatalyst.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value named by <paramref name="constant" />.</summary>
<param name="constant">The name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.GetValueFromHandle(ObjCRuntime.NativeHandle)">
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value represented by the backing field value in <paramref name="handle" />.</summary>
<param name="handle">The native handle with the name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.ToConstantArray(XmlDocumentation.E2[])">
<summary>Converts an array of <see cref="T:XmlDocumentation.E2" /> enum values into an array of their corresponding constants.</summary>
<param name="values">The array of enum values to convert.</param>
Expand Down
4 changes: 4 additions & 0 deletions tests/generator/ExpectedXmlDocs.iOS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value named by <paramref name="constant" />.</summary>
<param name="constant">The name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.GetValueFromHandle(ObjCRuntime.NativeHandle)">
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value represented by the backing field value in <paramref name="handle" />.</summary>
<param name="handle">The native handle with the name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.ToConstantArray(XmlDocumentation.E2[])">
<summary>Converts an array of <see cref="T:XmlDocumentation.E2" /> enum values into an array of their corresponding constants.</summary>
<param name="values">The array of enum values to convert.</param>
Expand Down
4 changes: 4 additions & 0 deletions tests/generator/ExpectedXmlDocs.macOS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value named by <paramref name="constant" />.</summary>
<param name="constant">The name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.GetValueFromHandle(ObjCRuntime.NativeHandle)">
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value represented by the backing field value in <paramref name="handle" />.</summary>
<param name="handle">The native handle with the name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.ToConstantArray(XmlDocumentation.E2[])">
<summary>Converts an array of <see cref="T:XmlDocumentation.E2" /> enum values into an array of their corresponding constants.</summary>
<param name="values">The array of enum values to convert.</param>
Expand Down
4 changes: 4 additions & 0 deletions tests/generator/ExpectedXmlDocs.tvOS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value named by <paramref name="constant" />.</summary>
<param name="constant">The name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.GetValueFromHandle(ObjCRuntime.NativeHandle)">
<summary>Retrieves the <see cref="T:XmlDocumentation.E2" /> value represented by the backing field value in <paramref name="handle" />.</summary>
<param name="handle">The native handle with the name of the constant to retrieve.</param>
</member>
<member name="M:XmlDocumentation.E2Extensions.ToConstantArray(XmlDocumentation.E2[])">
<summary>Converts an array of <see cref="T:XmlDocumentation.E2" /> enum values into an array of their corresponding constants.</summary>
<param name="values">The array of enum values to convert.</param>
Expand Down

8 comments on commit 82d877a

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build #82d877a] Build passed (CodeQL) ✅

Pipeline on Agent
Hash: 82d877aadc1fb0ffd950c1a0f0043efae7cd9851 [CI build]

Please sign in to comment.