pkg/macos: add missing functions to macos/foundation

This commit is contained in:
Mitchell Hashimoto
2024-04-04 11:25:34 -07:00
parent 5440fda3fb
commit 290496d7fe
2 changed files with 30 additions and 5 deletions

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const base = @import("base.zig"); const base = @import("base.zig");
const c = @import("c.zig");
const cftype = @import("type.zig"); const cftype = @import("type.zig");
const ComparisonResult = base.ComparisonResult; const ComparisonResult = base.ComparisonResult;
const Range = base.Range; const Range = base.Range;
@ -42,6 +43,14 @@ pub const Array = opaque {
}; };
pub const MutableArray = 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 { pub fn createCopy(array: *Array) Allocator.Error!*MutableArray {
return CFArrayCreateMutableCopy( return CFArrayCreateMutableCopy(
null, null,
@ -54,6 +63,14 @@ pub const MutableArray = opaque {
cftype.CFRelease(self); cftype.CFRelease(self);
} }
pub fn appendValue(
self: *MutableArray,
comptime Elem: type,
value: *const Elem,
) void {
CFArrayAppendValue(self, @constCast(@ptrCast(value)));
}
pub fn sortValues( pub fn sortValues(
self: *MutableArray, self: *MutableArray,
comptime Elem: type, 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( extern "c" fn CFArrayCreateMutableCopy(
allocator: ?*anyopaque, allocator: ?*anyopaque,
capacity: usize, capacity: usize,
array: *Array, array: *Array,
) ?*MutableArray; ) ?*MutableArray;
extern "c" fn CFArrayAppendValue(
*MutableArray,
*anyopaque,
) void;
extern "c" fn CFArraySortValues( extern "c" fn CFArraySortValues(
array: *MutableArray, array: *MutableArray,
range: Range, range: Range,

View File

@ -6,8 +6,8 @@ const c = @import("c.zig");
pub const Dictionary = opaque { pub const Dictionary = opaque {
pub fn create( pub fn create(
keys: ?[]?*const anyopaque, keys: ?[]const ?*const anyopaque,
values: ?[]?*const anyopaque, values: ?[]const ?*const anyopaque,
) Allocator.Error!*Dictionary { ) Allocator.Error!*Dictionary {
if (keys != null or values != null) { if (keys != null or values != null) {
assert(keys != null); assert(keys != null);
@ -17,8 +17,8 @@ pub const Dictionary = opaque {
return @as(?*Dictionary, @ptrFromInt(@intFromPtr(c.CFDictionaryCreate( return @as(?*Dictionary, @ptrFromInt(@intFromPtr(c.CFDictionaryCreate(
null, null,
@ptrCast(if (keys) |slice| slice.ptr else null), @constCast(@ptrCast(if (keys) |slice| slice.ptr else null)),
@ptrCast(if (values) |slice| slice.ptr else null), @constCast(@ptrCast(if (values) |slice| slice.ptr else null)),
@intCast(if (keys) |slice| slice.len else 0), @intCast(if (keys) |slice| slice.len else 0),
&c.kCFTypeDictionaryKeyCallBacks, &c.kCFTypeDictionaryKeyCallBacks,
&c.kCFTypeDictionaryValueCallBacks, &c.kCFTypeDictionaryValueCallBacks,