Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -182,10 +182,17 @@ * @return A new autoreleased OFArray */ + (instancetype)arrayWithObjects: (ObjectType const _Nonnull *_Nonnull)objects count: (size_t)count; +/** + * @brief Initializes an OFArray with no objects. + * + * @return An initialized OFArray + */ +- (instancetype)init OF_DESIGNATED_INITIALIZER; + /** * @brief Initializes an OFArray with the specified object. * * @param object An object * @return An initialized OFArray @@ -225,11 +232,11 @@ * @param objects A C array of objects * @param count The length of the C array * @return An initialized OFArray */ - (instancetype)initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects - count: (size_t)count; + count: (size_t)count OF_DESIGNATED_INITIALIZER; /** * @brief Returns an OFEnumerator to enumerate through all objects of the array. * * @return An OFEnumerator to enumerate through all objects of the array Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -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 } Index: src/OFConcreteArray.m ================================================================== --- src/OFConcreteArray.m +++ src/OFConcreteArray.m @@ -123,11 +123,11 @@ return self; } - (instancetype)initWithObjects: (id const *)objects count: (size_t)count { - self = [self init]; + self = [super init]; @try { bool ok = true; for (size_t i = 0; i < count; i++) { @@ -138,10 +138,12 @@ } if (!ok) @throw [OFInvalidArgumentException exception]; + _array = [[OFMutableData alloc] initWithItemSize: sizeof(id) + capacity: count]; [_array addItems: objects count: count]; } @catch (id e) { for (size_t i = 0; i < count; i++) [objects[i] release]; Index: src/OFMutableArray.h ================================================================== --- src/OFMutableArray.h +++ src/OFMutableArray.h @@ -51,18 +51,25 @@ * @param capacity The initial capacity for the OFMutableArray * @return A new autoreleased OFMutableArray */ + (instancetype)arrayWithCapacity: (size_t)capacity; +/** + * @brief Initializes an OFMutableArray with no objects. + * + * @return An initialized OFMutableArray + */ +- (instancetype)init OF_DESIGNATED_INITIALIZER; + /** * @brief Initializes an already allocated OFMutableArray with enough memory to * hold the specified number of objects. * * @param capacity The initial capacity for the OFMutableArray * @return An initialized OFMutableArray */ -- (instancetype)initWithCapacity: (size_t)capacity; +- (instancetype)initWithCapacity: (size_t)capacity OF_DESIGNATED_INITIALIZER; /** * @brief Adds an object to the end of the array. * * @param object An object to add Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -77,10 +77,16 @@ left = i + 1; } } @implementation OFPlaceholderMutableArray +#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)[[OFConcreteMutableArray alloc] init]; } @@ -121,10 +127,13 @@ - (instancetype)initWithObjects: (id const *)objects count: (size_t)count { return (id)[[OFConcreteMutableArray alloc] initWithObjects: objects count: count]; } +#ifdef __clang__ +# pragma clang diagnostic pop +#endif OF_SINGLETON_METHODS @end @implementation OFMutableArray @@ -145,15 +154,35 @@ + (instancetype)arrayWithCapacity: (size_t)capacity { return [[[self alloc] initWithCapacity: capacity] autorelease]; } + +- (instancetype)init +{ + return [super init]; +} + +#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 +} - (instancetype)initWithCapacity: (size_t)capacity { OF_INVALID_INIT_METHOD } +#ifdef __clang__ +# pragma clang diagnostic pop +#endif - (id)copy { return [[OFArray alloc] initWithArray: self]; }