Skip to content
Merged
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
21 changes: 17 additions & 4 deletions src/ObjCRuntime/BackingField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@

#if IOS

// Disable until we get around to enable + fix any issues.
#nullable disable
#nullable enable

namespace ObjCRuntime {

/// <summary>Helpers to convert between native handles and cached managed wrappers.</summary>
static class BackingField {
public static T Get<T> (ref T value, IntPtr handle, Converter<IntPtr, T> c)
/// <summary>Gets a cached managed wrapper or creates a new one from a native handle.</summary>
/// <typeparam name="T">The managed wrapper type.</typeparam>
/// <param name="value">The cached managed value.</param>
/// <param name="handle">The native handle.</param>
/// <param name="c">A converter that creates a managed wrapper from a native handle.</param>
/// <returns>The cached or newly created managed value, or <see langword="null"/> if <paramref name="handle"/> is <see cref="IntPtr.Zero"/>.</returns>
public static T? Get<T> (ref T? value, IntPtr handle, Converter<IntPtr, T?> c)
where T : class, INativeObject, IDisposable
{
c = ThrowHelper.ThrowIfNull (c);

if (handle == IntPtr.Zero) {
if (value is not null)
value.Dispose ();
Expand All @@ -59,7 +67,12 @@ public static T Get<T> (ref T value, IntPtr handle, Converter<IntPtr, T> c)
return value = c (handle);
}

public static IntPtr Save<T> (ref T value, T newValue)
/// <summary>Saves a managed wrapper in a backing field and returns the corresponding native handle.</summary>
/// <typeparam name="T">The managed wrapper type.</typeparam>
/// <param name="value">The current cached managed value.</param>
/// <param name="newValue">The new managed value to cache.</param>
/// <returns>The native handle for <paramref name="newValue"/>, or <see cref="IntPtr.Zero"/> if <paramref name="newValue"/> is <see langword="null"/>.</returns>
public static IntPtr Save<T> (ref T? value, T? newValue)
where T : class, INativeObject, IDisposable
{
#pragma warning disable RBI0014
Expand Down
Loading