Skip to content

Commit

Permalink
[.NET] Move the nfloat type to the ObjCRuntime namespace for .NET.
Browse files Browse the repository at this point in the history
Also move the NMath type from the System namespace to the ObjCRuntime namespace.
  • Loading branch information
rolfbjarne committed Oct 22, 2021
1 parent 91e3537 commit bd61159
Show file tree
Hide file tree
Showing 26 changed files with 136 additions and 22 deletions.
21 changes: 21 additions & 0 deletions dotnet/BreakingChanges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Breaking changes in .NET

## System.nfloat moved to ObjCRuntime.nfloat

The `nfloat` type moved from the `System` namespace to the `ObjCRuntime` namespace.

* Code that references the `nfloat` type might not compile unless the `ObjCRuntime` namespace is imported.

Fix: add `using ObjCRuntime` to the file in question.

* Code that references the full typename, `System.nfloat` won't compile.

Fix: use `ObjCRuntime.nfloat` instead.

## System.NMath moved to ObjCRuntime.NMath

The `NMath` type moved from the `System` namespace to the `ObjCRuntime` namespace.

* Code that uses the `NMath` type won't compile unless the `ObjCRuntime` namespace is imported.

Fix: add `using ObjCRuntime` to the file in question.
4 changes: 4 additions & 0 deletions runtime/trampolines.m
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,11 @@
func = to_managed ? (void *) xamarin_nsnumber_to_nint : (void *) xamarin_nint_to_nsnumber;
} else if (!strcmp (fullname, "System.nuint")) {
func = to_managed ? (void *) xamarin_nsnumber_to_nuint : (void *) xamarin_nuint_to_nsnumber;
#if DOTNET
} else if (!strcmp (fullname, "ObjCRuntime.nfloat")) {
#else
} else if (!strcmp (fullname, "System.nfloat")) {
#endif
func = to_managed ? (void *) xamarin_nsnumber_to_nfloat : (void *) xamarin_nfloat_to_nsnumber;
} else if (mono_class_is_enum (managedType)) {
MonoType *baseType = mono_class_enum_basetype (managedType);
Expand Down
3 changes: 3 additions & 0 deletions src/AppKit/Defs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
//
using System;
using System.Runtime.InteropServices;

using ObjCRuntime;

namespace AppKit {
[StructLayout (LayoutKind.Sequential)]
public struct NSEdgeInsets {
Expand Down
2 changes: 2 additions & 0 deletions src/AppKit/NSEvent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#if !__MACCATALYST__
using System;
using System.Diagnostics;

using Foundation;
using CoreGraphics;
using ObjCRuntime;

namespace AppKit {

Expand Down
1 change: 1 addition & 0 deletions src/Foundation/NSNumber.mac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#if MONOMAC

using System;
using ObjCRuntime;

namespace Foundation
{
Expand Down
5 changes: 5 additions & 0 deletions src/NativeTypes/NMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
// Copyright 2014 Xamarin Inc. All rights reserved.
//

using System;
using System.Runtime.CompilerServices;

#if NET
namespace ObjCRuntime
#else
namespace System
#endif
{
public static class NMath
{
Expand Down
16 changes: 13 additions & 3 deletions src/NativeTypes/Primitives.tt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
// Copyright 2013 Xamarin, Inc. All rights reserved.
//

#if NET
#define OBJCRUNTIME_nfloat
#endif

<#@ template language="C#v3.5" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
Expand All @@ -32,8 +36,8 @@ using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;

namespace System
{
using ObjCRuntime;

<#
foreach (var type in new [] {
new { NSName = "nint", CilName32 = "Int32", CilName64 = "Int64", IsIntegerType = true },
Expand All @@ -56,6 +60,12 @@ namespace System
binops.Add ("^");
}
#>
#if OBJCRUNTIME_<#= type.NSName #>
namespace ObjCRuntime
#else
namespace System
#endif
{
[Serializable]
[DebuggerDisplay ("{v,nq}")]
public unsafe struct <#= type.NSName #> : IFormattable, IConvertible, IComparable, IComparable<<#= type.NSName #>>, IEquatable <<#= type.NSName #>>
Expand Down Expand Up @@ -427,5 +437,5 @@ namespace System
Marshal.WriteIntPtr (destination, i * <#= type.NSName #>.Size, (IntPtr)source [i + startIndex]);
}
}
<# } #>
}
<# } #>
16 changes: 13 additions & 3 deletions src/ObjCRuntime/Registrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ abstract partial class Registrar {
public Application App { get; protected set; }
#endif

#if MMP || MTOUCH || BUNDLER
static string NFloatTypeName { get => Driver.IsDotNet ? "ObjCRuntime.nfloat" : "System.nfloat"; }
#elif NET
const string NFloatTypeName = "ObjCRuntime.nfloat";
#else
const string NFloatTypeName = "System.nfloat";
#endif

Dictionary<TAssembly, object> assemblies = new Dictionary<TAssembly, object> (); // Use Dictionary instead of HashSet to avoid pulling in System.Core.dll.
// locking: all accesses must lock 'types'.
Dictionary<TType, ObjCType> types = new Dictionary<TType, ObjCType> ();
Expand Down Expand Up @@ -715,10 +723,11 @@ bool IsValidToManagedTypeConversion (TType inputType, TType outputType)
case "System.nuint":
case "System.Single":
case "System.Double":
case "System.nfloat":
case "System.Boolean":
return true;
default:
if (outputTypeName == NFloatTypeName)
return true;
return Registrar.IsEnum (underlyingOutputType);
}
} else if (Registrar.Is (underlyingInputType, Foundation, "NSValue")) {
Expand Down Expand Up @@ -2641,12 +2650,13 @@ protected string ToSignature (TType type, ObjCMember member, ref bool success, b
return Is64Bits ? "q" : "i";
case "System.nuint":
return Is64Bits ? "Q" : "I";
case "System.nfloat":
return Is64Bits ? "d" : "f";
case "System.DateTime":
throw CreateException (4102, member, Errors.MT4102, "System.DateTime", "Foundation.NSDate", member.FullName);
}

if (typeFullName == NFloatTypeName)
return Is64Bits ? "d" : "f";

if (Is (type, ObjCRuntime, "Selector"))
return ":";

Expand Down
9 changes: 9 additions & 0 deletions src/ObjCRuntime/Stret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ static bool IsBuiltInType (Type type, bool is_64_bits, out int type_size)
if (type.IsNested)
return false;

#if NET
if (type.Namespace == "ObjCRuntime" && type.Name == "nfloat") {
type_size = is_64_bits ? 8 : 4;
return true;
}
#endif

if (type.Namespace != "System")
return false;

Expand All @@ -251,7 +258,9 @@ static bool IsBuiltInType (Type type, bool is_64_bits, out int type_size)
type_size = 8;
return true;
case "IntPtr":
#if !NET
case "nfloat":
#endif
case "nuint":
case "nint":
type_size = is_64_bits ? 8 : 4;
Expand Down
2 changes: 2 additions & 0 deletions src/PdfKit/PdfCompat.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#if !XAMCORE_4_0
using System;

using Foundation;
using ObjCRuntime;

#nullable enable

Expand Down
1 change: 1 addition & 0 deletions src/PdfKit/PdfKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using CoreGraphics;
using Foundation;
using ObjCRuntime;

#nullable enable

Expand Down
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ We have introduced 6 new types to make this possible:

In the Classic assembly, the `System.Drawing` types are backed by the 32-bit
`System.Single` type. In the Unified assemblies, the `CoreGraphics` types
are backed by 32/64-bit `System.nfloat` type.
are backed by 32/64-bit `System.nfloat` type (`ObjCRuntime.nfloat` in .NET).

#### Enums ####

Expand Down
2 changes: 2 additions & 0 deletions src/SceneKit/Constructors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
// Copyright 2012 Xamarin Inc
//
using System;

using Foundation;
using ObjCRuntime;

#nullable enable

Expand Down
4 changes: 4 additions & 0 deletions src/SceneKit/SCNMatrix4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using Quaternion = global::OpenTK.Quaternion;
using Quaterniond = global::OpenTK.Quaterniond;
#if MONOMAC
#if NET
using pfloat = ObjCRuntime.nfloat;
#else
using pfloat = System.nfloat;
#endif
#else
using pfloat = System.Single;
#endif
Expand Down
1 change: 1 addition & 0 deletions src/SceneKit/SCNNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using CoreAnimation;
#endif
using Foundation;
using ObjCRuntime;

#nullable enable

Expand Down
4 changes: 4 additions & 0 deletions src/SceneKit/SCNQuaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using MathHelper = global::OpenTK.MathHelper;

#if MONOMAC
#if NET
using pfloat = ObjCRuntime.nfloat;
#else
using pfloat = System.nfloat;
#endif
#else
using pfloat = System.Single;
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/SceneKit/SCNVector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using Vector3 = global::OpenTK.Vector3;
using MathHelper = global::OpenTK.MathHelper;
#if MONOMAC
#if NET
using pfloat = ObjCRuntime.nfloat;
#else
using pfloat = System.nfloat;
#endif
#else
using pfloat = System.Single;
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/SceneKit/SCNVector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using Quaternion = global::OpenTK.Quaternion;
using MathHelper = global::OpenTK.MathHelper;
#if MONOMAC
#if NET
using pfloat = ObjCRuntime.nfloat;
#else
using pfloat = System.nfloat;
#endif
#else
using pfloat = System.Single;
#endif
Expand Down
1 change: 1 addition & 0 deletions src/UIKit/Compat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using CoreGraphics;
using Foundation;
using ObjCRuntime;

namespace UIKit {

Expand Down
10 changes: 5 additions & 5 deletions src/UIKit/UIStringDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static CGSize DrawString (this string This, CGPoint point, UIFont font)
[UnsupportedOSPlatform ("ios7.0")]
[Obsolete ("Starting with ios7.0 use NSString.DrawString(CGRect, UIStringAttributes) instead.", DiagnosticId = "BI1234", UrlFormat = "https://github.com/xamarin/xamarin-macios/wiki/Obsolete")]
#endif
public static CGSize DrawString (this string This, CGPoint point, global::System.nfloat width, UIFont font, UILineBreakMode breakMode)
public static CGSize DrawString (this string This, CGPoint point, nfloat width, UIFont font, UILineBreakMode breakMode)
{
using (var self = ((NSString) This))
return self.DrawString (point, width, font, breakMode);
Expand All @@ -40,7 +40,7 @@ public static CGSize DrawString (this string This, CGPoint point, global::System
[UnsupportedOSPlatform ("ios7.0")]
[Obsolete ("Starting with ios7.0 use NSString.DrawString(CGRect, UIStringAttributes) instead.", DiagnosticId = "BI1234", UrlFormat = "https://github.com/xamarin/xamarin-macios/wiki/Obsolete")]
#endif
public static CGSize DrawString (this string This, CGPoint point, global::System.nfloat width, UIFont font, global::System.nfloat fontSize, UILineBreakMode breakMode, UIBaselineAdjustment adjustment)
public static CGSize DrawString (this string This, CGPoint point, nfloat width, UIFont font, nfloat fontSize, UILineBreakMode breakMode, UIBaselineAdjustment adjustment)
{
using (var self = ((NSString) This))
return self.DrawString (point, width, font, fontSize, breakMode, adjustment);
Expand All @@ -52,7 +52,7 @@ public static CGSize DrawString (this string This, CGPoint point, global::System
[UnsupportedOSPlatform ("ios7.0")]
[Obsolete ("Starting with ios7.0 use NSString.DrawString(CGRect, UIStringAttributes) instead.", DiagnosticId = "BI1234", UrlFormat = "https://github.com/xamarin/xamarin-macios/wiki/Obsolete")]
#endif
public static CGSize DrawString (this string This, CGPoint point, global::System.nfloat width, UIFont font, global::System.nfloat minFontSize, ref global::System.nfloat actualFontSize, UILineBreakMode breakMode, UIBaselineAdjustment adjustment)
public static CGSize DrawString (this string This, CGPoint point, nfloat width, UIFont font, nfloat minFontSize, ref nfloat actualFontSize, UILineBreakMode breakMode, UIBaselineAdjustment adjustment)
{
using (var self = ((NSString) This))
return self.DrawString (point, width, font, minFontSize, ref actualFontSize, breakMode, adjustment);
Expand Down Expand Up @@ -112,7 +112,7 @@ public static CGSize StringSize (this string This, UIFont font)
[UnsupportedOSPlatform ("ios7.0")]
[Obsolete ("Starting with ios7.0 use NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext) instead.", DiagnosticId = "BI1234", UrlFormat = "https://github.com/xamarin/xamarin-macios/wiki/Obsolete")]
#endif
public static CGSize StringSize (this string This, UIFont font, global::System.nfloat forWidth, UILineBreakMode breakMode)
public static CGSize StringSize (this string This, UIFont font, nfloat forWidth, UILineBreakMode breakMode)
{
using (var self = ((NSString) This))
return self.StringSize (font, forWidth, breakMode);
Expand Down Expand Up @@ -147,7 +147,7 @@ public static CGSize StringSize (this string This, UIFont font, CGSize constrain
#else
[UnsupportedOSPlatform ("ios7.0")]
#endif
public static CGSize StringSize (this string This, UIFont font, global::System.nfloat minFontSize, ref global::System.nfloat actualFontSize, global::System.nfloat forWidth, UILineBreakMode lineBreakMode)
public static CGSize StringSize (this string This, UIFont font, nfloat minFontSize, ref nfloat actualFontSize, nfloat forWidth, UILineBreakMode lineBreakMode)
{
using (var self = ((NSString) This))
return self.StringSize (font, minFontSize, ref actualFontSize, forWidth, lineBreakMode);
Expand Down
1 change: 1 addition & 0 deletions src/WKWebKit/WKWindowFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;

using Foundation;
using ObjCRuntime;

#nullable enable

Expand Down
4 changes: 4 additions & 0 deletions src/generator-typemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ public void Initialize (BindingTouch binding_touch, Assembly api, Assembly corli

System_nint = Lookup (platform_assembly, "System", "nint");
System_nuint = Lookup (platform_assembly, "System", "nuint");
#if NET
System_nfloat = Lookup (platform_assembly, "ObjCRuntime", "nfloat");
#else
System_nfloat = Lookup (platform_assembly, "System", "nfloat");
#endif

/* fundamental */
NSObject = Lookup (platform_assembly, "Foundation", "NSObject");
Expand Down
4 changes: 4 additions & 0 deletions src/glkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
using MathHelper = global::OpenTK.MathHelper;

#if MONOMAC
#if NET
using pfloat = ObjCRuntime.nfloat;
#else
using pfloat = System.nfloat;
#endif
using AppKit;
#else
using OpenGLES;
Expand Down
4 changes: 4 additions & 0 deletions src/spritekit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
using UIColor = global::AppKit.NSColor;
using UIImage = global::AppKit.NSImage;
using UIView = global::AppKit.NSView;
#if NET
using pfloat = ObjCRuntime.nfloat;
#else
using pfloat = System.nfloat;
#endif
#else
using UIKit;
using NSLineBreakMode = global::UIKit.UILineBreakMode;
Expand Down
Loading

0 comments on commit bd61159

Please sign in to comment.