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 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,

View File

@ -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,