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] Add a few missing APIs to CFRunLoop and enable nullability. #18322

Merged
merged 4 commits into from
May 30, 2023
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
35 changes: 33 additions & 2 deletions src/CoreFoundation/CFRunLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
using NativeHandle = System.IntPtr;
#endif

#nullable enable

namespace CoreFoundation {

// anonymous and typeless native enum - System/Library/Frameworks/CoreFoundation.framework/Headers/CFRunLoop.h
Expand Down Expand Up @@ -183,7 +185,9 @@ protected CFRunLoopSourceCustom ()
InitializeHandle (handle);
}

#if !NET
delegate void ScheduleCallback (IntPtr info, IntPtr runLoop, IntPtr mode);
#endif

#if NET
[UnmanagedCallersOnly]
Expand All @@ -204,7 +208,9 @@ static void Schedule (IntPtr info, IntPtr runLoop, IntPtr mode)

protected abstract void OnSchedule (CFRunLoop loop, NSString mode);

#if !NET
delegate void CancelCallback (IntPtr info, IntPtr runLoop, IntPtr mode);
#endif

#if NET
[UnmanagedCallersOnly]
Expand All @@ -225,7 +231,9 @@ static void Cancel (IntPtr info, IntPtr runLoop, IntPtr mode)

protected abstract void OnCancel (CFRunLoop loop, NSString mode);

#if !NET
delegate void PerformCallback (IntPtr info);
#endif

#if NET
[UnmanagedCallersOnly]
Expand Down Expand Up @@ -321,6 +329,11 @@ public CFRunLoopExitReason RunInMode (NSString mode, double seconds, bool return
return CFRunLoopRunInMode (mode.Handle, seconds, returnAfterSourceHandled);
}

public CFRunLoopExitReason RunInMode (string mode, double seconds, bool returnAfterSourceHandled)
{
return RunInMode ((NSString) mode, seconds, returnAfterSourceHandled);
}

[DllImport (Constants.CoreFoundationLibrary)]
extern static void CFRunLoopAddSource (/* CFRunLoopRef */ IntPtr rl, /* CFRunLoopSourceRef */ IntPtr source, /* CFStringRef */ IntPtr mode);

Expand Down Expand Up @@ -367,8 +380,26 @@ internal CFRunLoop (NativeHandle handle, bool owns)
{
}

[DllImport (Constants.CoreFoundationLibrary)]
extern static NativeHandle /* CFArrayRef */ CFRunLoopCopyAllModes (NativeHandle /* CFRunLoopRef */ rl);

public string? []? AllModes {
get {
return CFArray.StringArrayFromHandle (CFRunLoopCopyAllModes (GetCheckedHandle ()), releaseHandle: true);
}
}

[DllImport (Constants.CoreFoundationLibrary)]
extern static IntPtr /* CFRunLoopMode */ CFRunLoopCopyCurrentMode (NativeHandle /* CFRunLoopRef */ rl);

public string? CurrentMode {
get {
return CFString.FromHandle (CFRunLoopCopyCurrentMode (GetCheckedHandle ()), releaseHandle: true);
}
}

#if !NET
public static bool operator == (CFRunLoop a, CFRunLoop b)
public static bool operator == (CFRunLoop? a, CFRunLoop? b)
{
if (a is null)
return b is null;
Expand All @@ -378,7 +409,7 @@ internal CFRunLoop (NativeHandle handle, bool owns)
return a.Handle == b.Handle;
}

public static bool operator != (CFRunLoop a, CFRunLoop b)
public static bool operator != (CFRunLoop? a, CFRunLoop? b)
{
if (a is null)
return b is not null;
Expand Down
44 changes: 44 additions & 0 deletions tests/monotouch-test/CoreFoundation/RunLoopTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Unit tests for CFRunLoop
//
// Authors:
// Rolf Bjarne Kvinge <rolf@xamarin.com>
//
// Copyright 2023 Microsoft Corp. All rights reserved.
//

using System;
using Foundation;
using CoreFoundation;

using NUnit.Framework;

namespace MonoTouchFixtures.CoreFoundation {

[TestFixture]
[Preserve (AllMembers = true)]
public class RunLoopTest {

[Test]
public void AllModes ()
{
Assert.That (CFRunLoop.Main.AllModes, Is.Not.Empty, "AllModes");
}

[Test]
public void CurrentMode ()
{
Assert.DoesNotThrow (() => { GC.KeepAlive (CFRunLoop.Main.CurrentMode); }, "CurrentMode");
}

[Test]
public void RunInMode ()
{
var loop = CFRunLoop.Main;
Assert.DoesNotThrow (() => { loop.RunInMode ((string) loop.AllModes [0], 0.01, false); }, "RunInMode (string, false)");
Assert.DoesNotThrow (() => { loop.RunInMode ((string) loop.AllModes [0], 0.01, true); }, "RunInMode (string, true)");
Assert.DoesNotThrow (() => { loop.RunInMode ((NSString) loop.AllModes [0], 0.01, false); }, "RunInMode (NSString, false)");
Assert.DoesNotThrow (() => { loop.RunInMode ((NSString) loop.AllModes [0], 0.01, true); }, "RunInMode (NSString, true)");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,6 @@
!missing-pinvoke! CFRunLoopAddTimer is not bound
!missing-pinvoke! CFRunLoopContainsObserver is not bound
!missing-pinvoke! CFRunLoopContainsTimer is not bound
!missing-pinvoke! CFRunLoopCopyAllModes is not bound
!missing-pinvoke! CFRunLoopCopyCurrentMode is not bound
!missing-pinvoke! CFRunLoopGetNextTimerFireDate is not bound
!missing-pinvoke! CFRunLoopGetTypeID is not bound
!missing-pinvoke! CFRunLoopObserverCreate is not bound
Expand Down
2 changes: 0 additions & 2 deletions tests/xtro-sharpie/common-CoreFoundation.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,6 @@
!missing-pinvoke! CFRunLoopAddTimer is not bound
!missing-pinvoke! CFRunLoopContainsObserver is not bound
!missing-pinvoke! CFRunLoopContainsTimer is not bound
!missing-pinvoke! CFRunLoopCopyAllModes is not bound
!missing-pinvoke! CFRunLoopCopyCurrentMode is not bound
!missing-pinvoke! CFRunLoopGetNextTimerFireDate is not bound
!missing-pinvoke! CFRunLoopGetTypeID is not bound
!missing-pinvoke! CFRunLoopObserverCreate is not bound
Expand Down