lib/DDInvocationGrabber.m
author Dave Dribin <dave@dribin.org>
Tue Jan 27 16:05:23 2009 -0600 (2009-01-27)
changeset 158 14076243f425
parent 48 7df54d322b9c
child 160 05b4f1a8852c
permissions -rw-r--r--
Update copyright header
     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     _waitUntilDone = NO;
    76     
    77     return self;
    78 }
    79 
    80 - (void)dealloc
    81 {
    82     [self setTarget:NULL];
    83     [self setInvocation:NULL];
    84     //
    85     [super dealloc];
    86 }
    87 
    88 #pragma mark -
    89 
    90 - (id)target
    91 {
    92     return _target;
    93 }
    94 
    95 - (void)setTarget:(id)inTarget
    96 {
    97     if (_target != inTarget)
    98 	{
    99         [_target autorelease];
   100         _target = [inTarget retain];
   101 	}
   102 }
   103 
   104 - (NSInvocation *)invocation
   105 {
   106     return _invocation;
   107 }
   108 
   109 - (void)setInvocation:(NSInvocation *)inInvocation
   110 {
   111     if (_invocation != inInvocation)
   112 	{
   113         [_invocation autorelease];
   114         _invocation = [inInvocation retain];
   115 	}
   116 }
   117 
   118 - (BOOL)forwardInvokesOnMainThread;
   119 {
   120     return _forwardInvokesOnMainThread;
   121 }
   122 
   123 - (void)setForwardInvokesOnMainThread:(BOOL)forwardInvokesOnMainThread;
   124 {
   125     _forwardInvokesOnMainThread = forwardInvokesOnMainThread;
   126 }
   127 
   128 - (BOOL)waitUntilDone;
   129 {
   130     return _waitUntilDone;
   131 }
   132 
   133 - (void)setWaitUntilDone:(BOOL)waitUntilDone;
   134 {
   135     _waitUntilDone = waitUntilDone;
   136 }
   137 
   138 #pragma mark -
   139 
   140 - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
   141 {
   142     return [[self target] methodSignatureForSelector:selector];
   143 }
   144 
   145 - (void)forwardInvocation:(NSInvocation *)ioInvocation
   146 {
   147     [ioInvocation setTarget:[self target]];
   148     [self setInvocation:ioInvocation];
   149     if (_forwardInvokesOnMainThread)
   150     {
   151         if (!_waitUntilDone)
   152             [_invocation retainArguments];
   153         [_invocation performSelectorOnMainThread:@selector(invoke)
   154                                       withObject:nil
   155                                    waitUntilDone:_waitUntilDone];
   156     }
   157 }
   158 
   159 @end
   160 
   161 #pragma mark -
   162 
   163 @implementation DDInvocationGrabber (DDnvocationGrabber_Conveniences)
   164 
   165 - (id)prepareWithInvocationTarget:(id)inTarget
   166 {
   167     [self setTarget:inTarget];
   168     return(self);
   169 }
   170 
   171 @end