Configurable logging (LogTo).
Added "my_" prefix to category method names.
Added MurmurHash.
Added UniqueWindowController.
Bug fixes.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import "CollectionUtils.h"
13 NSDictionary* _dictof(const struct _dictpair* pairs, size_t count)
16 id objects[count], keys[count];
18 for( size_t i=0; i<count; i++,pairs++ ) {
20 objects[n] = pairs->value;
25 return [NSDictionary dictionaryWithObjects: objects forKeys: keys count: n];
29 NSMutableDictionary* _mdictof(const struct _dictpair* pairs, size_t count)
32 id objects[count], keys[count];
34 for( size_t i=0; i<count; i++,pairs++ ) {
36 objects[n] = pairs->value;
41 return [NSMutableDictionary dictionaryWithObjects: objects forKeys: keys count: n];
45 BOOL $equal(id obj1, id obj2) // Like -isEqual: but works even if either/both are nil
48 return obj2 && [obj1 isEqual: obj2];
54 NSValue* _box(const void *value, const char *encoding)
56 // file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.DeveloperTools.docset/Contents/Resources/Documents/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html
58 if( e=='r' ) // ignore 'const' modifier
61 case 'c': return [NSNumber numberWithChar: *(char*)value];
62 case 'C': return [NSNumber numberWithUnsignedChar: *(char*)value];
63 case 's': return [NSNumber numberWithShort: *(short*)value];
64 case 'S': return [NSNumber numberWithUnsignedShort: *(unsigned short*)value];
65 case 'i': return [NSNumber numberWithInt: *(int*)value];
66 case 'I': return [NSNumber numberWithUnsignedInt: *(unsigned int*)value];
67 case 'l': return [NSNumber numberWithLong: *(long*)value];
68 case 'L': return [NSNumber numberWithUnsignedLong: *(unsigned long*)value];
69 case 'q': return [NSNumber numberWithLongLong: *(long long*)value];
70 case 'Q': return [NSNumber numberWithUnsignedLongLong: *(unsigned long long*)value];
71 case 'f': return [NSNumber numberWithFloat: *(float*)value];
72 case 'd': return [NSNumber numberWithDouble: *(double*)value];
73 case '*': return [NSString stringWithUTF8String: *(char**)value];
74 case '@': return *(id*)value;
75 default: return [NSValue value: value withObjCType: encoding];
80 id _cast( Class requiredClass, id object )
82 if( object && ! [object isKindOfClass: requiredClass] )
83 [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
84 requiredClass,[object class],object];
88 id _castNotNil( Class requiredClass, id object )
90 if( ! [object isKindOfClass: requiredClass] )
91 [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
92 requiredClass,[object class],object];
96 id _castIf( Class requiredClass, id object )
98 if( object && ! [object isKindOfClass: requiredClass] )
103 NSArray* _castArrayOf(Class itemClass, NSArray *a)
106 foreach( item, $cast(NSArray,a) )
107 _cast(itemClass,item);
112 void setObj( id *var, id value )
114 if( value != *var ) {
116 *var = [value retain];
120 BOOL ifSetObj( id *var, id value )
122 if( value != *var && ![value isEqual: *var] ) {
124 *var = [value retain];
132 void setString( NSString **var, NSString *value )
134 if( value != *var ) {
141 BOOL ifSetString( NSString **var, NSString *value )
143 if( value != *var && ![value isEqualToString: *var] ) {
153 @implementation NSArray (MYUtils)
155 - (BOOL) my_containsObjectIdenticalTo: (id)object
157 return [self indexOfObjectIdenticalTo: object] != NSNotFound;
165 TestCase(CollectionUtils) {
166 NSArray *a = $array(@"foo",@"bar",@"baz");
168 NSArray *aa = [NSArray arrayWithObjects: @"foo",@"bar",@"baz",nil];
171 const char *cstr = "a C string";
172 id o = $object(cstr);
174 CAssertEqual(o,@"a C string");
176 NSDictionary *d = $dict({@"int", $object(1)},
177 {@"double", $object(-1.1)},
178 {@"char", $object('x')},
179 {@"ulong", $object(1234567UL)},
180 {@"longlong",$object(987654321LL)},
181 {@"cstr", $object(cstr)});
183 NSDictionary *dd = [NSDictionary dictionaryWithObjectsAndKeys:
184 [NSNumber numberWithInt: 1], @"int",
185 [NSNumber numberWithDouble: -1.1], @"double",
186 [NSNumber numberWithChar: 'x'], @"char",
187 [NSNumber numberWithUnsignedLong: 1234567UL], @"ulong",
188 [NSNumber numberWithDouble: 987654321LL], @"longlong",
189 @"a C string", @"cstr",