1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const base = @import("base.zig");
const c = @import("c.zig").c;
const cftype = @import("type.zig");
const ComparisonResult = base.ComparisonResult;
const Range = base.Range;
pub const Array = opaque {
pub fn create(comptime T: type, values: []*const T) Allocator.Error!*Array {
return CFArrayCreate(
null,
@ptrCast(values.ptr),
@intCast(values.len),
null,
) orelse error.OutOfMemory;
}
pub fn release(self: *Array) void {
cftype.CFRelease(self);
}
pub fn getCount(self: *Array) usize {
return CFArrayGetCount(self);
}
/// Note the return type is actually a `*const T` but we strip the
/// constness so that further API calls work correctly. The Foundation
/// API doesn't properly mark things const/non-const.
pub fn getValueAtIndex(self: *Array, comptime T: type, idx: usize) *T {
return @ptrCast(@alignCast(CFArrayGetValueAtIndex(self, idx)));
}
pub extern "c" fn CFArrayCreate(
allocator: ?*anyopaque,
values: [*]*const anyopaque,
num_values: usize,
callbacks: ?*const anyopaque,
) ?*Array;
pub extern "c" fn CFArrayGetCount(*Array) usize;
pub extern "c" fn CFArrayGetValueAtIndex(*Array, usize) *anyopaque;
extern "c" var kCFTypeArrayCallBacks: anyopaque;
};
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,
0,
array,
) orelse error.OutOfMemory;
}
pub fn release(self: *MutableArray) void {
cftype.CFRelease(self);
}
pub fn appendValue(
self: *MutableArray,
comptime Elem: type,
value: *const Elem,
) void {
CFArrayAppendValue(self, @ptrCast(@constCast(value)));
}
pub fn removeValue(self: *MutableArray, idx: usize) void {
CFArrayRemoveValueAtIndex(self, idx);
}
pub fn sortValues(
self: *MutableArray,
comptime Elem: type,
comptime Context: type,
context: ?*Context,
comptime comparator: ?*const fn (
a: *const Elem,
b: *const Elem,
context: ?*Context,
) callconv(.c) ComparisonResult,
) void {
CFArraySortValues(
self,
Range.init(0, Array.CFArrayGetCount(@ptrCast(self))),
comparator,
context,
);
}
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 CFArrayRemoveValueAtIndex(
*MutableArray,
usize,
) void;
extern "c" fn CFArraySortValues(
array: *MutableArray,
range: Range,
comparator: ?*const anyopaque,
context: ?*anyopaque,
) void;
};
test "array" {
const testing = std.testing;
const str = "hello";
var values = [_]*const u8{ &str[0], &str[1] };
const arr = try Array.create(u8, &values);
defer arr.release();
try testing.expectEqual(@as(usize, 2), arr.getCount());
{
const ch = arr.getValueAtIndex(u8, 0);
try testing.expectEqual(@as(u8, 'h'), ch.*);
}
// Can make it mutable
var mut = try MutableArray.createCopy(arr);
defer mut.release();
}
test "array sorting" {
const testing = std.testing;
const str = "hello";
var values = [_]*const u8{ &str[0], &str[1] };
const arr = try Array.create(u8, &values);
defer arr.release();
const mut = try MutableArray.createCopy(arr);
defer mut.release();
mut.sortValues(
u8,
void,
null,
struct {
fn compare(a: *const u8, b: *const u8, _: ?*void) callconv(.c) ComparisonResult {
if (a.* > b.*) return .greater;
if (a.* == b.*) return .equal;
return .less;
}
}.compare,
);
{
const mutarr: *Array = @ptrCast(mut);
const ch = mutarr.getValueAtIndex(u8, 0);
try testing.expectEqual(@as(u8, 'e'), ch.*);
}
{
const mutarr: *Array = @ptrCast(mut);
const ch = mutarr.getValueAtIndex(u8, 1);
try testing.expectEqual(@as(u8, 'h'), ch.*);
}
}
|