2 Copyright (c) 2011, Joachim Bengtsson
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 * Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 #import "SPInvocationGrabbing.h"
15 #pragma mark Invocation grabbing
16 @interface SPInvocationGrabber ()
17 @property (readwrite, retain, nonatomic) id object;
18 @property (readwrite, retain, nonatomic) NSInvocation *invocation;
22 @implementation SPInvocationGrabber
23 - (id)initWithObject:(id)obj;
25 return [self initWithObject:obj stacktraceSaving:YES];
28 -(id)initWithObject:(id)obj stacktraceSaving:(BOOL)saveStack;
41 self.invocation = nil;
44 @synthesize invocation = _invocation, object = _object;
46 @synthesize backgroundAfterForward, onMainAfterForward, waitUntilDone;
47 - (void)runInBackground;
49 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
59 - (void)forwardInvocation:(NSInvocation *)anInvocation {
60 [anInvocation retainArguments];
61 anInvocation.target = _object;
62 self.invocation = anInvocation;
64 if(backgroundAfterForward)
65 [NSThread detachNewThreadSelector:@selector(runInBackground) toTarget:self withObject:nil];
66 else if(onMainAfterForward)
67 [self performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:waitUntilDone];
69 - (NSMethodSignature *)methodSignatureForSelector:(SEL)inSelector {
70 NSMethodSignature *signature = [super methodSignatureForSelector:inSelector];
71 if (signature == NULL)
72 signature = [_object methodSignatureForSelector:inSelector];
83 @catch (NSException * e) {
84 NSLog(@"SPInvocationGrabber's target raised %@:\n\t%@\nInvocation was originally scheduled at:", e.name, e);
85 [self printBacktrace];
90 self.invocation = nil;
96 void *backtraceFrames[128];
97 frameCount = backtrace(&backtraceFrames[0], 128);
98 frameStrings = backtrace_symbols(&backtraceFrames[0], frameCount);
100 -(void)printBacktrace;
102 for(int x = 3; x < frameCount; x++) {
103 if(frameStrings[x] == NULL) { break; }
104 printf("%s\n", frameStrings[x]);
109 @implementation NSObject (SPInvocationGrabbing)
112 return [[[SPInvocationGrabber alloc] initWithObject:self] autorelease];
114 -(id)invokeAfter:(NSTimeInterval)delta;
116 id grabber = [self grab];
117 [NSTimer scheduledTimerWithTimeInterval:delta target:grabber selector:@selector(invoke) userInfo:nil repeats:NO];
122 return [self invokeAfter:0];
126 SPInvocationGrabber *grabber = [self grab];
127 grabber.backgroundAfterForward = YES;
130 -(id)onMainAsync:(BOOL)async;
132 SPInvocationGrabber *grabber = [self grab];
133 grabber.onMainAfterForward = YES;
134 grabber.waitUntilDone = !async;