Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -6,10 +6,12 @@ * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ + +#include #import "OFObject.h" #import "OFDataArray.h" /** @@ -23,10 +25,48 @@ /** * \return A new autoreleased OFArray */ + array; +/** + * \param obj An object + * \return A new autoreleased OFArray + */ ++ arrayWithObject: (OFObject*)obj; + +/** + * \param first The first object in the array + * \return A new autoreleased OFArray + */ ++ arrayWithObjects: (OFObject*)first, ...; + +/** + * Initializes an OFArray with the specified object. + * + * \param obj An object + * \return An initialized OFArray + */ +- initWithObject: (OFObject*)obj; + +/** + * Initializes an OFArray with the specified objects. + * + * \param obj The first object + * \return An initialized OFArray + */ +- initWithObjects: (OFObject*)first, ...; + +/** + * Initializes an OFArray with the specified object and a va_list. + * + * \param first The first object + * \param args A va_list + * \return An initialized OFArray + */ +- initWithObject: (OFObject*)first + andArgList: (va_list)args; + /** * \return The number of objects in the OFArray */ - (size_t)count; Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -9,18 +9,38 @@ * the packaging of this file. */ #import "config.h" +#include + #import "OFArray.h" #import "OFExceptions.h" @implementation OFArray + array { return [[[self alloc] init] autorelease]; } + ++ arrayWithObject: (OFObject*)obj +{ + return [[[self alloc] initWithObject: obj] autorelease]; +} + ++ arrayWithObjects: (OFObject*)first, ... +{ + id ret; + va_list args; + + va_start(args, first); + ret = [[[self alloc] initWithObject: first + andArgList: args] autorelease]; + va_end(args); + + return ret; +} - init { self = [super init]; @@ -34,10 +54,68 @@ */ [self dealloc]; @throw e; } + return self; +} + +- initWithObject: (OFObject*)obj +{ + self = [self init]; + + @try { + [array add: &obj]; + } @catch (OFException *e) { + [self dealloc]; + @throw e; + } + + [obj retain]; + + return self; +} + +- initWithObjects: (OFObject*)first, ... +{ + id ret; + va_list args; + + va_start(args, first); + ret = [self initWithObject: first + andArgList: args]; + va_end(args); + + return ret; +} + +- initWithObject: (OFObject*)first + andArgList: (va_list)args +{ + id obj; + OFObject **objs; + size_t len, i; + + self = [self init]; + + @try { + [array add: &first]; + while ((obj = va_arg(args, id)) != nil) + [array add: &obj]; + } @catch (OFException *e) { + [self dealloc]; + @throw e; + } + + /* Retain objects after adding them for the case adding failed */ + objs = [array data]; + len = [array count]; + + [first retain]; + for (i = 0; i < len; i++) + [objs[i] retain]; + return self; } - (size_t)count {