NSPointerArray: Implement NSFastEnumeration

This commit is contained in:
Hugo Melder 2024-12-12 11:21:14 +01:00
parent 4a4a802060
commit b993f10e76
2 changed files with 29 additions and 1 deletions

View file

@ -42,7 +42,7 @@ extern "C" {
* or grow (adding nil/zero items).
*/
GS_EXPORT_CLASS
@interface NSPointerArray : NSObject <NSCopying, NSCoding>
@interface NSPointerArray : NSObject <NSCoding, NSCopying, NSFastEnumeration>
/** Allocate an instance, initialise using initWithOptions: and
* return it autoreleased.

View file

@ -197,6 +197,34 @@ static Class concreteClass = Nil;
[self subclassResponsibility: _cmd];
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (__unsafe_unretained id[])stackbuf
count: (NSUInteger)len
{
NSInteger count;
state->mutationsPtr = (unsigned long *)&state->mutationsPtr;
count = MIN(len, [self count] - state->state);
if (count > 0)
{
IMP imp = [self methodForSelector: @selector(pointerAtIndex:)];
int p = state->state;
int i;
for (i = 0; i < count; i++, p++)
{
stackbuf[i] = (*imp)(self, @selector(pointerAtIndex:), p);
}
state->state += count;
}
else
{
count = 0;
}
state->itemsPtr = stackbuf;
return count;
}
@end
@implementation NSPointerArray (NSArrayConveniences)