From 2440b0ec67edd7b8fa380f4423bb910a5c5d64bb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 1 Oct 2022 09:53:48 -0700 Subject: [PATCH] pkg/macos: dictionary create --- pkg/macos/foundation/dictionary.zig | 41 ++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/pkg/macos/foundation/dictionary.zig b/pkg/macos/foundation/dictionary.zig index 495accd50..6d9c6bd9e 100644 --- a/pkg/macos/foundation/dictionary.zig +++ b/pkg/macos/foundation/dictionary.zig @@ -1,21 +1,35 @@ const std = @import("std"); +const assert = std.debug.assert; const Allocator = std.mem.Allocator; -const cftype = @import("type.zig"); +const foundation = @import("../foundation.zig"); pub const Dictionary = opaque { - pub fn create() Allocator.Error!*Dictionary { + pub fn create( + keys: ?[]*const anyopaque, + values: ?[]*const anyopaque, + ) Allocator.Error!*Dictionary { + if (keys != null or values != null) { + assert(keys != null); + assert(values != null); + assert(keys.?.len == values.?.len); + } + return CFDictionaryCreate( null, - null, - null, - 0, + if (keys) |slice| slice.ptr else null, + if (values) |slice| slice.ptr else null, + if (keys) |slice| slice.len else 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks, ) orelse Allocator.Error.OutOfMemory; } pub fn release(self: *Dictionary) void { - cftype.CFRelease(self); + foundation.CFRelease(self); + } + + pub fn getCount(self: *Dictionary) usize { + return CFDictionaryGetCount(self); } pub extern "c" fn CFDictionaryCreate( @@ -26,12 +40,25 @@ pub const Dictionary = opaque { key_callbacks: *const anyopaque, value_callbacks: *const anyopaque, ) ?*Dictionary; + pub extern "c" fn CFDictionaryGetCount(*Dictionary) usize; extern "c" var kCFTypeDictionaryKeyCallBacks: anyopaque; extern "c" var kCFTypeDictionaryValueCallBacks: anyopaque; }; +// Just used for a test +extern "c" var kCFURLIsPurgeableKey: *const anyopaque; + test "dictionary" { - const dict = try Dictionary.create(); + const testing = std.testing; + + const str = try foundation.String.createWithBytes("hello", .unicode, false); + defer str.release(); + + var keys = [_]*const anyopaque{kCFURLIsPurgeableKey}; + var values = [_]*const anyopaque{str}; + const dict = try Dictionary.create(&keys, &values); defer dict.release(); + + try testing.expectEqual(@as(usize, 1), dict.getCount()); }