Target.m
author Jens Alfke <jens@mooseyard.com>
Thu Mar 20 09:05:58 2008 -0700 (2008-03-20)
changeset 1 e55a17cdabd2
parent 0 d84d25d6cdbb
child 7 59addced5e2a
permissions -rw-r--r--
Configurable logging (LogTo).
Added "my_" prefix to category method names.
Added MurmurHash.
Added UniqueWindowController.
Bug fixes.
     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     id result = nil;
    32     if( target && target.target ) {
    33         [target retain];
    34         @try{
    35             [target setArgument: &param atIndex: 2];
    36             [target invoke];
    37             
    38             NSMethodSignature *sig = target.methodSignature;
    39             NSUInteger returnLength = sig.methodReturnLength;
    40             if( returnLength==0 ) {
    41                 result = nil; // void
    42             } else {
    43                 const char *returnType = sig.methodReturnType;
    44                 if( returnType[0]=='@' ) {
    45                     [target getReturnValue: &result];
    46                 } else {
    47                     UInt8 returnBuffer[returnLength];
    48                     [target getReturnValue: &returnBuffer];
    49                     result = [NSValue valueWithBytes: &returnBuffer objCType: returnType];
    50                 }
    51             }
    52         }@finally{
    53             [target release];
    54         }
    55     }
    56     return result;
    57 }