Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -17,11 +17,10 @@ OFXMLFactory.m \ ${ASPRINTF} INCLUDESTMP = ${SRCS:.c=.h} INCLUDES = ${INCLUDESTMP:.m=.h} \ - OFComparable.h \ OFMacros.h \ OFStream.h include ../buildsys.mk Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -8,18 +8,17 @@ * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "OFObject.h" -#import "OFComparable.h" /** * The OFArray class provides a class for storing dynamically sized arrays. * If you plan to store large hunks of data, you should consider using * OFBigArray, which allocates the memory in pages and not in bytes. */ -@interface OFArray: OFObject +@interface OFArray: OFObject { char *data; size_t itemsize; size_t items; } @@ -62,10 +61,26 @@ /** * \return All elements of the OFArray */ - (void*)data; +/** + * Clones the OFArray, creating a new one. + * + * \return A new autoreleased copy of the OFArray + */ +- (id)copy; + +/** + * Compares the OFArray to another object. + * + * \param obj An object to compare with + * \return An integer which is the result of the comparison, see for example + * strcmp + */ +- (int)compare: (id)obj; + /** * Returns a specific item of the OFArray. * * \param item The number of the item to return * \return The specified item of the OFArray @@ -97,17 +112,10 @@ * Removes a specified amount of the last items from the OFArray. * * \param nitems The number of items to remove */ - removeNItems: (size_t)nitems; - -/** - * Clones the OFArray, creating a new one. - * - * \return A new autoreleased copy of the OFArray - */ -- (id)copy; @end @interface OFBigArray: OFArray { size_t size; DELETED src/OFComparable.h Index: src/OFComparable.h ================================================================== --- src/OFComparable.h +++ src/OFComparable.h @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * Jonathan Schleifer - * - * All rights reserved. - * - * 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. - */ - -#import "OFObject.h" - -/** - * The OFComparable protocol provides functions to compare two objects. - */ -@protocol OFComparable -/** - * \param obj The object which is tested for equality - * \return A boolean whether the object is equal to the other object - */ -- (BOOL)isEqual: (id)obj; - -/** - * Compares the object to another object. - * - * \param obj An object to compare with - * \return An integer which is the result of the comparison, see for example - * strcmp - */ -- (int)compare: (id)obj; -@end Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -574,6 +574,46 @@ - (long double)asLongDouble { RETURN_AS(long double) } + +- (BOOL)isEqual: (id)obj +{ + if (![obj isKindOf: [OFNumber class]]) + return NO; + + switch (type) { + case OF_NUMBER_CHAR: + case OF_NUMBER_SHORT: + case OF_NUMBER_INT: + case OF_NUMBER_LONG: + case OF_NUMBER_INT8: + case OF_NUMBER_INT16: + case OF_NUMBER_INT32: + case OF_NUMBER_INT64: + case OF_NUMBER_INTMAX: + case OF_NUMBER_PTRDIFF: + return ([obj asIntMax] == [self asIntMax] ? YES : NO); + case OF_NUMBER_SSIZE: + case OF_NUMBER_UCHAR: + case OF_NUMBER_USHORT: + case OF_NUMBER_UINT: + case OF_NUMBER_ULONG: + case OF_NUMBER_UINT8: + case OF_NUMBER_UINT16: + case OF_NUMBER_UINT32: + case OF_NUMBER_UINT64: + case OF_NUMBER_SIZE: + case OF_NUMBER_UINTMAX: + case OF_NUMBER_INTPTR: + return ([obj asUIntMax] == [self asUIntMax] ? YES : NO); + case OF_NUMBER_FLOAT: + case OF_NUMBER_DOUBLE: + case OF_NUMBER_LONG_DOUBLE: + return ([obj asLongDouble] == [self asLongDouble] ? YES : NO); + default: + @throw [OFInvalidArgumentException newWithClass: [self class] + andSelector: _cmd]; + } +} @end Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -49,10 +49,34 @@ /** * \return The retain count */ - (size_t)retainCount; +/** + * Frees the object and also frees all memory allocated via its memory pool. + */ +- free; + +/** + * Compare two objects. + * Classes containing data (like strings, arrays, lists etc.) should reimplement + * this! + * + * \param obj The object which is tested for equality + * \return A boolean whether the object is equal to the other object + */ +- (BOOL)isEqual: (id)obj; + +/** + * Calculate a hash for the object. + * Classes containing data (like strings, arrays, lists etc.) should reimplement + * this! + * + * \return A 24 bit hash for the object + */ +- (uint32_t)hash; + /** * Adds a pointer to the memory pool. * This is useful to add memory allocated by functions such as asprintf to the * pool so it gets freed automatically when the object is freed. * @@ -107,11 +131,6 @@ * Frees allocated memory and removes it from the memory pool. * * \param ptr A pointer to the allocated memory */ - freeMem: (void*)ptr; - -/** - * Frees the object and also frees all memory allocated via its memory pool. - */ -- free; @end Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -69,10 +69,22 @@ - (size_t)retainCount { return __retain_count; } + +- (BOOL)isEqual: (id)obj +{ + /* Classes containing data should reimplement this! */ + return (self == obj ? YES : NO); +} + +- (uint32_t)hash +{ + /* Classes containing data should reimplement this! */ + return (uint32_t)self & 0xFFFFFF; +} - addToMemoryPool: (void*)ptr { void **memchunks; size_t memchunks_size; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -11,16 +11,15 @@ #include #include #import "OFObject.h" -#import "OFComparable.h" /** * A class for storing and modifying strings. */ -@interface OFString: OFObject +@interface OFString: OFObject { char *string; size_t length; BOOL is_utf8; } @@ -108,10 +107,19 @@ * * \return A new autoreleased copy of the OFString */ - (id)copy; +/** + * Compares the OFString to another object. + * + * \param obj An object to compare with + * \return An integer which is the result of the comparison, see for example + * strcmp + */ +- (int)compare: (id)obj; + /** * Sets the OFString to the specified OFString. * * \param str An OFString to set the OFString to. */