2001-12-17 14:31:42 +00:00
|
|
|
/** NSEnumerator abstrace class for GNUStep
|
1996-02-01 17:04:17 +00:00
|
|
|
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
1995-04-09 01:28:47 +00:00
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-04-09 01:28:47 +00:00
|
|
|
Date: March 1995
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-04-09 01:28:47 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1995-04-09 01:28:47 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1995-04-09 01:28:47 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1995-04-09 01:28:47 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-20 10:56:27 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSEnumerator class reference</title>
|
|
|
|
$Date$ $Revision$
|
1995-04-09 01:28:47 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2007-11-29 20:53:26 +00:00
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSEnumerator.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#import "Foundation/NSException.h"
|
2009-09-05 17:43:13 +00:00
|
|
|
|
1995-04-09 01:28:47 +00:00
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
/**
|
|
|
|
* Simple class for iterating over a collection of objects, usually returned
|
|
|
|
* from an [NSArray] or similar.
|
|
|
|
*/
|
1995-04-09 01:28:47 +00:00
|
|
|
@implementation NSEnumerator
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
/**
|
2008-02-13 15:25:37 +00:00
|
|
|
* Returns all objects remaining in the enumeration as an array.<br />
|
|
|
|
* Calling this method 'exhausts' the enumerator, leaving it at the
|
|
|
|
* end of the collection being enumerated.
|
2004-06-22 22:40:40 +00:00
|
|
|
*/
|
2001-10-15 14:42:56 +00:00
|
|
|
- (NSArray*) allObjects
|
2001-01-03 11:22:59 +00:00
|
|
|
{
|
2001-10-15 14:42:56 +00:00
|
|
|
NSMutableArray *array;
|
|
|
|
id obj;
|
|
|
|
SEL nsel;
|
|
|
|
IMP nimp;
|
|
|
|
SEL asel;
|
|
|
|
IMP aimp;
|
|
|
|
|
|
|
|
array = [NSMutableArray arrayWithCapacity: 10];
|
|
|
|
|
|
|
|
nsel = @selector(nextObject);
|
|
|
|
nimp = [self methodForSelector: nsel];
|
|
|
|
asel = @selector(addObject:);
|
|
|
|
aimp = [array methodForSelector: asel];
|
|
|
|
|
|
|
|
while ((obj = (*nimp)(self, nsel)) != nil)
|
|
|
|
{
|
|
|
|
(*aimp)(array, asel, obj);
|
|
|
|
}
|
2001-01-03 11:22:59 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
/**
|
|
|
|
* Returns next object in enumeration, or nil if none remain. Use code like
|
|
|
|
* <code>while (object = [enumerator nextObject]) { ... }</code>.
|
|
|
|
*/
|
1995-04-09 01:28:47 +00:00
|
|
|
- (id) nextObject
|
|
|
|
{
|
1996-02-01 17:04:17 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-09 01:28:47 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2009-01-28 12:03:41 +00:00
|
|
|
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
|
|
|
|
objects: (id*)stackbuf
|
|
|
|
count: (NSUInteger)len
|
|
|
|
{
|
|
|
|
IMP nextObject = [self methodForSelector: @selector(nextObject)];
|
|
|
|
int i;
|
|
|
|
|
2009-02-04 05:30:39 +00:00
|
|
|
state->itemsPtr = stackbuf;
|
|
|
|
state->mutationsPtr = (unsigned long*)self;
|
2009-01-28 12:03:41 +00:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
id next = nextObject(self, @selector(nextObject));
|
|
|
|
|
|
|
|
if (nil == next)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
*(stackbuf+i) = next;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
1995-04-09 01:28:47 +00:00
|
|
|
@end
|
2009-09-05 17:43:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* objc_enumerationMutation() is called whenever a collection mutates in the
|
|
|
|
* middle of fast enumeration.
|
|
|
|
*/
|
|
|
|
void objc_enumerationMutation(id obj)
|
|
|
|
{
|
2016-06-28 11:17:25 +00:00
|
|
|
[NSException raise: NSGenericException
|
|
|
|
format: @"Collection %@ was mutated while being enumerated", obj];
|
2009-09-05 17:43:13 +00:00
|
|
|
}
|