Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coretext] Add nullability to (generated and manual) bindings #14978

Merged
merged 19 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 22 additions & 20 deletions src/CoreText/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#nullable enable

using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -65,15 +67,15 @@ public static void AssertWritable (NSDictionary dictionary)
public static int? GetInt32Value (IDictionary<NSObject, NSObject> dictionary, NSObject key)
{
var value = dictionary [key];
if (value == null)
if (value is null)
return null;
return ((NSNumber) value).Int32Value;
}

public static nuint? GetUnsignedIntegerValue (IDictionary<NSObject, NSObject> dictionary, NSObject key)
{
var value = dictionary [key];
if (value == null)
if (value is null)
return null;
return ((NSNumber) value).NUIntValue;
}
Expand All @@ -89,39 +91,39 @@ public static T[] GetNativeArray<T> (NSDictionary dictionary, NSObject key, Conv
public static float? GetSingleValue (IDictionary<NSObject, NSObject> dictionary, NSObject key)
{
var value = dictionary [key];
if (value == null)
if (value is null)
return null;
return ((NSNumber) value).FloatValue;
}

public static string[] GetStringArray (IDictionary<NSObject, NSObject> dictionary, NSObject key)
{
var value = dictionary [key];
if (value == null)
if (value is null)
return Array.Empty<string> ();
return CFArray.StringArrayFromHandle (value.Handle);
return CFArray.StringArrayFromHandle (value.Handle)!;
}

public static string GetStringValue (IDictionary<NSObject, NSObject> dictionary, NSObject key)
public static string? GetStringValue (IDictionary<NSObject, NSObject> dictionary, NSObject key)
{
var value = dictionary [key];
if (value == null)
if (value is null)
return null;
return ((NSString) value).ToString ();
}

public static uint? GetUInt32Value (IDictionary<NSObject, NSObject> dictionary, NSObject key)
{
var value = dictionary [key];
if (value == null)
if (value is null)
return null;
return ((NSNumber) value).UInt32Value;
}

public static bool? GetBoolValue (NSDictionary dictionary, NSObject key)
{
var value = dictionary [key];
if (value == null)
if (value is null)
return null;
return ((NSNumber) value).BoolValue;
}
Expand Down Expand Up @@ -169,25 +171,25 @@ public static void SetValue (IDictionary<NSObject, NSObject> dictionary, NSObjec
public static void SetValue (IDictionary<NSObject, NSObject> dictionary, NSObject key, IEnumerable<string> value)
{
List<string> v;
if (value == null || (v = new List<string>(value)).Count == 0)
SetValue (dictionary, key, (NSObject) null);
if (value is null || (v = new List<string>(value)).Count == 0)
SetValue (dictionary, key, (NSObject?) null);
else
using (var array = NSArray.FromStrings (v.ToArray ()))
SetValue (dictionary, key, array);
}

public static void SetValue (IDictionary<NSObject, NSObject> dictionary, NSObject key, NSObject value)
public static void SetValue (IDictionary<NSObject, NSObject> dictionary, NSObject key, NSObject? value)
{
if (value != null)
if (value is not null)
dictionary [key] = value;
else
dictionary.Remove (key);
}

public static void SetValue (IDictionary<NSObject, NSObject> dictionary, NSObject key, string value)
public static void SetValue (IDictionary<NSObject, NSObject> dictionary, NSObject key, string? value)
{
if (value == null)
SetValue (dictionary, key, (NSObject) null);
if (value is null)
SetValue (dictionary, key, (NSObject?) null);
else
using (var s = new NSString (value))
SetValue (dictionary, key, (NSObject) s);
Expand All @@ -197,8 +199,8 @@ public static void SetNativeValue<T> (NSDictionary dictionary, NSObject key, IEn
where T : INativeObject
{
List<NativeHandle> v;
if (value == null || (v = GetHandles (value)).Count == 0)
SetNativeValue (dictionary, key, (INativeObject) null);
if (value is null || (v = GetHandles (value)).Count == 0)
SetNativeValue (dictionary, key, (INativeObject?) null);
else
using (var array = CFArray.FromIntPtrs (v.ToArray ()))
SetNativeValue (dictionary, key, array);
Expand All @@ -213,9 +215,9 @@ static List<NativeHandle> GetHandles<T> (IEnumerable<T> value)
return v;
}

public static void SetNativeValue (NSDictionary dictionary, NSObject key, INativeObject value)
public static void SetNativeValue (NSDictionary dictionary, NSObject key, INativeObject? value)
{
if (value != null) {
if (value is not null) {
AssertWritable (dictionary);
CFMutableDictionary.SetValue (dictionary.Handle, key.Handle, value.Handle);
}
Expand Down
19 changes: 11 additions & 8 deletions src/CoreText/CTBaselineClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#nullable enable

using System;

using ObjCRuntime;
Expand Down Expand Up @@ -54,12 +57,12 @@ static partial class CTBaselineClassID {
static CTBaselineClassID ()
{
var handle = Libraries.CoreText.Handle;
Roman = Dlfcn.GetStringConstant (handle, "kCTBaselineClassRoman");
IdeographicCentered = Dlfcn.GetStringConstant (handle, "kCTBaselineClassIdeographicCentered");
IdeographicLow = Dlfcn.GetStringConstant (handle, "kCTBaselineClassIdeographicLow");
IdeographicHigh = Dlfcn.GetStringConstant (handle, "kCTBaselineClassIdeographicHigh");
Hanging = Dlfcn.GetStringConstant (handle, "kCTBaselineClassHanging");
Math = Dlfcn.GetStringConstant (handle, "kCTBaselineClassMath");
Roman = Dlfcn.GetStringConstant (handle, "kCTBaselineClassRoman")!;
tj-devel709 marked this conversation as resolved.
Show resolved Hide resolved
IdeographicCentered = Dlfcn.GetStringConstant (handle, "kCTBaselineClassIdeographicCentered")!;
IdeographicLow = Dlfcn.GetStringConstant (handle, "kCTBaselineClassIdeographicLow")!;
IdeographicHigh = Dlfcn.GetStringConstant (handle, "kCTBaselineClassIdeographicHigh")!;
Hanging = Dlfcn.GetStringConstant (handle, "kCTBaselineClassHanging")!;
Math = Dlfcn.GetStringConstant (handle, "kCTBaselineClassMath")!;
}
#endif

Expand Down Expand Up @@ -104,8 +107,8 @@ static partial class CTBaselineFontID {
static CTBaselineFontID ()
{
var handle = Libraries.CoreText.Handle;
Reference = Dlfcn.GetStringConstant (handle, "kCTBaselineReferenceFont");
Original = Dlfcn.GetStringConstant (handle, "kCTBaselineOriginalFont");
Reference = Dlfcn.GetStringConstant (handle, "kCTBaselineReferenceFont")!;
Original = Dlfcn.GetStringConstant (handle, "kCTBaselineOriginalFont")!;
}
#endif // !NET

Expand Down
2 changes: 2 additions & 0 deletions src/CoreText/CTFont.Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#nullable enable

using CoreFoundation;

namespace CoreText {
Expand Down
Loading