]> git.sesse.net Git - vlc/blob - modules/gui/macosx/SPInvocationGrabbing.m
Useless #includes
[vlc] / modules / gui / macosx / SPInvocationGrabbing.m
1 /*
2  Copyright (c) 2011, Joachim Bengtsson
3  All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
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.
8
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.
10  */
11
12 #import "SPInvocationGrabbing.h"
13 #import <execinfo.h>
14
15 #pragma mark Invocation grabbing
16 @interface SPInvocationGrabber ()
17 @property (readwrite, retain, nonatomic) id object;
18 @property (readwrite, retain, nonatomic) NSInvocation *invocation;
19
20 @end
21
22 @implementation SPInvocationGrabber
23 - (id)initWithObject:(id)obj;
24 {
25     return [self initWithObject:obj stacktraceSaving:YES];
26 }
27
28 -(id)initWithObject:(id)obj stacktraceSaving:(BOOL)saveStack;
29 {
30     self.object = obj;
31     if(saveStack)
32         [self saveBacktrace];
33
34     return self;
35 }
36
37 -(void)dealloc;
38 {
39     free(frameStrings);
40     self.object = nil;
41     self.invocation = nil;
42
43     [super dealloc];
44 }
45 @synthesize invocation = _invocation, object = _object;
46
47 @synthesize backgroundAfterForward, onMainAfterForward, waitUntilDone;
48 - (void)runInBackground;
49 {
50     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
51     @try {
52         [self invoke];
53     }
54     @finally {
55         [pool drain];
56     }
57 }
58
59
60 - (void)forwardInvocation:(NSInvocation *)anInvocation {
61     [anInvocation retainArguments];
62     anInvocation.target = _object;
63     self.invocation = anInvocation;
64
65     if(backgroundAfterForward)
66         [NSThread detachNewThreadSelector:@selector(runInBackground) toTarget:self withObject:nil];
67     else if(onMainAfterForward)
68         [self performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:waitUntilDone];
69 }
70
71 - (NSMethodSignature *)methodSignatureForSelector:(SEL)inSelector {
72     NSMethodSignature *signature = [super methodSignatureForSelector:inSelector];
73     if (signature == NULL)
74         signature = [_object methodSignatureForSelector:inSelector];
75
76     return signature;
77 }
78
79 - (void)invoke;
80 {
81     @try {
82         [_invocation invoke];
83     }
84
85     @catch (NSException * e) {
86         NSLog(@"SPInvocationGrabber's target raised %@:\n\t%@\nInvocation was originally scheduled at:", e.name, e);
87         [self printBacktrace];
88         printf("\n");
89         [e raise];
90     }
91
92     self.invocation = nil;
93     self.object = nil;
94 }
95
96 -(void)saveBacktrace;
97 {
98     void *backtraceFrames[128];
99     frameCount = backtrace(&backtraceFrames[0], 128);
100     frameStrings = backtrace_symbols(&backtraceFrames[0], frameCount);
101 }
102
103 -(void)printBacktrace;
104 {
105     for(int x = 3; x < frameCount; x++) {
106         if(frameStrings[x] == NULL) { break; }
107         printf("%s\n", frameStrings[x]);
108     }
109 }
110 @end
111
112 @implementation NSObject (SPInvocationGrabbing)
113 -(id)grab;
114 {
115     return [[[SPInvocationGrabber alloc] initWithObject:self] autorelease];
116 }
117
118 -(id)invokeAfter:(NSTimeInterval)delta;
119 {
120     id grabber = [self grab];
121     [NSTimer scheduledTimerWithTimeInterval:delta target:grabber selector:@selector(invoke) userInfo:nil repeats:NO];
122     return grabber;
123 }
124
125 - (id)nextRunloop;
126 {
127     return [self invokeAfter:0];
128 }
129
130 -(id)inBackground;
131 {
132     SPInvocationGrabber *grabber = [self grab];
133     grabber.backgroundAfterForward = YES;
134     return grabber;
135 }
136
137 -(id)onMainAsync:(BOOL)async;
138 {
139     SPInvocationGrabber *grabber = [self grab];
140     grabber.onMainAfterForward = YES;
141     grabber.waitUntilDone = !async;
142     return grabber;
143 }
144
145 @end