Target.m
author Jens Alfke <jens@mooseyard.com>
Sat Mar 08 21:04:41 2008 -0800 (2008-03-08)
changeset 0 d84d25d6cdbb
child 1 e55a17cdabd2
permissions -rw-r--r--
Initial checkin.
     1 //
     2 //  Target.m
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 2/11/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "Target.h"
    10 
    11 
    12 NSInvocation* _mktarget( id rcvr, SEL action )
    13 {
    14     NSMethodSignature *sig = [rcvr methodSignatureForSelector: action];
    15     CAssert(sig,@"%@<%p> does not respond to %@",[rcvr class],rcvr,NSStringFromSelector(action));
    16     CAssert(sig.numberOfArguments==3,
    17            @"-[%@ %@] can't be used as a target because it takes >1 param",
    18            [rcvr class],NSStringFromSelector(action));
    19     CAssert(0==strcmp([sig getArgumentTypeAtIndex: 2],"@"),
    20            @"-[%@ %@] can't be used as a target because it takes a non-object param",
    21            [rcvr class],NSStringFromSelector(action));
    22     NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sig];
    23     inv.target = rcvr;
    24     inv.selector = action;
    25     return inv;
    26 }
    27 
    28 
    29 id $calltarget( NSInvocation *target, id param )
    30 {
    31     if( target && target.target ) {
    32         [target setArgument: &param atIndex: 2];
    33         [target invoke];
    34         
    35         NSMethodSignature *sig = target.methodSignature;
    36         NSUInteger returnLength = sig.methodReturnLength;
    37         if( returnLength==0 )
    38             return nil; // void
    39         else {
    40             const char *returnType = sig.methodReturnType;
    41             if( returnType[0]=='@' ) {
    42                 id returnObject;
    43                 [target getReturnValue: &returnObject];
    44                 return returnObject;
    45             } else {
    46                 UInt8 returnBuffer[returnLength];
    47                 [target getReturnValue: &returnBuffer];
    48                 return [NSValue valueWithBytes: &returnBuffer objCType: returnType];
    49             }
    50         }
    51     } else
    52         return nil;
    53 }