From 290496d7fec3a01824d372d9b04dcaf405727860 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 4 Apr 2024 11:25:34 -0700 Subject: [PATCH] pkg/macos: add missing functions to macos/foundation --- pkg/macos/foundation/array.zig | 27 ++++++++++++++++++++++++++- pkg/macos/foundation/dictionary.zig | 8 ++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/pkg/macos/foundation/array.zig b/pkg/macos/foundation/array.zig index 017984642..6fb4ec143 100644 --- a/pkg/macos/foundation/array.zig +++ b/pkg/macos/foundation/array.zig @@ -1,6 +1,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const base = @import("base.zig"); +const c = @import("c.zig"); const cftype = @import("type.zig"); const ComparisonResult = base.ComparisonResult; const Range = base.Range; @@ -42,6 +43,14 @@ pub const Array = opaque { }; pub const MutableArray = opaque { + pub fn create() Allocator.Error!*MutableArray { + return CFArrayCreateMutable( + null, + 0, + &c.kCFTypeArrayCallBacks, + ) orelse error.OutOfMemory; + } + pub fn createCopy(array: *Array) Allocator.Error!*MutableArray { return CFArrayCreateMutableCopy( null, @@ -54,6 +63,14 @@ pub const MutableArray = opaque { cftype.CFRelease(self); } + pub fn appendValue( + self: *MutableArray, + comptime Elem: type, + value: *const Elem, + ) void { + CFArrayAppendValue(self, @constCast(@ptrCast(value))); + } + pub fn sortValues( self: *MutableArray, comptime Elem: type, @@ -73,12 +90,20 @@ pub const MutableArray = opaque { ); } + extern "c" fn CFArrayCreateMutable( + allocator: ?*anyopaque, + capacity: usize, + callbacks: ?*const anyopaque, + ) ?*MutableArray; extern "c" fn CFArrayCreateMutableCopy( allocator: ?*anyopaque, capacity: usize, array: *Array, ) ?*MutableArray; - + extern "c" fn CFArrayAppendValue( + *MutableArray, + *anyopaque, + ) void; extern "c" fn CFArraySortValues( array: *MutableArray, range: Range, diff --git a/pkg/macos/foundation/dictionary.zig b/pkg/macos/foundation/dictionary.zig index ebb0fcc33..499681190 100644 --- a/pkg/macos/foundation/dictionary.zig +++ b/pkg/macos/foundation/dictionary.zig @@ -6,8 +6,8 @@ const c = @import("c.zig"); pub const Dictionary = opaque { pub fn create( - keys: ?[]?*const anyopaque, - values: ?[]?*const anyopaque, + keys: ?[]const ?*const anyopaque, + values: ?[]const ?*const anyopaque, ) Allocator.Error!*Dictionary { if (keys != null or values != null) { assert(keys != null); @@ -17,8 +17,8 @@ pub const Dictionary = opaque { return @as(?*Dictionary, @ptrFromInt(@intFromPtr(c.CFDictionaryCreate( null, - @ptrCast(if (keys) |slice| slice.ptr else null), - @ptrCast(if (values) |slice| slice.ptr else null), + @constCast(@ptrCast(if (keys) |slice| slice.ptr else null)), + @constCast(@ptrCast(if (values) |slice| slice.ptr else null)), @intCast(if (keys) |slice| slice.len else 0), &c.kCFTypeDictionaryKeyCallBacks, &c.kCFTypeDictionaryValueCallBacks,