@@ -82,12 +82,20 @@ /*! * @class OFArray OFArray.h ObjFW/OFArray.h * * @brief An abstract class for storing objects in an array. */ -@interface OFArray: OFObject +#ifdef OF_HAVE_GENERICS +@interface OFArray : +#else +# ifndef DOXYGEN +# define ObjectType id +# endif +@interface OFArray: +#endif + OFObject #ifdef OF_HAVE_PROPERTIES @property (readonly) size_t count; #endif /*! @@ -101,82 +109,82 @@ * @brief Creates a new OFArray with the specified object. * * @param object An object * @return A new autoreleased OFArray */ -+ (instancetype)arrayWithObject: (id)object; ++ (instancetype)arrayWithObject: (ObjectType)object; /*! * @brief Creates a new OFArray with the specified objects, terminated by nil. * * @param firstObject The first object in the array * @return A new autoreleased OFArray */ -+ (instancetype)arrayWithObjects: (id)firstObject, ... OF_SENTINEL; ++ (instancetype)arrayWithObjects: (ObjectType)firstObject, ... OF_SENTINEL; /*! * @brief Creates a new OFArray with the objects from the specified array. * * @param array An array * @return A new autoreleased OFArray */ -+ (instancetype)arrayWithArray: (OFArray*)array; ++ (instancetype)arrayWithArray: (OFArray OF_GENERIC(ObjectType)*)array; /*! * @brief Creates a new OFArray with the objects from the specified C array of * the specified length. * * @param objects A C array of objects * @param count The length of the C array * @return A new autoreleased OFArray */ -+ (instancetype)arrayWithObjects: (id const*)objects ++ (instancetype)arrayWithObjects: (ObjectType const*)objects count: (size_t)count; /*! * @brief Initializes an OFArray with the specified object. * * @param object An object * @return An initialized OFArray */ -- initWithObject: (id)object; +- initWithObject: (ObjectType)object; /*! * @brief Initializes an OFArray with the specified objects. * * @param firstObject The first object * @return An initialized OFArray */ -- initWithObjects: (id)firstObject, ... OF_SENTINEL; +- initWithObjects: (ObjectType)firstObject, ... OF_SENTINEL; /*! * @brief Initializes an OFArray with the specified object and a va_list. * * @param firstObject The first object * @param arguments A va_list * @return An initialized OFArray */ -- initWithObject: (id)firstObject +- initWithObject: (ObjectType)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; +- initWithArray: (OFArray OF_GENERIC(ObjectType)*)array; /*! * @brief Initializes an OFArray with the objects from the specified C array of * the specified length. * * @param objects A C array of objects * @param count The length of the C array * @return An initialized OFArray */ -- initWithObjects: (id const*)objects +- initWithObjects: (ObjectType const*)objects count: (size_t)count; /*! * @brief Returns the object at the specified index in the array. * @@ -184,86 +192,95 @@ * performance reasons! * * @param index The index of the object to return * @return The object at the specified index in the array */ -- (id)objectAtIndex: (size_t)index; -- (id)objectAtIndexedSubscript: (size_t)index; +- (ObjectType)objectAtIndex: (size_t)index; +- (ObjectType)objectAtIndexedSubscript: (size_t)index; /*! * @brief Copies the objects at the specified range to the specified buffer. * * @param buffer The buffer to copy the objects to * @param range The range to copy */ -- (void)getObjects: (__unsafe_unretained id*)buffer +- (void)getObjects: (__unsafe_unretained ObjectType*)buffer inRange: (of_range_t)range; /*! * @brief Returns the objects of the array as a C array. * * @return The objects of the array as a C array */ -- (id const*)objects; +- (ObjectType const*)objects; /*! * @brief Returns the index of the first object that is equivalent to the * specified object or `OF_NOT_FOUND` if it was not found. * * @param object The object whose index is returned * @return The index of the first object equivalent to the specified object * or `OF_NOT_FOUND` if it was not found */ -- (size_t)indexOfObject: (id)object; +- (size_t)indexOfObject: (ObjectType)object; /*! * @brief Returns the index of the first object that has the same address as the * specified object or `OF_NOT_FOUND` if it was not found. * * @param object The object whose index is returned * @return The index of the first object that has the same aaddress as * the specified object or `OF_NOT_FOUND` if it was not found */ -- (size_t)indexOfObjectIdenticalTo: (id)object; +- (size_t)indexOfObjectIdenticalTo: (ObjectType)object; + +/*! + * @brief Checks whether the array contains an object equal to the specified + * object. + * + * @param object The object which is checked for being in the array + * @return A boolean whether the array contains the specified object + */ +- (bool)containsObject: (ObjectType)object; /*! * @brief Checks whether the array contains an object with the specified * address. * * @param object The object which is checked for being in the array * @return A boolean whether the array contains an object with the specified - * address. + * address */ -- (bool)containsObjectIdenticalTo: (id)object; +- (bool)containsObjectIdenticalTo: (ObjectType)object; /*! * @brief Returns the first object of the array or nil. * * @warning The returned object is *not* retained and autoreleased for * performance reasons! * * @return The first object of the array or nil */ -- (id)firstObject; +- (ObjectType)firstObject; /*! * @brief Returns the last object of the array or nil. * * @warning The returned object is *not* retained and autoreleased for * performance reasons! * * @return The last object of the array or nil */ -- (id)lastObject; +- (ObjectType)lastObject; /*! * @brief Returns the objects in the specified range as a new OFArray. * * @param range The range for the subarray * @return The subarray as a new autoreleased OFArray */ -- (OFArray*)objectsInRange: (of_range_t)range; +- (OFArray OF_GENERIC(ObjectType)*)objectsInRange: (of_range_t)range; /*! * @brief Creates a string by joining all objects of the array. * * @param separator The string with which the objects should be joined @@ -334,11 +351,11 @@ /*! * @brief Returns a sorted copy of the array. * * @return A sorted copy of the array */ -- (OFArray*)sortedArray; +- (OFArray OF_GENERIC(ObjectType)*)sortedArray; /*! * @brief Returns a sorted copy of the array. * * @param options The options to use when sorting the array.@n @@ -346,42 +363,51 @@ * Value | Description * ---------------------------|------------------------- * `OF_ARRAY_SORT_DESCENDING` | Sort in descending order * @return A sorted copy of the array */ -- (OFArray*)sortedArrayWithOptions: (int)options; +- (OFArray OF_GENERIC(ObjectType)*)sortedArrayWithOptions: (int)options; /*! * @brief Returns a copy of the array with the order reversed. * * @return A copy of the array with the order reversed */ -- (OFArray*)reversedArray; +- (OFArray OF_GENERIC(ObjectType)*)reversedArray; /*! * @brief Creates a new array with the specified object added. * * @param object The object to add * @return A new array with the specified object added */ -- (OFArray*)arrayByAddingObject: (id)object; +- (OFArray OF_GENERIC(ObjectType)*)arrayByAddingObject: (ObjectType)object; /*! * @brief Creates a new array with the objects from the specified array added. * * @param array The array with objects to add * @return A new array with the objects from the specified array added */ -- (OFArray*)arrayByAddingObjectsFromArray: (OFArray*)array; +- (OFArray OF_GENERIC(ObjectType)*)arrayByAddingObjectsFromArray: + (OFArray OF_GENERIC(ObjectType)*)array; /*! * @brief Creates a new array with the specified object removed. * * @param object The object to remove * @return A new array with the specified object removed */ -- (OFArray*)arrayByRemovingObject: (id)object; +- (OFArray OF_GENERIC(ObjectType)*)arrayByRemovingObject: (ObjectType)object; + +/*! + * @brief Returns an OFEnumerator to enumerate through all objects of the + * array. + * + * @returns An OFEnumerator to enumerate through all objects of the array + */ +- (OFEnumerator OF_GENERIC(ObjectType)*)objectEnumerator; #ifdef OF_HAVE_BLOCKS /*! * @brief Executes a block for each object. * @@ -403,11 +429,12 @@ * * @param block A block which determines if the object should be in the new * array * @return A new, autoreleased OFArray */ -- (OFArray*)filteredArrayUsingBlock: (of_array_filter_block_t)block; +- (OFArray OF_GENERIC(ObjectType)*)filteredArrayUsingBlock: + (of_array_filter_block_t)block; /*! * @brief Folds the array to a single object using the specified block. * * If the array is empty, it will return nil. @@ -424,10 +451,13 @@ * @return The array folded to a single object */ - (id)foldUsingBlock: (of_array_fold_block_t)block; #endif @end +#if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN) +# undef ObjectType +#endif @interface OFArrayEnumerator: OFEnumerator { OFArray *_array; size_t _count;