# HG changeset patch # User Jens Alfke # Date 1209757783 25200 # Node ID 59addced5e2a7703db2635154862babcd50c7879 # Parent 2d492d8c20530aff1ec379b3153e9d34cc4e277d Added URLUtils. Rewrote Target. diff -r 2d492d8c2053 -r 59addced5e2a CollectionUtils.h --- a/CollectionUtils.h Fri Apr 18 09:25:10 2008 -0700 +++ b/CollectionUtils.h Fri May 02 12:49:43 2008 -0700 @@ -47,6 +47,7 @@ @interface NSSet (MYUtils) + (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2; ++ (NSSet*) my_intersectionOfSet: (NSSet*)set1 andSet: (NSSet*)set2; + (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2; @end diff -r 2d492d8c2053 -r 59addced5e2a CollectionUtils.m --- a/CollectionUtils.m Fri Apr 18 09:25:10 2008 -0700 +++ b/CollectionUtils.m Fri May 02 12:49:43 2008 -0700 @@ -161,7 +161,9 @@ + @implementation NSSet (MYUtils) + + (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2 { if( set1 == set2 || set2.count==0 ) @@ -175,6 +177,19 @@ } } ++ (NSSet*) my_intersectionOfSet: (NSSet*)set1 andSet: (NSSet*)set2 +{ + if( set1 == set2 || set1.count==0 ) + return set1; + else if( set2.count==0 ) + return set2; + else { + NSMutableSet *result = [set1 mutableCopy]; + [result intersectSet: set2]; + return [result autorelease]; + } +} + + (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2 { if( set1.count==0 || set2.count==0 ) @@ -187,6 +202,7 @@ return [result autorelease]; } } + @end diff -r 2d492d8c2053 -r 59addced5e2a Target.h --- a/Target.h Fri Apr 18 09:25:10 2008 -0700 +++ b/Target.h Fri May 02 12:49:43 2008 -0700 @@ -9,9 +9,18 @@ #import -#define $target(RCVR,METHOD) _mktarget((RCVR),@selector(METHOD)) +@interface MYTarget : NSObject +{ + id _invocations; // May be an NSInvocation, or an NSMutableArray of them +} -id $calltarget( NSInvocation *target, id sender ); ++ (MYTarget*) targetWithReceiver: (id)receiver action: (SEL)action; +- (void) addTarget: (MYTarget*)target; -NSInvocation* _mktarget( id rcvr, SEL action ); +- (id) invokeWithSender: (id)sender; + +@end + + +#define $target(RCVR,METHOD) [MYTarget targetWithReceiver: (RCVR) action: @selector(METHOD)] diff -r 2d492d8c2053 -r 59addced5e2a Target.m --- a/Target.m Fri Apr 18 09:25:10 2008 -0700 +++ b/Target.m Fri May 02 12:49:43 2008 -0700 @@ -9,49 +9,140 @@ #import "Target.h" -NSInvocation* _mktarget( id rcvr, SEL action ) +@implementation MYTarget + + +- (id) initWithReceiver: (id)receiver action: (SEL)action { - NSMethodSignature *sig = [rcvr methodSignatureForSelector: action]; - CAssert(sig,@"%@<%p> does not respond to %@",[rcvr class],rcvr,NSStringFromSelector(action)); - CAssert(sig.numberOfArguments==3, - @"-[%@ %@] can't be used as a target because it takes >1 param", - [rcvr class],NSStringFromSelector(action)); - CAssert(0==strcmp([sig getArgumentTypeAtIndex: 2],"@"), - @"-[%@ %@] can't be used as a target because it takes a non-object param", - [rcvr class],NSStringFromSelector(action)); - NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sig]; - inv.target = rcvr; - inv.selector = action; - return inv; + self = [super init]; + if( self ) { + NSMethodSignature *sig = [receiver methodSignatureForSelector: action]; + CAssert(sig,@"%@<%p> does not respond to %@",[receiver class],receiver,NSStringFromSelector(action)); + CAssert(sig.numberOfArguments==3, + @"-[%@ %@] can't be used as a target because it takes >1 param", + [receiver class],NSStringFromSelector(action)); + CAssert(0==strcmp([sig getArgumentTypeAtIndex: 2],"@"), + @"-[%@ %@] can't be used as a target because it takes a non-object param", + [receiver class],NSStringFromSelector(action)); + NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sig]; + inv.target = receiver; + inv.selector = action; + _invocations = [inv retain]; + } + return self; } ++ (MYTarget*) targetWithReceiver: (id)receiver action: (SEL)action +{ + return [[[self alloc] initWithReceiver: receiver action: action] autorelease]; +} -id $calltarget( NSInvocation *target, id param ) +- (void) dealloc +{ + [_invocations release]; + [super dealloc]; +} + + +- (NSArray*) invocations +{ + NSMutableArray *invocations = $castIf(NSMutableArray,_invocations); + if( ! invocations ) + invocations = [NSMutableArray arrayWithObject: _invocations]; + return invocations; +} + + +- (NSString*) description +{ + NSMutableString *desc = [NSMutableString stringWithFormat: @"%@{", self.class]; + BOOL first = YES; + for( NSInvocation *inv in self.invocations ) { + if( first ) + first = NO; + else + [desc appendString: @", "]; + [desc appendFormat: @"-[%@ %s]", [inv.target class], inv.selector]; + } + [desc appendString: @"}"]; + return desc; +} + + +static BOOL equalInvocations( NSInvocation *a, NSInvocation *b ) +{ + return a.target==b.target && a.selector==b.selector; +} + + +- (BOOL) isEqual: (MYTarget*)t +{ + if( ! [t isKindOfClass: [self class]] ) + return NO; + if( [_invocations isKindOfClass: [NSInvocation class]] && [t->_invocations isKindOfClass: [NSInvocation class]] ) + return equalInvocations(_invocations,t->_invocations); + NSArray *myInvocations = self.invocations, *itsInvocations = t.invocations; + unsigned n = myInvocations.count; + if( n != itsInvocations.count ) + return NO; + for( int i=0; i + + +@interface NSHTTPURLResponse (MYUtilities) + +- (NSError*) HTTPError; + +@end + +extern NSString* const MyHTTPErrorDomain; diff -r 2d492d8c2053 -r 59addced5e2a URLUtils.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/URLUtils.m Fri May 02 12:49:43 2008 -0700 @@ -0,0 +1,32 @@ +// +// URLUtils.m +// Cloudy +// +// Created by Jens Alfke on 4/28/08. +// Copyright 2008 __MyCompanyName__. All rights reserved. +// + +#import "URLUtils.h" + + +@implementation NSHTTPURLResponse (MYUtilities) + + +- (NSError*) HTTPError +{ + // HTTP status >= 300 is considered an error: + int status = self.statusCode; + if( status >= 300 ) { + NSString *reason = NSLocalizedStringFromTable( @"HTTP_ERROR_MESSAGE",@"UKCrashReporter",@""); + reason = [NSHTTPURLResponse localizedStringForStatusCode: status]; + NSDictionary *info = $dict({NSLocalizedFailureReasonErrorKey,reason}); + return [NSError errorWithDomain: MyHTTPErrorDomain code: status userInfo: info]; + } else + return nil; +} + + +NSString* const MyHTTPErrorDomain = @"HTTP"; + + +@end