Skip to content

Commit

Permalink
[Foundation] Make P/Invokes in NSDecimal.cs have blittable signatures.
Browse files Browse the repository at this point in the history
Contributes towards xamarin#15684.
  • Loading branch information
rolfbjarne committed Dec 22, 2023
1 parent 51aaac3 commit 0f85419
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 39 deletions.
102 changes: 73 additions & 29 deletions src/Foundation/NSDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

Expand All @@ -53,74 +54,117 @@ public struct NSDecimal

#if !COREBUILD
[DllImport (Constants.FoundationLibrary)]
static extern nint NSDecimalCompare (ref NSDecimal left, ref NSDecimal right);
public static NSComparisonResult Compare (ref NSDecimal left, ref NSDecimal right)
unsafe static extern nint NSDecimalCompare (NSDecimal* left, NSDecimal* right);
public unsafe static NSComparisonResult Compare (ref NSDecimal left, ref NSDecimal right)
{
return (NSComparisonResult) (long) NSDecimalCompare (ref left, ref right);
return (NSComparisonResult) (long) NSDecimalCompare (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref left),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref right));
}

[DllImport (Constants.FoundationLibrary)]
static extern void NSDecimalRound (out NSDecimal result, ref NSDecimal number, nint scale, nuint mode);
public static void Round (out NSDecimal result, ref NSDecimal number, nint scale, NSRoundingMode mode)
unsafe static extern void NSDecimalRound (NSDecimal* result, NSDecimal* number, nint scale, nuint mode);
public unsafe static void Round (out NSDecimal result, ref NSDecimal number, nint scale, NSRoundingMode mode)
{
NSDecimalRound (out result, ref number, scale, (nuint) (ulong) mode);
result = default (NSDecimal);
NSDecimalRound (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref result),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref number),
scale,
(nuint) (ulong) mode);
}

[DllImport (Constants.FoundationLibrary)]
static extern nuint NSDecimalNormalize (ref NSDecimal number1, ref NSDecimal number2);
public static NSCalculationError Normalize (ref NSDecimal number1, ref NSDecimal number2)
unsafe static extern nuint NSDecimalNormalize (NSDecimal* number1, NSDecimal* number2);
public unsafe static NSCalculationError Normalize (ref NSDecimal number1, ref NSDecimal number2)
{
return (NSCalculationError) (ulong) NSDecimalNormalize (ref number1, ref number2);
return (NSCalculationError) (ulong) NSDecimalNormalize (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref number1),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref number2));
}

[DllImport (Constants.FoundationLibrary)]
static extern nuint NSDecimalAdd (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode);
public static NSCalculationError Add (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
static unsafe extern nuint NSDecimalAdd (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode);
public unsafe static NSCalculationError Add (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
{
return (NSCalculationError) (ulong) NSDecimalAdd (out result, ref left, ref right, (nuint) (ulong) mode);
result = default (NSDecimal);
return (NSCalculationError) (ulong) NSDecimalAdd (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref result),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref left),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref right),
(nuint) (ulong) mode);
}

[DllImport (Constants.FoundationLibrary)]
static extern nuint NSDecimalSubtract (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode);
public static NSCalculationError Subtract (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
unsafe static extern nuint NSDecimalSubtract (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode);
public unsafe static NSCalculationError Subtract (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
{
return (NSCalculationError) (ulong) NSDecimalSubtract (out result, ref left, ref right, (nuint) (ulong) mode);
result = default (NSDecimal);
return (NSCalculationError) (ulong) NSDecimalSubtract (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref result),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref left),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref right),
(nuint) (ulong) mode);
}

[DllImport (Constants.FoundationLibrary)]
static extern nuint NSDecimalMultiply (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode);
public static NSCalculationError Multiply (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
static unsafe extern nuint NSDecimalMultiply (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode);
public unsafe static NSCalculationError Multiply (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
{
return (NSCalculationError) (ulong) NSDecimalMultiply (out result, ref left, ref right, (nuint) (ulong) mode);
result = default (NSDecimal);
return (NSCalculationError) (ulong) NSDecimalMultiply (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref result),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref left),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref right),
(nuint) (ulong) mode);
}

[DllImport (Constants.FoundationLibrary)]
static extern nuint NSDecimalDivide (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode);
public static NSCalculationError Divide (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
unsafe static extern nuint NSDecimalDivide (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode);
public unsafe static NSCalculationError Divide (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode)
{
return (NSCalculationError) (ulong) NSDecimalDivide (out result, ref left, ref right, (nuint) (ulong) mode);
result = default (NSDecimal);
return (NSCalculationError) (ulong) NSDecimalDivide (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref result),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref left),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref right),
(nuint) (ulong) mode);
}

[DllImport (Constants.FoundationLibrary)]
static extern nuint NSDecimalPower (out NSDecimal result, ref NSDecimal number, nint power, nuint mode);
public static NSCalculationError Power (out NSDecimal result, ref NSDecimal number, nint power, NSRoundingMode mode)
unsafe static extern nuint NSDecimalPower (NSDecimal* result, NSDecimal* number, nint power, nuint mode);
public unsafe static NSCalculationError Power (out NSDecimal result, ref NSDecimal number, nint power, NSRoundingMode mode)
{
return (NSCalculationError) (ulong) NSDecimalPower (out result, ref number, power, (nuint) (ulong) mode);
result = default (NSDecimal);
return (NSCalculationError) (ulong) NSDecimalPower (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref result),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref number),
power,
(nuint) (ulong) mode);
}

[DllImport (Constants.FoundationLibrary)]
static extern nuint NSDecimalMultiplyByPowerOf10 (out NSDecimal result, ref NSDecimal number, short power10, nuint mode);
public static NSCalculationError MultiplyByPowerOf10 (out NSDecimal result, ref NSDecimal number, short power10, NSRoundingMode mode)
unsafe static extern nuint NSDecimalMultiplyByPowerOf10 (NSDecimal* result, NSDecimal* number, short power10, nuint mode);
public unsafe static NSCalculationError MultiplyByPowerOf10 (out NSDecimal result, ref NSDecimal number, short power10, NSRoundingMode mode)
{
return (NSCalculationError) (ulong) NSDecimalMultiplyByPowerOf10 (out result, ref number, power10, (nuint) (ulong) mode);
result = default (NSDecimal);
return (NSCalculationError) (ulong) NSDecimalMultiplyByPowerOf10 (
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref result),
(NSDecimal *) Unsafe.AsPointer<NSDecimal> (ref number),
power10,
(nuint) (ulong) mode);
}

[DllImport (Constants.FoundationLibrary)]
static extern IntPtr NSDecimalString (ref NSDecimal value, /* _Nullable */ IntPtr locale);
unsafe static extern IntPtr NSDecimalString (NSDecimal* value, /* _Nullable */ IntPtr locale);

public override string ToString ()
{
return new NSString (NSDecimalString (ref this, NSLocale.CurrentLocale.Handle));
unsafe {
fixed (NSDecimal* self = &this) {
return new NSString (NSDecimalString (self, NSLocale.CurrentLocale.Handle));
}
}
}

public static NSDecimal operator + (NSDecimal left, NSDecimal right)
Expand Down
10 changes: 0 additions & 10 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,6 @@ public partial class BlittablePInvokes {
"System.IntPtr CoreText.CTRunDelegate::CTRunDelegateCreate(CoreText.CTRunDelegateCallbacks&,System.IntPtr)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferCopyAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferGetAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr Foundation.NSDecimal::NSDecimalCompare(Foundation.NSDecimal&,Foundation.NSDecimal&)",
"System.IntPtr Foundation.NSDecimal::NSDecimalString(Foundation.NSDecimal&,System.IntPtr)",
"System.IntPtr Foundation.NSSearchPath::NSSearchPathForDirectoriesInDomains(System.UIntPtr,System.UIntPtr,System.Boolean)",
"System.IntPtr Foundation.NSThread::xamarin_init_nsthread(System.IntPtr,System.Boolean,System.IntPtr,System.IntPtr,System.IntPtr)",
"System.IntPtr GameController.GCExtendedGamepadSnapshotData::NSDataFromGCExtendedGamepadSnapshotData(GameController.GCExtendedGamepadSnapshotData&)",
Expand Down Expand Up @@ -760,13 +758,6 @@ public partial class BlittablePInvokes {
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetRelativeCharacterSize(System.IntPtr,System.IntPtr&)",
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetWindowOpacity(System.IntPtr,System.IntPtr&)",
"System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetWindowRoundedCornerRadius(System.IntPtr,System.IntPtr&)",
"System.UIntPtr Foundation.NSDecimal::NSDecimalAdd(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)",
"System.UIntPtr Foundation.NSDecimal::NSDecimalDivide(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)",
"System.UIntPtr Foundation.NSDecimal::NSDecimalMultiply(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)",
"System.UIntPtr Foundation.NSDecimal::NSDecimalMultiplyByPowerOf10(Foundation.NSDecimal&,Foundation.NSDecimal&,System.Int16,System.UIntPtr)",
"System.UIntPtr Foundation.NSDecimal::NSDecimalNormalize(Foundation.NSDecimal&,Foundation.NSDecimal&)",
"System.UIntPtr Foundation.NSDecimal::NSDecimalPower(Foundation.NSDecimal&,Foundation.NSDecimal&,System.IntPtr,System.UIntPtr)",
"System.UIntPtr Foundation.NSDecimal::NSDecimalSubtract(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)",
"System.Void AppKit.NSCell::NSDrawNinePartImage(CoreGraphics.CGRect,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.Runtime.InteropServices.NFloat,System.Boolean)",
"System.Void AppKit.NSCell::NSDrawThreePartImage(CoreGraphics.CGRect,System.IntPtr,System.IntPtr,System.IntPtr,System.Boolean,System.IntPtr,System.Runtime.InteropServices.NFloat,System.Boolean)",
"System.Void CoreFoundation.CFBundle::CFBundleGetPackageInfo(System.IntPtr,System.UInt32&,System.UInt32&)",
Expand Down Expand Up @@ -794,7 +785,6 @@ public partial class BlittablePInvokes {
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontsWithAssetNames(System.IntPtr,System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontURLs(System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
"System.Void CoreVideo.CVPixelBuffer::CVPixelBufferGetExtendedPixels(System.IntPtr,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&)",
"System.Void Foundation.NSDecimal::NSDecimalRound(Foundation.NSDecimal&,Foundation.NSDecimal&,System.IntPtr,System.UIntPtr)",
"System.Void Foundation.NSObject::xamarin_release_managed_ref(System.IntPtr,System.Boolean)",
"System.Void ImageIO.CGImageSource::CGImageSourceUpdateData(System.IntPtr,System.IntPtr,System.Boolean)",
"System.Void ImageIO.CGImageSource::CGImageSourceUpdateDataProvider(System.IntPtr,System.IntPtr,System.Boolean)",
Expand Down

0 comments on commit 0f85419

Please sign in to comment.