Skip to content

[RGen] Add the new added methods to smart enums. #22250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)",
Copy link
Member

Choose a reason for hiding this comment

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

Can this be internal instead of public? That way we don't have to worry about API diffs, etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can it when we are using it for bindings? I don't think so right?

Copy link
Member

Choose a reason for hiding this comment

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

That depends on where these methods are going to be used. Is it always going to be from the same assembly?

Copy link
Member Author

Choose a reason for hiding this comment

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

well, this are a diff assembly if bgen is used to generate the bindings for a client or third party. If they use BindAs in their code, they'll need this to be public.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can just use GetValue, it'll be an overload of the other `GetValue method:

Suggested change
classBlock.CreateBlock ($"public static {binding.Name} GetValueFromHandle (NativeHandle handle)",
classBlock.CreateBlock ($"public static {binding.Name} GetValue (NativeHandle handle)",

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me land this and change it in bgen too. Because bgen is already doing GetValueFromHandle. Sounds good?

true)) {
getValueFromHandle.WriteRaw (
@"using var str = Runtime.GetNSObject<NSString> (handle);
return GetValue (str);
");
}

classBlock.WriteLine ();
// To ConstantArray
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
Loading