diff -r 000000000000 -r e55a17cdabd2 CollectionUtils.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CollectionUtils.m Thu Mar 20 09:05:58 2008 -0700 @@ -0,0 +1,192 @@ +// +// CollectionUtils.m +// MYUtilities +// +// Created by Jens Alfke on 1/5/08. +// Copyright 2008 Jens Alfke. All rights reserved. +// + +#import "CollectionUtils.h" +#import "Test.h" + + +NSDictionary* _dictof(const struct _dictpair* pairs, size_t count) +{ + CAssert(count<10000); + id objects[count], keys[count]; + size_t n = 0; + for( size_t i=0; ivalue ) { + objects[n] = pairs->value; + keys[n] = pairs->key; + n++; + } + } + return [NSDictionary dictionaryWithObjects: objects forKeys: keys count: n]; +} + + +NSMutableDictionary* _mdictof(const struct _dictpair* pairs, size_t count) +{ + CAssert(count<10000); + id objects[count], keys[count]; + size_t n = 0; + for( size_t i=0; ivalue ) { + objects[n] = pairs->value; + keys[n] = pairs->key; + n++; + } + } + return [NSMutableDictionary dictionaryWithObjects: objects forKeys: keys count: n]; +} + + +BOOL $equal(id obj1, id obj2) // Like -isEqual: but works even if either/both are nil +{ + if( obj1 ) + return obj2 && [obj1 isEqual: obj2]; + else + return obj2==nil; +} + + +NSValue* _box(const void *value, const char *encoding) +{ + // file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.DeveloperTools.docset/Contents/Resources/Documents/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html + char e = encoding[0]; + if( e=='r' ) // ignore 'const' modifier + e = encoding[1]; + switch( e ) { + case 'c': return [NSNumber numberWithChar: *(char*)value]; + case 'C': return [NSNumber numberWithUnsignedChar: *(char*)value]; + case 's': return [NSNumber numberWithShort: *(short*)value]; + case 'S': return [NSNumber numberWithUnsignedShort: *(unsigned short*)value]; + case 'i': return [NSNumber numberWithInt: *(int*)value]; + case 'I': return [NSNumber numberWithUnsignedInt: *(unsigned int*)value]; + case 'l': return [NSNumber numberWithLong: *(long*)value]; + case 'L': return [NSNumber numberWithUnsignedLong: *(unsigned long*)value]; + case 'q': return [NSNumber numberWithLongLong: *(long long*)value]; + case 'Q': return [NSNumber numberWithUnsignedLongLong: *(unsigned long long*)value]; + case 'f': return [NSNumber numberWithFloat: *(float*)value]; + case 'd': return [NSNumber numberWithDouble: *(double*)value]; + case '*': return [NSString stringWithUTF8String: *(char**)value]; + case '@': return *(id*)value; + default: return [NSValue value: value withObjCType: encoding]; + } +} + + +id _cast( Class requiredClass, id object ) +{ + if( object && ! [object isKindOfClass: requiredClass] ) + [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p", + requiredClass,[object class],object]; + return object; +} + +id _castNotNil( Class requiredClass, id object ) +{ + if( ! [object isKindOfClass: requiredClass] ) + [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p", + requiredClass,[object class],object]; + return object; +} + +id _castIf( Class requiredClass, id object ) +{ + if( object && ! [object isKindOfClass: requiredClass] ) + object = nil; + return object; +} + +NSArray* _castArrayOf(Class itemClass, NSArray *a) +{ + id item; + foreach( item, $cast(NSArray,a) ) + _cast(itemClass,item); + return a; +} + + +void setObj( id *var, id value ) +{ + if( value != *var ) { + [*var release]; + *var = [value retain]; + } +} + +BOOL ifSetObj( id *var, id value ) +{ + if( value != *var && ![value isEqual: *var] ) { + [*var release]; + *var = [value retain]; + return YES; + } else { + return NO; + } +} + + +void setString( NSString **var, NSString *value ) +{ + if( value != *var ) { + [*var release]; + *var = [value copy]; + } +} + + +BOOL ifSetString( NSString **var, NSString *value ) +{ + if( value != *var && ![value isEqualToString: *var] ) { + [*var release]; + *var = [value copy]; + return YES; + } else { + return NO; + } +} + + +@implementation NSArray (MYUtils) + +- (BOOL) my_containsObjectIdenticalTo: (id)object +{ + return [self indexOfObjectIdenticalTo: object] != NSNotFound; +} + +@end + + +#import "Test.h" + +TestCase(CollectionUtils) { + NSArray *a = $array(@"foo",@"bar",@"baz"); + //Log(@"a = %@",a); + NSArray *aa = [NSArray arrayWithObjects: @"foo",@"bar",@"baz",nil]; + CAssertEqual(a,aa); + + const char *cstr = "a C string"; + id o = $object(cstr); + //Log(@"o = %@",o); + CAssertEqual(o,@"a C string"); + + NSDictionary *d = $dict({@"int", $object(1)}, + {@"double", $object(-1.1)}, + {@"char", $object('x')}, + {@"ulong", $object(1234567UL)}, + {@"longlong",$object(987654321LL)}, + {@"cstr", $object(cstr)}); + //Log(@"d = %@",d); + NSDictionary *dd = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithInt: 1], @"int", + [NSNumber numberWithDouble: -1.1], @"double", + [NSNumber numberWithChar: 'x'], @"char", + [NSNumber numberWithUnsignedLong: 1234567UL], @"ulong", + [NSNumber numberWithDouble: 987654321LL], @"longlong", + @"a C string", @"cstr", + nil]; + CAssertEqual(d,dd); +}