Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -61,10 +61,18 @@ * \param firstObject The first object in the array * \return A new autoreleased OFArray */ + arrayWithObjects: (id)firstObject, ...; +/** + * \brief Creates a new OFArray with the objects from the specified array. + * + * \param array An array + * \return A new autoreleased OFArray + */ ++ arrayWithArray: (OFArray*)array; + /** * \brief Creates a new OFArray with the objects from the specified C array. * * \param objects A C array of objects, terminated with nil * \return A new autoreleased OFArray @@ -106,10 +114,18 @@ * \return An initialized OFArray */ - initWithObject: (id)firstObject arguments: (va_list)arguments; +/** + * \brief Initializes an OFArray with the objects from the specified array. + * + * \param array An array + * \return An initialized OFArray + */ +- initWithArray: (OFArray*)array; + /** * \brief Initializes an OFArray with the objects from the specified C array. * * \param objects A C array of objects, terminated with nil * \return An initialized OFArray Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -50,10 +50,15 @@ arguments: arguments] autorelease]; va_end(arguments); return ret; } + ++ arrayWithArray: (OFArray*)array +{ + return [[[self alloc] initWithArray: array] autorelease]; +} + arrayWithCArray: (id*)objects { return [[[self alloc] initWithCArray: objects] autorelease]; } @@ -121,10 +126,46 @@ while ((object = va_arg(arguments, id)) != nil) { [array addItem: &object]; [object retain]; } } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithArray: (OFArray*)array_ +{ + id *cArray; + size_t i, count; + + self = [self init]; + + @try { + cArray = [array_ cArray]; + count = [array_ count]; + } @catch (id e) { + [self release]; + @throw e; + } + + @try { + for (i = 0; i < count; i++) + [cArray[i] retain]; + + [array addNItems: count + fromCArray: cArray]; + } @catch (id e) { + for (i = 0; i < count; i++) + [cArray[i] release]; + + /* Prevent double-release of objects */ + [array release]; + array = nil; + [self release]; @throw e; } return self; @@ -199,24 +240,11 @@ return [self retain]; } - mutableCopy { - OFArray *mutableCopy = [[OFMutableArray alloc] init]; - id *cArray; - size_t count, i; - - cArray = [array cArray]; - count = [array count]; - - [mutableCopy->array addNItems: count - fromCArray: cArray]; - - for (i = 0; i < count; i++) - [cArray[i] retain]; - - return mutableCopy; + return [[OFMutableArray alloc] initWithArray: self]; } - (id)objectAtIndex: (size_t)index { return *((id*)[array itemAtIndex: index]); Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -27,21 +27,11 @@ #import "OFOutOfRangeException.h" @implementation OFMutableArray - copy { - OFArray *copy = [[OFArray alloc] init]; - id *cArray = [array cArray]; - size_t i, count = [array count]; - - [copy->array addNItems: count - fromCArray: cArray]; - - for (i = 0; i < count; i++) - [cArray[i] retain]; - - return copy; + return [[OFArray alloc] initWithArray: self]; } - (void)addObject: (id)object { [array addItem: &object];