2 * Copyright (c) 2007-2009 Dave Dribin
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
27 * This class is based on CInvocationGrabber:
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
35 * * Redistributions of source code must retain the above copyright notice,
36 * this list of conditions and the following disclaimer.
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.
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.
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.
60 #import "DDInvocationGrabber.h"
63 @implementation DDInvocationGrabber
65 + (id)invocationGrabber
67 return([[[self alloc] init] autorelease]);
74 _forwardInvokesOnMainThread = NO;
75 _invocationThread = nil;
83 [self setTarget:NULL];
84 [self setInvocation:NULL];
96 - (void)setTarget:(id)inTarget
98 if (_target != inTarget)
100 [_target autorelease];
101 _target = [inTarget retain];
105 - (NSInvocation *)invocation
110 - (void)setInvocation:(NSInvocation *)inInvocation
112 if (_invocation != inInvocation)
114 [_invocation autorelease];
115 _invocation = [inInvocation retain];
119 - (BOOL)forwardInvokesOnMainThread;
121 return _forwardInvokesOnMainThread;
124 - (void)setForwardInvokesOnMainThread:(BOOL)forwardInvokesOnMainThread;
126 _forwardInvokesOnMainThread = forwardInvokesOnMainThread;
129 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
130 - (void)setInvokesOnThread:(NSThread *)thread;
132 _invocationThread = thread;
136 - (BOOL)waitUntilDone;
138 return _waitUntilDone;
141 - (void)setWaitUntilDone:(BOOL)waitUntilDone;
143 _waitUntilDone = waitUntilDone;
148 - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
150 return [[self target] methodSignatureForSelector:selector];
153 - (void)forwardInvocation:(NSInvocation *)ioInvocation
155 [ioInvocation setTarget:[self target]];
156 [self setInvocation:ioInvocation];
157 BOOL invokeOnOtherThread = _forwardInvokesOnMainThread || (_invocationThread != nil);
158 if (invokeOnOtherThread && !_waitUntilDone)
160 [_invocation retainArguments];
163 if (_forwardInvokesOnMainThread)
165 [_invocation performSelectorOnMainThread:@selector(invoke)
167 waitUntilDone:_waitUntilDone];
170 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
171 if (_invocationThread != nil)
173 [_invocation performSelector:@selector(invoke)
174 onThread:_invocationThread
176 waitUntilDone:_waitUntilDone];
185 @implementation DDInvocationGrabber (DDnvocationGrabber_Conveniences)
187 - (id)prepareWithInvocationTarget:(id)inTarget
189 [self setTarget:inTarget];