Skip to content

Commit

Permalink
Fix support for non Apple frameworks and add a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel-macaque committed Oct 17, 2024
1 parent 926570d commit d1a186f
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void Emit (TabbedStringBuilder classBlock, (IFieldSymbol Symbol, FieldData Field
using (var propertyBlock = classBlock.CreateBlock ($"internal unsafe static IntPtr {enumField.FieldData.SymbolName}", true))
using (var getterBlock = propertyBlock.CreateBlock ("get", true)) {
getterBlock.AppendFormatLine ("fixed (IntPtr *storage = &values [{0}])", index);
getterBlock.AppendFormatLine("\treturn Dlfcn.CachePointer (Libraries.{0}.Handle, \"{1}\", storage);",
libraryPath ?? libraryName, enumField.FieldData.SymbolName);
getterBlock.AppendFormatLine("\treturn Dlfcn.CachePointer ({0}, \"{1}\", storage);",
(libraryPath is null) ? $"Libraries.{libraryName}.Handle" : $"\"{libraryPath}\"", enumField.FieldData.SymbolName);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Runtime.Versioning;
using Foundation;
using ObjCRuntime;
using ObjCBindings;

namespace CustomLibrary;

[BindingType]
public enum CustomLibraryEnum {
[Field ("None", "/path/to/customlibrary.framework")]
None,
[Field ("Medium", "/path/to/customlibrary.framework")]
Medium,
[Field ("High", "/path/to/customlibrary.framework")]
High,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// <auto-generated>

#nullable enable

using System;
using System.Runtime.Versioning;
using Foundation;
using ObjCRuntime;
using ObjCBindings;

namespace CustomLibrary;

[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
static public partial class CustomLibraryEnumExtensions
{

static IntPtr[] values = new IntPtr [3];

[Field ("None", "/path/to/customlibrary.framework")]
internal unsafe static IntPtr None
{
get
{
fixed (IntPtr *storage = &values [0])
return Dlfcn.CachePointer ("/path/to/customlibrary.framework", "None", storage);
}
}

[Field ("Medium", "/path/to/customlibrary.framework")]
internal unsafe static IntPtr Medium
{
get
{
fixed (IntPtr *storage = &values [1])
return Dlfcn.CachePointer ("/path/to/customlibrary.framework", "Medium", storage);
}
}

[Field ("High", "/path/to/customlibrary.framework")]
internal unsafe static IntPtr High
{
get
{
fixed (IntPtr *storage = &values [2])
return Dlfcn.CachePointer ("/path/to/customlibrary.framework", "High", storage);
}
}

public static NSString? GetConstant (this CustomLibraryEnum self)
{
IntPtr ptr = IntPtr.Zero;
switch ((int) self)
{
case 0: // None
ptr = None;
break;
case 1: // Medium
ptr = Medium;
break;
case 2: // High
ptr = High;
break;
}
return (NSString?) Runtime.GetNSObject (ptr);
}

public static CustomLibraryEnum GetValue (NSString constant)
{
if (constant is null)
throw new ArgumentNullException (nameof (constant));
if (constant.IsEqualTo (None))
return CustomLibraryEnum.None;
if (constant.IsEqualTo (Medium))
return CustomLibraryEnum.Medium;
if (constant.IsEqualTo (High))
return CustomLibraryEnum.High;
throw new NotSupportedException ($"{constant} has no associated enum value on this platform.");
}

internal static NSString?[]? ToConstantArray (this CustomLibraryEnum[]? values)
{
if (values is null)
return null;
var rv = new global::System.Collections.Generic.List<NSString?> ();
for (var i = 0; i < values.Length; i++) {
var value = values [i];
rv.Add (value.GetConstant ());
}
return rv.ToArray ();
}

internal static CustomLibraryEnum[]? ToEnumArray (this NSString[]? values)
{
if (values is null)
return null;
var rv = new global::System.Collections.Generic.List<CustomLibraryEnum> ();
for (var i = 0; i < values.Length; i++) {
var value = values [i];
rv.Add (GetValue (value));
}
return rv.ToArray ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TestDataGenerator : BaseTestDataGenerator, IEnumerable<object[]>
(ApplePlatform.iOS, "AVCaptureSystemPressureLevelExtensions", "AVCaptureSystemPressureLevel.cs", "ExpectedAVCaptureSystemPressureLevel.cs" ),
(ApplePlatform.iOS, "AVMediaCharacteristicsExtensions", "AVMediaCharacteristics.cs", "ExpectediOSAVMediaCharacteristics.cs" ),
(ApplePlatform.MacOSX, "AVMediaCharacteristicsExtensions", "AVMediaCharacteristics.cs", "ExpectedMacOSAVMediaCharacteristics.cs" ),
(ApplePlatform.MacOSX, "CustomLibraryEnumExtensions", "CustomLibraryEnum.cs", "ExpectedCustomLibraryEnum.cs" ),
};

public IEnumerator<object[]> GetEnumerator()
Expand Down

0 comments on commit d1a186f

Please sign in to comment.