-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix support for non Apple frameworks and add a test.
- Loading branch information
1 parent
926570d
commit d1a186f
Showing
4 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/CustomLibraryEnum.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
103 changes: 103 additions & 0 deletions
103
tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters