10.6 compatibility: Fix some new compiler warnings, and work around apparent regressions in NSTask and -stringByStandardizingPath.
5 // Created by Jens Alfke on 2/11/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
14 @implementation MYTarget
17 - (id) initWithReceiver: (id)receiver action: (SEL)action
21 NSMethodSignature *sig = [receiver methodSignatureForSelector: action];
22 CAssert(sig,@"%@<%p> does not respond to %@",[receiver class],receiver,NSStringFromSelector(action));
23 CAssert(sig.numberOfArguments==3,
24 @"-[%@ %@] can't be used as a target because it takes >1 param",
25 [receiver class],NSStringFromSelector(action));
26 CAssert(0==strcmp([sig getArgumentTypeAtIndex: 2],"@"),
27 @"-[%@ %@] can't be used as a target because it takes a non-object param",
28 [receiver class],NSStringFromSelector(action));
29 NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sig];
30 inv.target = receiver;
31 inv.selector = action;
32 _invocations = [inv retain];
37 + (MYTarget*) targetWithReceiver: (id)receiver action: (SEL)action
39 return [[[self alloc] initWithReceiver: receiver action: action] autorelease];
44 [_invocations release];
49 - (NSArray*) invocations
51 NSMutableArray *invocations = $castIf(NSMutableArray,_invocations);
53 invocations = [NSMutableArray arrayWithObject: _invocations];
58 - (NSString*) description
60 NSMutableString *desc = [NSMutableString stringWithFormat: @"%@{", self.class];
62 for( NSInvocation *inv in self.invocations ) {
66 [desc appendString: @", "];
67 [desc appendFormat: @"-[%@ %s]", [inv.target class], inv.selector];
69 [desc appendString: @"}"];
74 static BOOL equalInvocations( NSInvocation *a, NSInvocation *b )
76 return a.target==b.target && a.selector==b.selector;
80 - (BOOL) isEqual: (MYTarget*)t
82 if( ! [t isKindOfClass: [self class]] )
84 if( [_invocations isKindOfClass: [NSInvocation class]] && [t->_invocations isKindOfClass: [NSInvocation class]] )
85 return equalInvocations(_invocations,t->_invocations);
86 NSArray *myInvocations = self.invocations, *itsInvocations = t.invocations;
87 NSUInteger n = myInvocations.count;
88 if( n != itsInvocations.count )
90 for( NSUInteger i=0; i<n; i++ )
91 if( ! equalInvocations( [myInvocations objectAtIndex: i],
92 [itsInvocations objectAtIndex: i] ) )
98 - (void) addTarget: (MYTarget*)target
100 setObj(&_invocations,[self invocations]);
101 [_invocations addObjectsFromArray: target.invocations];
105 static id invokeSingleTarget( NSInvocation *invocation, id param )
108 if( invocation && invocation.target ) {
111 [invocation setArgument: ¶m atIndex: 2];
114 NSMethodSignature *sig = invocation.methodSignature;
115 NSUInteger returnLength = sig.methodReturnLength;
116 if( returnLength==0 ) {
117 result = nil; // void
119 const char *returnType = sig.methodReturnType;
120 if( returnType[0]=='@' ) {
121 [invocation getReturnValue: &result];
123 UInt8 returnBuffer[returnLength];
124 [invocation getReturnValue: &returnBuffer];
125 result = [NSValue valueWithBytes: &returnBuffer objCType: returnType];
129 [invocation release];
136 - (id) invokeWithSender: (id)sender
139 NSMutableArray *invocations = $castIf(NSMutableArray,_invocations);
142 for( NSInvocation *invocation in invocations )
143 result = invokeSingleTarget(invocation,sender);
145 result = invokeSingleTarget(_invocations,sender);
155 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
157 Redistribution and use in source and binary forms, with or without modification, are permitted
158 provided that the following conditions are met:
160 * Redistributions of source code must retain the above copyright notice, this list of conditions
161 and the following disclaimer.
162 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
163 and the following disclaimer in the documentation and/or other materials provided with the
166 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
167 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
168 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
169 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
170 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
171 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
172 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
173 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.