Skip to content

Commit

Permalink
[Bgen] Simplify the NSValue BindAS generated code. (#22257)
Browse files Browse the repository at this point in the history
As with previous changes, move to use a method group instead of a lambda
with a using block.

We only add the new methods in those platforms that have the specifict
frameworks by using partial class. That way we keep the code cleaner
than with a collection of #if statements.

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Mar 3, 2025
1 parent 486ffef commit 74636f5
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 28 deletions.
25 changes: 25 additions & 0 deletions src/CoreAnimation/NSValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;

using ObjCRuntime;
using CoreAnimation;

#nullable enable

namespace Foundation;

public partial class NSValue : NSObject {

/// <summary>
/// Converts a native handle to a CATransform3D.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CATransform3D.</returns>
public static CATransform3D ToCATransform3D (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CATransform3DValue;
}
}
98 changes: 98 additions & 0 deletions src/CoreGraphics/NSValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;

using CoreGraphics;
using ObjCRuntime;

#nullable enable

namespace Foundation;

public partial class NSValue : NSObject {

/// <summary>
/// Converts a native handle to a CGAffineTransform.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CGAffineTransform.</returns>
public static CGAffineTransform ToCGAffineTransform (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CGAffineTransformValue;
}

/// <summary>
/// Converts a native handle to a CGPoint.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CGPoint.</returns>
public static CGPoint ToCGPoint (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CGPointValue;
}

/// <summary>
/// Converts a native handle to a CGRect.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CGRect.</returns>
public static CGRect ToCGRect (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CGRectValue;
}

/// <summary>
/// Converts a native handle to a CGSize.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CGSize.</returns>
public static CGSize ToCGSize (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CGSizeValue;
}

#if MONOMAC

// @encode(CGAffineTransform) -> "{CGAffineTransform=dddddd}" but...
// using a C string crash on macOS (while it works fine on iOS)
[DllImport ("__Internal")]
extern static IntPtr xamarin_encode_CGAffineTransform ();

// The `+valueWithCGAffineTransform:` selector comes from UIKit and is not available on macOS
public unsafe static NSValue FromCGAffineTransform (CGAffineTransform tran)
{
return Create ((IntPtr) (void*) &tran, xamarin_encode_CGAffineTransform ());
}

// The `CGAffineTransformValue` selector comes from UIKit and is not available on macOS
public unsafe virtual CGAffineTransform CGAffineTransformValue {
get {
var result = new CGAffineTransform ();
// avoid potential buffer overflow since we use the older `getValue:` API to cover all platforms
// and we can cheat here with the actual string comparison (since we are the one doing it)
if (ObjCType == "{CGAffineTransform=dddddd}")
StoreValueAtAddress ((IntPtr) (void*) &result);
return result;
}
}

#else

/// <summary>
/// Converts a native handle to a CGVector.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CGVector.</returns>
public static CGVector ToCGVector (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CGVectorValue;
}

#endif
}
25 changes: 25 additions & 0 deletions src/CoreLocation/NSValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;

using ObjCRuntime;
using CoreLocation;

#nullable enable

namespace Foundation;

public partial class NSValue : NSObject {

/// <summary>
/// Converts a native handle to a CLLocationCoordinate2D.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CLLocationCoordinate2D.</returns>
public static CLLocationCoordinate2D ToCLLocationCoordinate2D (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CoordinateValue;
}
}
59 changes: 59 additions & 0 deletions src/CoreMedia/NSValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;

using ObjCRuntime;
using CoreMedia;

#nullable enable

namespace Foundation;

public partial class NSValue : NSObject {

/// <summary>
/// Converts a native handle to a CMTime.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CMTime.</returns>
public static CMTimeRange ToCMTimeRange (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CMTimeRangeValue;
}

/// <summary>
/// Converts a native handle to a CMTime.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CMTime.</returns>
public static CMTime ToCMTime (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CMTimeValue;
}

/// <summary>
/// Converts a native handle to a CMTimeMapping.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CMTimeMapping.</returns>
public static CMTimeMapping ToCMTimeMapping (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CMTimeMappingValue;
}

/// <summary>
/// Converts a native handle to a CMTimeRange.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The CMTimeRange.</returns>
public static CMVideoDimensions ToCMVideoDimensions (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CMVideoDimensionsValue;
}

}
26 changes: 0 additions & 26 deletions src/Foundation/NSValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#endif
using System.Runtime.InteropServices;

using CoreGraphics;

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

Expand Down Expand Up @@ -72,30 +70,6 @@ public PointF PointFValue {
}
#endif

#if MONOMAC
// @encode(CGAffineTransform) -> "{CGAffineTransform=dddddd}" but...
// using a C string crash on macOS (while it works fine on iOS)
[DllImport ("__Internal")]
extern static IntPtr xamarin_encode_CGAffineTransform ();

// The `+valueWithCGAffineTransform:` selector comes from UIKit and is not available on macOS
public unsafe static NSValue FromCGAffineTransform (CGAffineTransform tran)
{
return Create ((IntPtr) (void*) &tran, xamarin_encode_CGAffineTransform ());
}

// The `CGAffineTransformValue` selector comes from UIKit and is not available on macOS
public unsafe virtual CGAffineTransform CGAffineTransformValue {
get {
var result = new CGAffineTransform ();
// avoid potential buffer overflow since we use the older `getValue:` API to cover all platforms
// and we can cheat here with the actual string comparison (since we are the one doing it)
if (ObjCType == "{CGAffineTransform=dddddd}")
StoreValueAtAddress ((IntPtr) (void*) &result);
return result;
}
}
#endif
#endif
}
}
26 changes: 26 additions & 0 deletions src/MapKit/NSValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;

using ObjCRuntime;
using MapKit;

#nullable enable

namespace Foundation;

public partial class NSValue : NSObject {

/// <summary>
/// Converts a native handle to a MKCoordinateRegion.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The MKCoordinateRegion.</returns>
public static MKCoordinateSpan ToMKCoordinateSpan (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.CoordinateSpanValue;
}

}
59 changes: 59 additions & 0 deletions src/SceneKit/NSValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;

using ObjCRuntime;
using SceneKit;

#nullable enable

namespace Foundation;

public partial class NSValue : NSObject {

/// <summary>
/// Converts a native handle to a SCNMatrix4.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The SCNMatrix4.</returns>
public static SCNMatrix4 ToSCNMatrix4 (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.SCNMatrix4Value;
}

/// <summary>
/// Converts a native handle to a NSRange.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The NSRange.</returns>
public static NSRange ToNSRange (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.RangeValue;
}

/// <summary>
/// Converts a native handle to a SCNVector3.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The SCNVector3.</returns>
public static SCNVector3 ToSCNVector3 (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.Vector3Value;
}

/// <summary>
/// Converts a native handle to a SCNVector4.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The SCNVector4.</returns>
public static SCNVector4 ToSCNVector4 (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.Vector4Value;
}

}
48 changes: 48 additions & 0 deletions src/UIKit/NSValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;

using ObjCRuntime;
using UIKit;

#nullable enable

namespace Foundation;

public partial class NSValue : NSObject {

/// <summary>
/// Converts a native handle to a UIEdgeInsets.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The UIEdgeInsets.</returns>
public static UIEdgeInsets ToUIEdgeInsets (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.UIEdgeInsetsValue;
}

/// <summary>
/// Converts a native handle to a UIOffset.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The UIOffset.</returns>
public static UIOffset ToUIOffset (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.UIOffsetValue;
}

/// <summary>
/// Converts a native handle to a NSDirectionalEdgeInsets.
/// </summary>
/// <param name="handle">The native handle.</param>
/// <returns>The NSDirectionalEdgeInsets.</returns>
public static NSDirectionalEdgeInsets ToNSDirectionalEdgeInsets (NativeHandle handle)
{
using var nsvalue = Runtime.GetNSObject<NSValue> (handle)!;
return nsvalue.DirectionalEdgeInsetsValue;
}
}

4 changes: 2 additions & 2 deletions src/bgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ string GetFromBindAsWrapper (MemberInformation minfo, out string suffix)
} else
throw GetBindAsException ("unbox", retType.Name, arrType.Name, "array", minfo.mi);
} else if (arrType == TypeCache.NSValue && !arrIsNullable) {
if (TypeManager.NSValueReturnMap.TryGetValue (arrRetType, out valueFetcher)) {
append = string.Format ("ptr => {{\n\tusing (var val = Runtime.GetNSObject<NSValue> (ptr)!) {{\n\t\treturn val{0};\n\t}}\n}}", valueFetcher);
if (TypeManager.NSValueBindAsMap.TryGetValue (arrRetType, out valueFetcher)) {
append = string.Format ("NSValue.{0}", valueFetcher);
} else {
throw GetBindAsException ("unbox", retType.Name, arrType.Name, "array", minfo.mi);
}
Expand Down
Loading

8 comments on commit 74636f5

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.