lib/DDInvocationGrabber.m
author Dave Dribin <dave@dribin.org>
Thu Feb 12 17:20:13 2009 -0600 (12 months ago)
changeset 163 702f3ad9dc6f
parent 158 14076243f425
permissions -rw-r--r--
Fix method misspelling
     1 /*
     2  * Copyright (c) 2007-2009 Dave Dribin
     3  * 
     4  * Permission is hereby granted, free of charge, to any person
     5  * obtaining a copy of this software and associated documentation
     6  * files (the "Software"), to deal in the Software without
     7  * restriction, including without limitation the rights to use, copy,
     8  * modify, merge, publish, distribute, sublicense, and/or sell copies
     9  * of the Software, and to permit persons to whom the Software is
    10  * furnished to do so, subject to the following conditions:
    11  * 
    12  * The above copyright notice and this permission notice shall be
    13  * included in all copies or substantial portions of the Software.
    14  *
    15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
    19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
    20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22  * SOFTWARE.
    23  */
    24 
    25 
    26 /*
    27  *  This class is based on CInvocationGrabber:
    28  *
    29  *  Copyright (c) 2007, Toxic Software
    30  *  All rights reserved.
    31  *  Redistribution and use in source and binary forms, with or without
    32  *  modification, are permitted provided that the following conditions are
    33  *  met:
    34  *  
    35  *  * Redistributions of source code must retain the above copyright notice,
    36  *  this list of conditions and the following disclaimer.
    37  *  
    38  *  * Redistributions in binary form must reproduce the above copyright
    39  *  notice, this list of conditions and the following disclaimer in the
    40  *  documentation and/or other materials provided with the distribution.
    41  *  
    42  *  * Neither the name of the Toxic Software nor the names of its
    43  *  contributors may be used to endorse or promote products derived from
    44  *  this software without specific prior written permission.
    45  *  
    46  *  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    47  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    48  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    49  *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
    50  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    51  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    52  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    53  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    54  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    55  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    56  *  THE POSSIBILITY OF SUCH DAMAGE.
    57  *
    58  */
    59 
    60 #import "DDInvocationGrabber.h"
    61 
    62 
    63 @implementation DDInvocationGrabber
    64 
    65 + (id)invocationGrabber
    66 {
    67     return([[[self alloc] init] autorelease]);
    68 }
    69 
    70 - (id)init
    71 {
    72     _target = nil;
    73     _invocation = nil;
    74     _forwardInvokesOnMainThread = NO;
    75     _invocationThread = nil;
    76     _waitUntilDone = NO;
    77     
    78     return self;
    79 }
    80 
    81 - (void)dealloc
    82 {
    83     [self setTarget:NULL];
    84     [self setInvocation:NULL];
    85     //
    86     [super dealloc];
    87 }
    88 
    89 #pragma mark -
    90 
    91 - (id)target
    92 {
    93     return _target;
    94 }
    95 
    96 - (void)setTarget:(id)inTarget
    97 {
    98     if (_target != inTarget)
    99 	{
   100         [_target autorelease];
   101         _target = [inTarget retain];
   102 	}
   103 }
   104 
   105 - (NSInvocation *)invocation
   106 {
   107     return _invocation;
   108 }
   109 
   110 - (void)setInvocation:(NSInvocation *)inInvocation
   111 {
   112     if (_invocation != inInvocation)
   113 	{
   114         [_invocation autorelease];
   115         _invocation = [inInvocation retain];
   116 	}
   117 }
   118 
   119 - (BOOL)forwardInvokesOnMainThread;
   120 {
   121     return _forwardInvokesOnMainThread;
   122 }
   123 
   124 - (void)setForwardInvokesOnMainThread:(BOOL)forwardInvokesOnMainThread;
   125 {
   126     _forwardInvokesOnMainThread = forwardInvokesOnMainThread;
   127 }
   128 
   129 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
   130 - (void)setInvokesOnThread:(NSThread *)thread;
   131 {
   132     _invocationThread = thread;
   133 }
   134 #endif
   135 
   136 - (BOOL)waitUntilDone;
   137 {
   138     return _waitUntilDone;
   139 }
   140 
   141 - (void)setWaitUntilDone:(BOOL)waitUntilDone;
   142 {
   143     _waitUntilDone = waitUntilDone;
   144 }
   145 
   146 #pragma mark -
   147 
   148 - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
   149 {
   150     return [[self target] methodSignatureForSelector:selector];
   151 }
   152 
   153 - (void)forwardInvocation:(NSInvocation *)ioInvocation
   154 {
   155     [ioInvocation setTarget:[self target]];
   156     [self setInvocation:ioInvocation];
   157     BOOL invokeOnOtherThread = _forwardInvokesOnMainThread || (_invocationThread != nil);
   158     if (invokeOnOtherThread && !_waitUntilDone)
   159     {
   160         [_invocation retainArguments];
   161     }
   162     
   163     if (_forwardInvokesOnMainThread)
   164     {
   165         [_invocation performSelectorOnMainThread:@selector(invoke)
   166                                       withObject:nil
   167                                    waitUntilDone:_waitUntilDone];
   168     }
   169     
   170 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
   171     if (_invocationThread != nil)
   172     {
   173         [_invocation performSelector:@selector(invoke)
   174                             onThread:_invocationThread
   175                           withObject:nil
   176                        waitUntilDone:_waitUntilDone];
   177     }
   178 #endif
   179 }
   180 
   181 @end
   182 
   183 #pragma mark -
   184 
   185 @implementation DDInvocationGrabber (DDnvocationGrabber_Conveniences)
   186 
   187 - (id)prepareWithInvocationTarget:(id)inTarget
   188 {
   189     [self setTarget:inTarget];
   190     return(self);
   191 }
   192 
   193 @end