Skip to content

Commit

Permalink
[coregraphics] Add nullability to (generated and manual) bindings (#1…
Browse files Browse the repository at this point in the history
…5067)

* enable nullability

* throw better exceptions

* use is null and is not null

Co-authored-by: tj_devel709 <antlambe@microsoft.com>
  • Loading branch information
tj-devel709 and tj_devel709 authored Jun 2, 2022
1 parent 9822cbc commit 0307004
Show file tree
Hide file tree
Showing 32 changed files with 178 additions and 137 deletions.
41 changes: 29 additions & 12 deletions src/CoreGraphics/CGAffineTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#nullable enable

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -88,13 +90,16 @@ public struct CGAffineTransform {
[Obsolete ("Use 'Ty' instead.")]
public /* CGFloat */ nfloat y0; // ty

#pragma warning disable CS0618 // Type or member is obsolete
public /* CGFloat */ nfloat A { get => xx; set => xx = value; }
public /* CGFloat */ nfloat B { get => yx; set => yx = value; }
public /* CGFloat */ nfloat C { get => xy; set => xy = value; }
public /* CGFloat */ nfloat D { get => yy; set => yy = value; }
public /* CGFloat */ nfloat Tx { get => x0; set => x0 = value; }
public /* CGFloat */ nfloat Ty { get => y0; set => y0 = value; }
#endif
#pragma warning restore CS0618 // Type or member is obsolete

#endif // NET

#if !COREBUILD
//
Expand All @@ -113,15 +118,17 @@ public CGAffineTransform (nfloat a, nfloat b, nfloat c, nfloat d, nfloat tx, nfl
#else
public CGAffineTransform (nfloat xx, nfloat yx, nfloat xy, nfloat yy, nfloat x0, nfloat y0)
{
#pragma warning disable CS0618 // Type or member is obsolete
this.xx = xx;
this.yx = yx;
this.xy = xy;
this.yy = yy;
this.x0 = x0;
this.y0 = y0;
#pragma warning restore CS0618 // Type or member is obsolete
}
#endif
#endif // NET

// Identity
public static CGAffineTransform MakeIdentity ()
{
Expand Down Expand Up @@ -161,13 +168,15 @@ public static CGAffineTransform Multiply (CGAffineTransform a, CGAffineTransform
a.Tx * b.A + a.Ty * b.C + b.Tx,
a.Tx * b.B + a.Ty * b.D + b.Ty);
#else
#pragma warning disable CS0618 // Type or member is obsolete
return new CGAffineTransform (a.xx * b.xx + a.yx * b.xy,
a.xx * b.yx + a.yx * b.yy,
a.xy * b.xx + a.yy * b.xy,
a.xy * b.yx + a.yy * b.yy,
a.x0 * b.xx + a.y0 * b.xy + b.x0,
a.x0 * b.yx + a.y0 * b.yy + b.y0);
#endif
#pragma warning restore CS0618 // Type or member is obsolete
#endif // NET
}

public void Multiply (CGAffineTransform b)
Expand All @@ -181,13 +190,16 @@ public void Multiply (CGAffineTransform b)
Tx = a.Tx * b.A + a.Ty * b.C + b.Tx;
Ty = a.Tx * b.B + a.Ty * b.D + b.Ty;
#else
#pragma warning disable CS0618 // Type or member is obsolete
xx = a.xx * b.xx + a.yx * b.xy;
yx = a.xx * b.yx + a.yx * b.yy;
xy = a.xy * b.xx + a.yy * b.xy;
yy = a.xy * b.yx + a.yy * b.yy;
x0 = a.x0 * b.xx + a.y0 * b.xy + b.x0;
y0 = a.x0 * b.yx + a.y0 * b.yy + b.y0;
#endif
#pragma warning restore CS0618 // Type or member is obsolete

#endif // NET
}

public void Scale (nfloat sx, nfloat sy, MatrixOrder order)
Expand Down Expand Up @@ -221,14 +233,16 @@ public static CGAffineTransform Scale (CGAffineTransform transform, nfloat sx, n
transform.Tx,
transform.Ty);
#else
#pragma warning disable CS0618 // Type or member is obsolete
return new CGAffineTransform (
sx * transform.xx,
sx * transform.yx,
sy * transform.xy,
sy * transform.yy,
transform.x0,
transform.y0);
#endif
#pragma warning restore CS0618 // Type or member is obsolete
#endif // NET
}

public void Translate (nfloat tx, nfloat ty, MatrixOrder order)
Expand Down Expand Up @@ -262,14 +276,17 @@ public static CGAffineTransform Translate (CGAffineTransform transform, nfloat t
tx * transform.A + ty * transform.C + transform.Tx,
tx * transform.B + ty * transform.D + transform.Ty);
#else
#pragma warning disable CS0618 // Type or member is obsolete
return new CGAffineTransform (
transform.xx,
transform.yx,
transform.xy,
transform.yy,
tx * transform.xx + ty * transform.xy + transform.x0,
tx * transform.yx + ty * transform.yy + transform.y0);
#endif
#pragma warning disable CS0618 // Type or member is obsolete

#endif // NET
}

public void Rotate (nfloat angle, MatrixOrder order)
Expand Down Expand Up @@ -335,16 +352,16 @@ public bool IsIdentity {
extern static /* NSString */ IntPtr NSStringFromCGAffineTransform (CGAffineTransform transform);
#endif

public override String ToString ()
public override String? ToString ()
{
#if NET
#if MONOMAC
String s = $"[{A}, {B}, {C}, {D}, {Tx}, {Ty}]";
var s = $"[{A}, {B}, {C}, {D}, {Tx}, {Ty}]";
#else
String s = CFString.FromHandle (NSStringFromCGAffineTransform (this));
var s = CFString.FromHandle (NSStringFromCGAffineTransform (this));
#endif
#else
String s = String.Format ("xx:{0:##0.0#} yx:{1:##0.0#} xy:{2:##0.0#} yy:{3:##0.0#} x0:{4:##0.0#} y0:{5:##0.0#}", xx, yx, xy, yy, x0, y0);
var s = String.Format ("xx:{0:##0.0#} yx:{1:##0.0#} xy:{2:##0.0#} yy:{3:##0.0#} x0:{4:##0.0#} y0:{5:##0.0#}", xx, yx, xy, yy, x0, y0);
#endif
return s;
}
Expand Down Expand Up @@ -386,7 +403,7 @@ public override String ToString ()
#endif
}

public override bool Equals(object o)
public override bool Equals(object? o)
{
if (o is CGAffineTransform transform) {
return this == transform;
Expand Down
4 changes: 2 additions & 2 deletions src/CoreGraphics/CGBitmapContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extern static IntPtr CGBitmapContextCreate (/* void* */ byte []? data, /* size_t
static IntPtr Create (byte []? data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace? colorSpace, CGImageAlphaInfo bitmapInfo, out GCHandle buffer)
{
buffer = default (GCHandle);
if (data != null)
if (data is not null)
buffer = GCHandle.Alloc (data, GCHandleType.Pinned); // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns.
return CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle (), (uint) bitmapInfo);
}
Expand All @@ -95,7 +95,7 @@ public CGBitmapContext (byte []? data, nint width, nint height, nint bitsPerComp
static IntPtr Create (byte []? data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace? colorSpace, CGBitmapFlags bitmapInfo, out GCHandle buffer)
{
buffer = default (GCHandle);
if (data != null)
if (data is not null)
buffer = GCHandle.Alloc (data, GCHandleType.Pinned); // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns.
return CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle (), (uint) bitmapInfo);
}
Expand Down
6 changes: 3 additions & 3 deletions src/CoreGraphics/CGColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public CGColor (nfloat red, nfloat green, nfloat blue)
static IntPtr Create (string name)
{
if (name is null)
throw new ArgumentNullException (nameof (name));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (name));

var nameHandle = CFString.CreateNative (name);
try {
Expand All @@ -143,7 +143,7 @@ static IntPtr Create (CGConstantColor color)
{
var constant = color.GetConstant ();
if (constant is null)
throw new ArgumentNullException (nameof (color));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (color));
var handle = CGColorGetConstantColor (constant.Handle);
if (handle == IntPtr.Zero)
throw new ArgumentException (nameof (color));
Expand Down Expand Up @@ -194,7 +194,7 @@ public CGColor (CGColorSpace colorspace, CGPattern pattern, nfloat [] components
static IntPtr Create (CGColor source, nfloat alpha)
{
if (source is null)
throw new ArgumentNullException (nameof (source));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (source));
return CGColorCreateCopyWithAlpha (source.GetCheckedHandle (), alpha);
}

Expand Down
10 changes: 5 additions & 5 deletions src/CoreGraphics/CGColorConversionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static IntPtr Create (NSDictionary? options, params GColorConversionInfoTriple [
// the API won't return a valid instance if no triple is given, i.e. at least one is needed.
// `null` is accepted to mark the end of the list, not to make it optional
if ((triples is null) || (triples.Length == 0))
throw new ArgumentNullException (nameof (triples));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (triples));
if (triples.Length > 3)
throw new ArgumentException ("A maximum of 3 triples are supported");

Expand Down Expand Up @@ -150,9 +150,9 @@ static IntPtr Create (CGColorSpace source, CGColorSpace destination)
{
// API accept null arguments but returns null, which we can't use
if (source is null)
throw new ArgumentNullException (nameof (source));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (source));
if (destination is null)
throw new ArgumentNullException (nameof (destination));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (destination));
return CGColorConversionInfoCreate (source.Handle, destination.Handle);
}

Expand All @@ -178,9 +178,9 @@ public CGColorConversionInfo (CGColorSpace source, CGColorSpace destination)
static IntPtr Create (CGColorSpace source, CGColorSpace destination, NSDictionary? options)
{
if (source is null)
throw new ArgumentNullException (nameof (source));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (source));
if (destination is null)
throw new ArgumentNullException (nameof (destination));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (destination));

return CGColorConversionInfoCreateWithOptions (source.Handle, destination.Handle, options.GetHandle ());
}
Expand Down
10 changes: 5 additions & 5 deletions src/CoreGraphics/CGColorSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static CGColorSpace CreateNull ()
static IntPtr Create (CFPropertyList propertyList)
{
if (propertyList is null)
throw new ArgumentNullException (nameof (propertyList));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (propertyList));
return CGColorSpaceCreateWithPropertyList (propertyList.GetCheckedHandle ());
}

Expand Down Expand Up @@ -186,7 +186,7 @@ public static CGColorSpace CreateDeviceRGB ()
public static CGColorSpace? CreateCalibratedGray (nfloat [] whitepoint, nfloat []? blackpoint, nfloat gamma)
{
if (whitepoint is null)
throw new ArgumentNullException (nameof (whitepoint));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (whitepoint));
if (whitepoint.Length != 3)
throw new ArgumentException ("Must have exactly 3 values", nameof (whitepoint));
if (blackpoint is not null && blackpoint.Length != 3)
Expand All @@ -203,7 +203,7 @@ public static CGColorSpace CreateDeviceRGB ()
public static CGColorSpace? CreateCalibratedRGB (nfloat [] whitepoint, nfloat []? blackpoint, nfloat []? gamma, nfloat []? matrix)
{
if (whitepoint is null)
throw new ArgumentNullException (nameof (whitepoint));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (whitepoint));
if (whitepoint.Length != 3)
throw new ArgumentException ("Must have exactly 3 values", nameof (whitepoint));
if (blackpoint is not null && blackpoint.Length != 3)
Expand All @@ -224,7 +224,7 @@ public static CGColorSpace CreateDeviceRGB ()
public static CGColorSpace? CreateLab (nfloat [] whitepoint, nfloat []? blackpoint, nfloat []? range)
{
if (whitepoint is null)
throw new ArgumentNullException (nameof (whitepoint));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (whitepoint));
if (whitepoint.Length != 3)
throw new ArgumentException ("Must have exactly 3 values", nameof (whitepoint));
if (blackpoint is not null && blackpoint.Length != 3)
Expand Down Expand Up @@ -262,7 +262,7 @@ public static CGColorSpace CreateDeviceRGB ()
public static CGColorSpace? CreateWithName (string name)
{
if (name is null)
throw new ArgumentNullException (nameof (name));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (name));
using (var ns = new NSString (name)) {
var cs = CGColorSpaceCreateWithName (ns.Handle);
return FromHandle (cs, true);
Expand Down
14 changes: 7 additions & 7 deletions src/CoreGraphics/CGContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void AddRect (CGRect rect)
public void AddRects (CGRect [] rects)
{
if (rects is null)
throw new ArgumentNullException (nameof (rects));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (rects));
CGContextAddRects (Handle, rects, rects.Length);
}

Expand All @@ -285,7 +285,7 @@ public void AddRects (CGRect [] rects)
public void AddLines (CGPoint [] points)
{
if (points is null)
throw new ArgumentNullException (nameof (points));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (points));
CGContextAddLines (Handle, points, points.Length);
}

Expand Down Expand Up @@ -319,7 +319,7 @@ public void AddArcToPoint (nfloat x1, nfloat y1, nfloat x2, nfloat y2, nfloat ra
public void AddPath (CGPath path)
{
if (path is null)
throw new ArgumentNullException (nameof (path));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (path));
CGContextAddPath (Handle, path.Handle);
}

Expand Down Expand Up @@ -412,7 +412,7 @@ public void FillRect (CGRect rect)
public void ContextFillRects (CGRect [] rects)
{
if (rects is null)
throw new ArgumentNullException (nameof (rects));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (rects));
CGContextFillRects (Handle, rects, rects.Length);
}

Expand Down Expand Up @@ -543,7 +543,7 @@ public void ClipToRect (CGRect rect)
public void ClipToRects (CGRect [] rects)
{
if (rects is null)
throw new ArgumentNullException (nameof (rects));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (rects));
CGContextClipToRects (Handle, rects, rects.Length);
}

Expand Down Expand Up @@ -1411,7 +1411,7 @@ public CGRect ConvertRectToUserSpace (CGRect rect)
public void DrawLayer (CGLayer layer, CGRect rect)
{
if (layer is null)
throw new ArgumentNullException (nameof (layer));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (layer));
CGContextDrawLayerInRect (Handle, rect, layer.Handle);
}

Expand All @@ -1421,7 +1421,7 @@ public void DrawLayer (CGLayer layer, CGRect rect)
public void DrawLayer (CGLayer layer, CGPoint point)
{
if (layer is null)
throw new ArgumentNullException (nameof (layer));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (layer));
CGContextDrawLayerAtPoint (Handle, point, layer.Handle);
}

Expand Down
6 changes: 3 additions & 3 deletions src/CoreGraphics/CGContextPDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void AddDocumentMetadata (NSData data)
public void SetUrl (NSUrl url, CGRect region)
{
if (url is null)
throw new ArgumentNullException (nameof (url));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (url));
CGPDFContextSetURLForRect (Handle, url.Handle, region);
}

Expand All @@ -280,7 +280,7 @@ public void SetUrl (NSUrl url, CGRect region)
public void AddDestination (string name, CGPoint point)
{
if (name is null)
throw new ArgumentNullException (nameof (name));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (name));

var nameHandle = CFString.CreateNative (name);
try {
Expand All @@ -296,7 +296,7 @@ public void AddDestination (string name, CGPoint point)
public void SetDestination (string name, CGRect rect)
{
if (name is null)
throw new ArgumentNullException (nameof (name));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (name));

var nameHandle = CFString.CreateNative (name);
try {
Expand Down
4 changes: 2 additions & 2 deletions src/CoreGraphics/CGDataConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static IntPtr Create (NSMutableData data)
{
// not it's a __nullable parameter but it would return nil (see unit tests) and create an invalid instance
if (data is null)
throw new ArgumentNullException (nameof (data));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (data));
return CGDataConsumerCreateWithCFData (data.Handle);
}

Expand All @@ -95,7 +95,7 @@ static IntPtr Create (NSUrl url)
{
// not it's a __nullable parameter but it would return nil (see unit tests) and create an invalid instance
if (url is null)
throw new ArgumentNullException (nameof (url));
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (url));
return CGDataConsumerCreateWithURL (url.Handle);
}

Expand Down
Loading

5 comments on commit 0307004

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS Mac Catalina (10.15) passed 💻

All tests on macOS Mac Catalina (10.15) passed.

Pipeline on Agent
Hash: 030700470b3141fbfe0268f569172f7878ad087b

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 [CI Build] API Diff 📋

API diff (for current PR)

ℹ️ API Diff (from PR only) (please review changes)

API diff: vsdrops gist

Xamarin
.NET
Xamarin vs .NET
iOS vs Mac Catalyst (.NET)

API diff (vs stable)

✅ API Diff from stable

API diff: vsdrops gist

Xamarin
.NET
Xamarin vs .NET
iOS vs Mac Catalyst (.NET)

Generator diff

Generator Diff (no change)

Pipeline on Agent XAMMINI-050.Monterey'
Hash: 030700470b3141fbfe0268f569172f7878ad087b

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ [CI Build] Tests on macOS M1 - Mac Big Sur (11.5) failed ❌

Failed tests are:

  • xammac_tests
  • monotouch-test

Pipeline on Agent
Hash: 030700470b3141fbfe0268f569172f7878ad087b

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📚 [CI Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent XAMMINI-067.Monterey
Hash: 030700470b3141fbfe0268f569172f7878ad087b

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ [CI Build] Tests failed on VSTS: simulator tests iOS ❌

Tests failed on VSTS: simulator tests iOS.

Test results

6 tests failed, 228 tests passed.

Failed tests

  • [NUnit] Mono SystemCoreTests/watchOS 32-bits - simulator/Debug: Crashed
  • [NUnit] Mono SystemXmlTests/watchOS 32-bits - simulator/Debug: Crashed
  • [NUnit] Mono SystemWebServicesTests/watchOS 32-bits - simulator/Debug: Crashed
  • mscorlib Part 2/watchOS 32-bits - simulator/Debug: Crashed
  • [xUnit] Mono SystemCoreXunit Part 1/watchOS 32-bits - simulator/Debug: Crashed
  • [xUnit] Mono SystemXunit/watchOS 32-bits - simulator/Debug: Crashed

Pipeline on Agent XAMBOT-1101.Monterey'
[coregraphics] Add nullability to (generated and manual) bindings (#15067)

Please sign in to comment.