some small tweaks, mostly subjective
This commit is contained in:
parent
e1c328f647
commit
31632abc24
2 changed files with 20 additions and 24 deletions
|
@ -3,34 +3,30 @@ const objc = @import("main.zig");
|
||||||
|
|
||||||
// From <Foundation/NSEnumerator.h>.
|
// From <Foundation/NSEnumerator.h>.
|
||||||
const NSFastEnumerationState = extern struct {
|
const NSFastEnumerationState = extern struct {
|
||||||
state: c_ulong,
|
state: c_ulong = 0,
|
||||||
itemsPtr: ?[*]objc.c.id,
|
itemsPtr: ?[*]objc.c.id = null,
|
||||||
mutationsPtr: ?*c_ulong,
|
mutationsPtr: ?*c_ulong = null,
|
||||||
extra: [5]c_ulong,
|
extra: [5]c_ulong = [_]c_ulong{0} ** 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// An iterator that uses the fast enumeration protocol[1] to iterate over
|
||||||
|
/// objects in an Objective-C collection. This can be used with any object
|
||||||
|
/// that conforms to the `NSFastEnumeration` protocol.
|
||||||
|
///
|
||||||
|
/// [1]: Nhttps://developer.apple.com/documentation/foundation/nsfastenumeration
|
||||||
pub const Iterator = struct {
|
pub const Iterator = struct {
|
||||||
object: objc.Object,
|
object: objc.Object,
|
||||||
sel: objc.Sel,
|
sel: objc.Sel,
|
||||||
state: NSFastEnumerationState,
|
state: NSFastEnumerationState = .{},
|
||||||
initial_mutations_value: ?c_ulong,
|
initial_mutations_value: ?c_ulong = null,
|
||||||
// Clang compiles `for…in` loops with a size 16 buffer.
|
// Clang compiles `for…in` loops with a size 16 buffer.
|
||||||
buffer: [16]objc.c.id,
|
buffer: [16]objc.c.id = [_]objc.c.id{null} ** 16,
|
||||||
slice: []objc.c.id,
|
slice: []const objc.c.id = &.{},
|
||||||
|
|
||||||
pub fn init(object: objc.Object) Iterator {
|
pub fn init(object: objc.Object) Iterator {
|
||||||
return .{
|
return .{
|
||||||
.object = object,
|
.object = object,
|
||||||
.sel = objc.sel("countByEnumeratingWithState:objects:count:"),
|
.sel = objc.sel("countByEnumeratingWithState:objects:count:"),
|
||||||
.state = .{
|
|
||||||
.state = 0,
|
|
||||||
.itemsPtr = null,
|
|
||||||
.mutationsPtr = null,
|
|
||||||
.extra = [_]c_ulong{0} ** 5,
|
|
||||||
},
|
|
||||||
.initial_mutations_value = null,
|
|
||||||
.buffer = [_]objc.c.id{null} ** 16,
|
|
||||||
.slice = &[_]objc.c.id{},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,13 +50,11 @@ pub const Iterator = struct {
|
||||||
self.slice = self.state.itemsPtr.?[0..count];
|
self.slice = self.state.itemsPtr.?[0..count];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.slice.len > 0) {
|
if (self.slice.len == 0) return null;
|
||||||
|
|
||||||
const first = self.slice[0];
|
const first = self.slice[0];
|
||||||
self.slice = self.slice[1..];
|
self.slice = self.slice[1..];
|
||||||
return objc.Object.fromId(first);
|
return objc.Object.fromId(first);
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -106,6 +106,8 @@ pub const Object = struct {
|
||||||
objc_release(self.value);
|
objc_release(self.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return an iterator for this object. The object must implement the
|
||||||
|
/// `NSFastEnumeration` protocol.
|
||||||
pub fn iterate(self: Object) Iterator {
|
pub fn iterate(self: Object) Iterator {
|
||||||
return Iterator.init(self);
|
return Iterator.init(self);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue