@@ -42,10 +42,16 @@ @interface OFPlaceholderArray: OFArray @end @implementation OFPlaceholderArray +#ifdef __clang__ +/* We intentionally don't call into super, so silence the warning. */ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunknown-pragmas" +# pragma clang diagnostic ignored "-Wobjc-designated-initializers" +#endif - (instancetype)init { return (id)[[OFConcreteArray alloc] init]; } @@ -83,10 +89,13 @@ count: (size_t)count { return (id)[[OFConcreteArray alloc] initWithObjects: objects count: count]; } +#ifdef __clang__ +# pragma clang diagnostic pop +#endif OF_SINGLETON_METHODS @end @implementation OFArray @@ -156,16 +165,11 @@ return [super init]; } - (instancetype)initWithObject: (id)object { - if (object == nil) { - [self release]; - @throw [OFInvalidArgumentException exception]; - } - - return [self initWithObjects: object, nil]; + return [self initWithObjects: &object count: 1]; } - (instancetype)initWithObjects: (id)firstObject, ... { id ret; @@ -179,23 +183,79 @@ } - (instancetype)initWithObject: (id)firstObject arguments: (va_list)arguments { - OF_INVALID_INIT_METHOD + size_t count = 1; + va_list argumentsCopy; + id *objects; + + va_copy(argumentsCopy, arguments); + while (va_arg(argumentsCopy, id) != nil) + count++; + + @try { + if (firstObject == nil) + @throw [OFInvalidArgumentException exception]; + + objects = OFAllocMemory(count, sizeof(id)); + } @catch (id e) { + [self release]; + @throw e; + } + + @try { + objects[0] = firstObject; + + for (size_t i = 1; i < count; i++) { + objects[i] = va_arg(arguments, id); + OFEnsure(objects[i] != nil); + } + + return [self initWithObjects: objects count: count]; + } @finally { + OFFreeMemory(objects); + } } - (instancetype)initWithArray: (OFArray *)array { - OF_INVALID_INIT_METHOD + id *objects; + size_t count; + + @try { + count = array.count; + objects = OFAllocMemory(count, sizeof(id)); + + [array getObjects: objects + inRange: OFMakeRange(0, count)]; + } @catch (id e) { + [self release]; + @throw e; + } + + @try { + return [self initWithObjects: objects count: count]; + } @finally { + OFFreeMemory(objects); + } } +#ifdef __clang__ +/* We intentionally don't call into super, so silence the warning. */ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunknown-pragmas" +# pragma clang diagnostic ignored "-Wobjc-designated-initializers" +#endif - (instancetype)initWithObjects: (id const *)objects count: (size_t)count { OF_INVALID_INIT_METHOD } +#ifdef __clang__ +# pragma clang diagnostic pop +#endif - (size_t)count { OF_UNRECOGNIZED_SELECTOR }