Skip to content

Commit

Permalink
[RGen] Add the new added methods to smart enums. (#22250)
Browse files Browse the repository at this point in the history
In order to simplify the BindAs/BindFrom code, new methods were added to
the smart enums generated by bgen. We have to do the same in rgen.

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Feb 28, 2025
1 parent d0107f4 commit c31fe77
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ void EmitExtensionMethods (TabbedWriter<StringWriter> classBlock, in Binding bin
getValueBlock.WriteLine (
"throw new NotSupportedException ($\"The constant {constant} has no associated enum value on this platform.\");");
}
classBlock.WriteLine ();

// get value from a handle, this is a helper method used in the BindAs bindings.
using (var getValueFromHandle =
classBlock.CreateBlock ($"public static {binding.Name} GetValueFromHandle (NativeHandle handle)",
true)) {
getValueFromHandle.WriteRaw (
@"using var str = Runtime.GetNSObject<NSString> (handle)!;
return GetValue (str);
");
}

classBlock.WriteLine ();
// To ConstantArray
Expand Down
1 change: 1 addition & 0 deletions tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23150,6 +23150,7 @@ M:AVFoundation.AVCapturePhotoSettingsThumbnailFormat.#ctor
M:AVFoundation.AVCapturePhotoSettingsThumbnailFormat.#ctor(Foundation.NSDictionary)
M:AVFoundation.AVCaptureReactionTypeExtensions.GetConstant(AVFoundation.AVCaptureReactionType)
M:AVFoundation.AVCaptureReactionTypeExtensions.GetValue(Foundation.NSString)
M:AVFoundation.AVCaptureReactionTypeExtensions.GetValueFromHandle(ObjCRuntime.NativeHandle)
M:AVFoundation.AVCaptureSessionRuntimeErrorEventArgs.#ctor(Foundation.NSNotification)
M:AVFoundation.AVCaptureStillImageOutput.CaptureStillImageTaskAsync(AVFoundation.AVCaptureConnection)
M:AVFoundation.AVCaptureVideoDataOutput.GetRecommendedVideoSettings(AVFoundation.AVVideoCodecType,AVFoundation.AVFileTypes,Foundation.NSUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ public static AVCaptureDeviceType GetValue (NSString constant)
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
}

public static AVCaptureDeviceType GetValueFromHandle (NativeHandle handle)
{
using var str = Runtime.GetNSObject<NSString> (handle)!;
return GetValue (str);
}

internal static NSString?[]? ToConstantArray (this AVCaptureDeviceType[]? values)
{
if (values is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public static AVCaptureSystemPressureLevel GetValue (NSString constant)
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
}

public static AVCaptureSystemPressureLevel GetValueFromHandle (NativeHandle handle)
{
using var str = Runtime.GetNSObject<NSString> (handle)!;
return GetValue (str);
}

internal static NSString?[]? ToConstantArray (this AVCaptureSystemPressureLevel[]? values)
{
if (values is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public static CustomLibraryEnum GetValue (NSString constant)
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
}

public static CustomLibraryEnum GetValueFromHandle (NativeHandle handle)
{
using var str = Runtime.GetNSObject<NSString> (handle)!;
return GetValue (str);
}

internal static NSString?[]? ToConstantArray (this CustomLibraryEnum[]? values)
{
if (values is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public static CustomLibraryEnumInternal GetValue (NSString constant)
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
}

public static CustomLibraryEnumInternal GetValueFromHandle (NativeHandle handle)
{
using var str = Runtime.GetNSObject<NSString> (handle)!;
return GetValue (str);
}

internal static NSString?[]? ToConstantArray (this CustomLibraryEnumInternal[]? values)
{
if (values is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ public static AVMediaCharacteristics GetValue (NSString constant)
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
}

public static AVMediaCharacteristics GetValueFromHandle (NativeHandle handle)
{
using var str = Runtime.GetNSObject<NSString> (handle)!;
return GetValue (str);
}

internal static NSString?[]? ToConstantArray (this AVMediaCharacteristics[]? values)
{
if (values is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ public static AVMediaCharacteristics GetValue (NSString constant)
throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform.");
}

public static AVMediaCharacteristics GetValueFromHandle (NativeHandle handle)
{
using var str = Runtime.GetNSObject<NSString> (handle)!;
return GetValue (str);
}

internal static NSString?[]? ToConstantArray (this AVMediaCharacteristics[]? values)
{
if (values is null)
Expand Down

8 comments on commit c31fe77

@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

This comment was marked as outdated.

Please sign in to comment.