Skip to content
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

[CoreFoundation] Make remaining P/Invokes have blittable signatures. #20166

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions src/CoreFoundation/CFBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,15 @@ public static CFBoolean FromBoolean (bool value)
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFBooleanGetValue (/* CFBooleanRef */ IntPtr boolean);
extern static /* Boolean */ byte CFBooleanGetValue (/* CFBooleanRef */ IntPtr boolean);

public bool Value {
get { return CFBooleanGetValue (Handle); }
get { return CFBooleanGetValue (Handle) != 0; }
}

public static bool GetValue (IntPtr boolean)
{
return CFBooleanGetValue (boolean);
return CFBooleanGetValue (boolean) != 0;
}
}
}
5 changes: 2 additions & 3 deletions src/CoreFoundation/CFMachPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ public void Invalidate ()
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CFMachPortIsValid (IntPtr handle);
extern static byte CFMachPortIsValid (IntPtr handle);
public bool IsValid {
get {
return CFMachPortIsValid (Handle);
return CFMachPortIsValid (Handle) != 0;
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/CoreFoundation/CFMutableString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#if !COREBUILD

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

Expand Down Expand Up @@ -86,8 +87,7 @@ public void Append (string @string)
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern internal bool /* Boolean */ CFStringTransform (/* CFMutableStringRef* */ IntPtr @string, /* CFRange* */ ref CFRange range, /* CFStringRef* */ IntPtr transform, [MarshalAs (UnmanagedType.I1)] /* Boolean */ bool reverse);
unsafe static extern internal byte /* Boolean */ CFStringTransform (/* CFMutableStringRef* */ IntPtr @string, /* CFRange* */ CFRange* range, /* CFStringRef* */ IntPtr transform, /* Boolean */ byte reverse);

public bool Transform (ref CFRange range, CFStringTransform transform, bool reverse)
{
Expand Down Expand Up @@ -120,13 +120,11 @@ bool Transform (ref CFRange range, IntPtr transform, bool reverse)
if (transform == IntPtr.Zero)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (transform));
str = null; // destroy any cached value
return CFStringTransform (Handle, ref range, transform, reverse);
unsafe {
return CFStringTransform (Handle, (CFRange*) Unsafe.AsPointer<CFRange> (ref range), transform, reverse ? (byte) 1 : (byte) 0) != 0;
}
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern internal bool /* Boolean */ CFStringTransform (/* CFMutableStringRef* */ IntPtr @string, /* CFRange* */ IntPtr range, /* CFStringRef* */ IntPtr transform, [MarshalAs (UnmanagedType.I1)] /* Boolean */ bool reverse);

public bool Transform (CFStringTransform transform, bool reverse)
{
return Transform (transform.GetConstant ().GetHandle (), reverse);
Expand Down Expand Up @@ -158,7 +156,9 @@ bool Transform (IntPtr transform, bool reverse)
if (transform == IntPtr.Zero)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (transform));
str = null; // destroy any cached value
return CFStringTransform (Handle, IntPtr.Zero, transform, reverse);
unsafe {
return CFStringTransform (Handle, null, transform, reverse ? (byte) 1 : (byte) 0) != 0;
}
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/CoreFoundation/CFPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ public static void RemoveAppValue (string key, NSString applicationId)
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CFPreferencesGetAppBooleanValue (IntPtr key, IntPtr applicationId,
static extern byte CFPreferencesGetAppBooleanValue (IntPtr key, IntPtr applicationId,
/*out bool*/ IntPtr keyExistsAndHasValidFormat);

public static bool GetAppBooleanValue (string key)
Expand Down Expand Up @@ -200,7 +199,7 @@ public static bool GetAppBooleanValue (string key, NSString applicationId)
}

using (var cfKey = new CFString (key)) {
return CFPreferencesGetAppBooleanValue (cfKey.Handle, applicationId.Handle, IntPtr.Zero);
return CFPreferencesGetAppBooleanValue (cfKey.Handle, applicationId.Handle, IntPtr.Zero) != 0;
}
}

Expand Down Expand Up @@ -308,8 +307,7 @@ public static void RemoveSuitePreferencesFromApp (NSString applicationId, string
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CFPreferencesAppSynchronize (IntPtr applicationId);
static extern byte CFPreferencesAppSynchronize (IntPtr applicationId);

public static bool AppSynchronize ()
{
Expand All @@ -335,12 +333,11 @@ public static bool AppSynchronize (NSString applicationId)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (applicationId));
}

return CFPreferencesAppSynchronize (applicationId.Handle);
return CFPreferencesAppSynchronize (applicationId.Handle) != 0;
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CFPreferencesAppValueIsForced (IntPtr key, IntPtr applicationId);
static extern byte CFPreferencesAppValueIsForced (IntPtr key, IntPtr applicationId);

public static bool AppValueIsForced (string key)
{
Expand Down Expand Up @@ -369,7 +366,7 @@ public static bool AppValueIsForced (string key, NSString applicationId)
}

using (var cfKey = new CFString (key)) {
return CFPreferencesAppValueIsForced (cfKey.Handle, applicationId.Handle);
return CFPreferencesAppValueIsForced (cfKey.Handle, applicationId.Handle) != 0;
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/CoreFoundation/CFPropertyList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public CFPropertyList (NativeHandle handle) : this (handle, false)
#endif

[DllImport (Constants.CoreFoundationLibrary)]
static extern IntPtr CFPropertyListCreateWithData (IntPtr allocator, IntPtr dataRef, nuint options, out nint format, /* CFError * */ out IntPtr error);
unsafe static extern IntPtr CFPropertyListCreateWithData (IntPtr allocator, IntPtr dataRef, nuint options, nint* format, /* CFError * */ IntPtr* error);

public static (CFPropertyList? PropertyList, CFPropertyListFormat Format, NSError? Error)
FromData (NSData data, CFPropertyListMutabilityOptions options = CFPropertyListMutabilityOptions.Immutable)
Expand All @@ -72,7 +72,10 @@ public static (CFPropertyList? PropertyList, CFPropertyListFormat Format, NSErro

nint fmt;
IntPtr error;
var ret = CFPropertyListCreateWithData (IntPtr.Zero, data.Handle, (nuint) (ulong) options, out fmt, out error);
IntPtr ret;
unsafe {
ret = CFPropertyListCreateWithData (IntPtr.Zero, data.Handle, (nuint) (ulong) options, &fmt, &error);
}
if (ret != IntPtr.Zero)
return (new CFPropertyList (ret, owns: true), (CFPropertyListFormat) (long) fmt, null);
return (null, CFPropertyListFormat.XmlFormat1, new NSError (error));
Expand All @@ -87,24 +90,26 @@ public CFPropertyList DeepCopy (CFPropertyListMutabilityOptions options = CFProp
}

[DllImport (Constants.CoreFoundationLibrary)]
extern static /*CFDataRef*/IntPtr CFPropertyListCreateData (IntPtr allocator, IntPtr propertyList, nint format, nuint options, out IntPtr error);
unsafe extern static /*CFDataRef*/IntPtr CFPropertyListCreateData (IntPtr allocator, IntPtr propertyList, nint format, nuint options, IntPtr* error);

public (NSData? Data, NSError? Error) AsData (CFPropertyListFormat format = CFPropertyListFormat.BinaryFormat1)
{
IntPtr error;
var x = CFPropertyListCreateData (IntPtr.Zero, Handle, (nint) (long) format, 0, out error);
IntPtr x;
unsafe {
x = CFPropertyListCreateData (IntPtr.Zero, Handle, (nint) (long) format, 0, &error);
}
if (x == IntPtr.Zero)
return (null, new NSError (error));
return (Runtime.GetNSObject<NSData> (x, owns: true), null);
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CFPropertyListIsValid (IntPtr plist, nint format);
extern static byte CFPropertyListIsValid (IntPtr plist, nint format);

public bool IsValid (CFPropertyListFormat format)
{
return CFPropertyListIsValid (Handle, (nint) (long) format);
return CFPropertyListIsValid (Handle, (nint) (long) format) != 0;
}

public object? Value {
Expand Down
15 changes: 6 additions & 9 deletions src/CoreFoundation/CFReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ internal CFReadStream (NativeHandle handle, bool owns)
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFReadStreamOpen (/* CFReadStreamRef */ IntPtr stream);
extern static /* Boolean */ byte CFReadStreamOpen (/* CFReadStreamRef */ IntPtr stream);

protected override bool DoOpen ()
{
return CFReadStreamOpen (Handle);
return CFReadStreamOpen (Handle) != 0;
}

[DllImport (Constants.CoreFoundationLibrary)]
Expand All @@ -108,12 +107,11 @@ protected override CFStreamStatus DoGetStatus ()
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFReadStreamHasBytesAvailable (/* CFReadStreamRef */ IntPtr stream);
extern static /* Boolean */ byte CFReadStreamHasBytesAvailable (/* CFReadStreamRef */ IntPtr stream);

public bool HasBytesAvailable ()
{
return CFReadStreamHasBytesAvailable (Handle);
return CFReadStreamHasBytesAvailable (Handle) != 0;
}

[DllImport (Constants.CoreFoundationLibrary)]
Expand Down Expand Up @@ -209,14 +207,13 @@ protected override IntPtr DoGetProperty (NSString name)
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFReadStreamSetProperty (/* CFReadStreamRef */ IntPtr stream, /* CFStreamRef */ IntPtr propertyName, /* CFTypeRef */ IntPtr propertyValue);
extern static /* Boolean */ byte CFReadStreamSetProperty (/* CFReadStreamRef */ IntPtr stream, /* CFStreamRef */ IntPtr propertyName, /* CFTypeRef */ IntPtr propertyValue);

protected override bool DoSetProperty (NSString name, INativeObject? value)
{
if (name is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (name));
return CFReadStreamSetProperty (Handle, name.Handle, value.GetHandle ());
return CFReadStreamSetProperty (Handle, name.Handle, value.GetHandle ()) != 0;
}
}
}
26 changes: 13 additions & 13 deletions src/CoreFoundation/CFRunLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ public void Invalidate ()
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFRunLoopSourceIsValid (/* CFRunLoopSourceRef */ IntPtr source);
extern static /* Boolean */ byte CFRunLoopSourceIsValid (/* CFRunLoopSourceRef */ IntPtr source);

public bool IsValid {
get {
return CFRunLoopSourceIsValid (Handle);
return CFRunLoopSourceIsValid (Handle) != 0;
}
}

Expand All @@ -155,7 +154,7 @@ public abstract class CFRunLoopSourceCustom : CFRunLoopSource {
GCHandle gch;

[DllImport (Constants.CoreFoundationLibrary)]
extern static /* CFRunLoopSourceRef */ IntPtr CFRunLoopSourceCreate (/* CFAllocatorRef */ IntPtr allocator, /* CFIndex */ nint order, /* CFRunLoopSourceContext* */ ref CFRunLoopSourceContext context);
unsafe extern static /* CFRunLoopSourceRef */ IntPtr CFRunLoopSourceCreate (/* CFAllocatorRef */ IntPtr allocator, /* CFIndex */ nint order, /* CFRunLoopSourceContext* */ CFRunLoopSourceContext* context);

#if !NET
static ScheduleCallback ScheduleDelegate = (ScheduleCallback) Schedule;
Expand All @@ -181,7 +180,10 @@ protected CFRunLoopSourceCustom ()
ctx.Perform = Marshal.GetFunctionPointerForDelegate (PerformDelegate);
#endif

var handle = CFRunLoopSourceCreate (IntPtr.Zero, 0, ref ctx);
IntPtr handle;
unsafe {
handle = CFRunLoopSourceCreate (IntPtr.Zero, 0, &ctx);
}
InitializeHandle (handle);
}

Expand Down Expand Up @@ -307,26 +309,25 @@ public void WakeUp ()
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFRunLoopIsWaiting (/* CFRunLoopRef */ IntPtr rl);
extern static /* Boolean */ byte CFRunLoopIsWaiting (/* CFRunLoopRef */ IntPtr rl);

public bool IsWaiting {
get {
return CFRunLoopIsWaiting (Handle);
return CFRunLoopIsWaiting (Handle) != 0;
}
}

[DllImport (Constants.CoreFoundationLibrary)]
extern static CFRunLoopExitReason /* SInt32 */ CFRunLoopRunInMode (/* CFStringRef */ IntPtr mode,
/* CFTimeInterval */ double seconds,
/* Boolean */ [MarshalAs (UnmanagedType.I1)] bool returnAfterSourceHandled);
/* Boolean */ byte returnAfterSourceHandled);

public CFRunLoopExitReason RunInMode (NSString mode, double seconds, bool returnAfterSourceHandled)
{
if (mode is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (mode));

return CFRunLoopRunInMode (mode.Handle, seconds, returnAfterSourceHandled);
return CFRunLoopRunInMode (mode.Handle, seconds, returnAfterSourceHandled ? (byte) 1 : (byte) 0);
}

public CFRunLoopExitReason RunInMode (string mode, double seconds, bool returnAfterSourceHandled)
Expand All @@ -348,8 +349,7 @@ public void AddSource (CFRunLoopSource source, NSString mode)
}

[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFRunLoopContainsSource (/* CFRunLoopRef */ IntPtr rl, /* CFRunLoopSourceRef */ IntPtr source, /* CFStringRef */ IntPtr mode);
extern static /* Boolean */ byte CFRunLoopContainsSource (/* CFRunLoopRef */ IntPtr rl, /* CFRunLoopSourceRef */ IntPtr source, /* CFStringRef */ IntPtr mode);

public bool ContainsSource (CFRunLoopSource source, NSString mode)
{
Expand All @@ -358,7 +358,7 @@ public bool ContainsSource (CFRunLoopSource source, NSString mode)
if (mode is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (mode));

return CFRunLoopContainsSource (Handle, source.Handle, mode.Handle);
return CFRunLoopContainsSource (Handle, source.Handle, mode.Handle) != 0;
}

[DllImport (Constants.CoreFoundationLibrary)]
Expand Down
7 changes: 3 additions & 4 deletions src/CoreFoundation/CFString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,15 @@ public int Length {
}
}

[DllImport (Constants.CoreFoundationLibrary, CharSet = CharSet.Unicode)]
[return: MarshalAs (UnmanagedType.U2)]
extern static char CFStringGetCharacterAtIndex (IntPtr handle, nint p);
[DllImport (Constants.CoreFoundationLibrary)]
extern static ushort CFStringGetCharacterAtIndex (IntPtr handle, nint p);

public char this [nint p] {
get {
if (str is not null)
return str [(int) p];
else
return CFStringGetCharacterAtIndex (Handle, p);
return (char) CFStringGetCharacterAtIndex (Handle, p);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/CoreFoundation/CFType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ internal CFType (NativeHandle handle, bool owns)
return CFString.FromHandle (CFCopyDescription (handle));
}

[DllImport (Constants.CoreFoundationLibrary, EntryPoint = "CFEqual")]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CFEqual (/*CFTypeRef*/ IntPtr cf1, /*CFTypeRef*/ IntPtr cf2);
[DllImport (Constants.CoreFoundationLibrary)]
extern static byte CFEqual (/*CFTypeRef*/ IntPtr cf1, /*CFTypeRef*/ IntPtr cf2);

public static bool Equal (IntPtr cf1, IntPtr cf2)
{
Expand All @@ -63,7 +62,7 @@ public static bool Equal (IntPtr cf1, IntPtr cf2)
return cf2 == IntPtr.Zero;
else if (cf2 == IntPtr.Zero)
return false;
return CFEqual (cf1, cf2);
return CFEqual (cf1, cf2) != 0;
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/CoreFoundation/CFUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class CFUrl : NativeObject {
extern static /* CFURLRef */ IntPtr CFURLCreateWithFileSystemPath (/* CFAllocatorRef */ IntPtr allocator,
/* CFStringRef */ IntPtr filePath,
/* CFURLPathStyle */ nint pathStyle,
/* Boolean */ [MarshalAs (UnmanagedType.I1)] bool isDirectory);
/* Boolean */ byte isDirectory);

[Preserve (Conditional = true)]
internal CFUrl (NativeHandle handle, bool owns)
Expand All @@ -77,7 +77,7 @@ internal CFUrl (NativeHandle handle, bool owns)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (filename));
var strHandle = CFString.CreateNative (filename);
try {
var handle = CFURLCreateWithFileSystemPath (IntPtr.Zero, strHandle, (nint) (long) CFUrlPathStyle.POSIX, false);
var handle = CFURLCreateWithFileSystemPath (IntPtr.Zero, strHandle, (nint) (long) CFUrlPathStyle.POSIX, (byte) 0);
if (handle == IntPtr.Zero)
return null;
return new CFUrl (handle, true);
Expand Down Expand Up @@ -141,8 +141,7 @@ public string? FileSystemPath {
[SupportedOSPlatform ("tvos")]
#endif
[DllImport (Constants.CoreFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static /* Boolean */ bool CFURLIsFileReferenceURL (/* CFURLRef */IntPtr url);
extern static /* Boolean */ byte CFURLIsFileReferenceURL (/* CFURLRef */IntPtr url);

#if NET
[SupportedOSPlatform ("ios")]
Expand All @@ -152,7 +151,7 @@ public string? FileSystemPath {
#endif
public bool IsFileReference {
get {
return CFURLIsFileReferenceURL (Handle);
return CFURLIsFileReferenceURL (Handle) != 0;
}
}

Expand Down
Loading
Loading