| author | Zachary West <zacw@adium.im> |
| Sun Nov 01 14:04:11 2009 -0500 (2009-11-01) | |
| changeset 2848 | d88a4b7a70a8 |
| parent 2118 | 9fbe80565319 |
| child 3063 | 3efcc26ae847 |
| permissions | -rw-r--r-- |
| David@0 | 1 |
// |
| David@0 | 2 |
// AIObjectAdditions.m |
| David@0 | 3 |
// Adium |
| David@0 | 4 |
// |
| David@0 | 5 |
// Created by Colin Barrett on Mon Sep 22 2003. |
| David@0 | 6 |
// |
| David@0 | 7 |
|
| David@0 | 8 |
#import "AIObjectAdditions.h" |
| David@0 | 9 |
|
| David@0 | 10 |
// Clever addition by Jonathan Jansson found on cocoadev.com (http://www.cocoadev.com/index.pl?ThreadCommunication) |
| David@0 | 11 |
@implementation NSObject (RunLoopMessenger) |
| David@0 | 12 |
|
| David@0 | 13 |
//Included to allow uniform coding |
| David@0 | 14 |
- (void)mainPerformSelector:(SEL)aSelector |
| David@0 | 15 |
{ |
| David@0 | 16 |
[self mainPerformSelector:aSelector waitUntilDone:NO]; |
| David@0 | 17 |
} |
| David@0 | 18 |
//Included to allow uniform coding - wrapped for performSelectorOnMainThread:withObject:waitUntilDone: |
| David@0 | 19 |
- (void)mainPerformSelector:(SEL)aSelector waitUntilDone:(BOOL)flag |
| David@0 | 20 |
{ |
| David@0 | 21 |
[self performSelectorOnMainThread:aSelector withObject:nil waitUntilDone:flag]; |
| David@0 | 22 |
} |
| David@0 | 23 |
|
| David@0 | 24 |
- (id)mainPerformSelector:(SEL)aSelector returnValue:(BOOL)flag |
| David@0 | 25 |
{ |
| David@0 | 26 |
id returnValue; |
| David@0 | 27 |
|
| David@0 | 28 |
if (flag) { |
| David@0 | 29 |
NSInvocation *invocation; |
| David@0 | 30 |
|
| David@0 | 31 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 32 |
[invocation setSelector:aSelector]; |
| David@0 | 33 |
|
| David@0 | 34 |
[self performSelectorOnMainThread:@selector(handleInvocation:) |
| David@0 | 35 |
withObject:invocation |
| David@0 | 36 |
waitUntilDone:YES]; |
| David@0 | 37 |
|
| David@0 | 38 |
[invocation getReturnValue:&returnValue]; |
| David@0 | 39 |
|
| David@0 | 40 |
} else { |
| David@0 | 41 |
returnValue = nil; |
| David@0 | 42 |
[self performSelectorOnMainThread:aSelector |
| David@0 | 43 |
withObject:nil |
| David@0 | 44 |
waitUntilDone:NO]; |
| David@0 | 45 |
} |
| David@0 | 46 |
|
| David@0 | 47 |
return returnValue; |
| David@0 | 48 |
} |
| David@0 | 49 |
|
| David@0 | 50 |
//Included to allow uniform coding |
| David@0 | 51 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 |
| David@0 | 52 |
{ |
| David@0 | 53 |
[self mainPerformSelector:aSelector withObject:argument1 waitUntilDone:NO]; |
| David@0 | 54 |
} |
| David@0 | 55 |
|
| David@0 | 56 |
//Included to allow uniform coding - wrapped for performSelectorOnMainThread:withObject:waitUntilDone: |
| David@0 | 57 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 waitUntilDone:(BOOL)flag |
| David@0 | 58 |
{ |
| David@0 | 59 |
[self performSelectorOnMainThread:aSelector withObject:argument1 waitUntilDone:flag]; |
| David@0 | 60 |
} |
| David@0 | 61 |
|
| David@0 | 62 |
//Perform a selector on the main thread, optionally taking an argument, and return its return value |
| David@0 | 63 |
- (id)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 returnValue:(BOOL)flag |
| David@0 | 64 |
{ |
| David@0 | 65 |
id returnValue; |
| David@0 | 66 |
|
| David@0 | 67 |
if (flag) { |
| David@0 | 68 |
NSInvocation *invocation; |
| David@0 | 69 |
|
| David@0 | 70 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 71 |
[invocation setSelector:aSelector]; |
| David@0 | 72 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 73 |
|
| David@0 | 74 |
[self performSelectorOnMainThread:@selector(handleInvocation:) |
| David@0 | 75 |
withObject:invocation |
| David@0 | 76 |
waitUntilDone:YES]; |
| David@0 | 77 |
|
| David@0 | 78 |
[invocation getReturnValue:&returnValue]; |
| David@0 | 79 |
|
| David@0 | 80 |
} else { |
| David@0 | 81 |
returnValue = nil; |
| David@0 | 82 |
[self performSelectorOnMainThread:aSelector withObject:argument1 waitUntilDone:NO]; |
| David@0 | 83 |
} |
| David@0 | 84 |
|
| David@0 | 85 |
return returnValue; |
| David@0 | 86 |
} |
| David@0 | 87 |
|
| David@0 | 88 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 |
| David@0 | 89 |
{ |
| David@0 | 90 |
[self mainPerformSelector:aSelector withObject:argument1 withObject:argument2 waitUntilDone:NO]; |
| David@0 | 91 |
} |
| David@0 | 92 |
|
| David@0 | 93 |
//Perform a selector on the main thread, taking 0-2 arguments, and return its return value |
| David@0 | 94 |
- (id)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 returnValue:(BOOL)flag |
| David@0 | 95 |
{ |
| David@0 | 96 |
id returnValue; |
| David@0 | 97 |
|
| David@0 | 98 |
if (flag) { |
| David@0 | 99 |
NSInvocation *invocation; |
| David@0 | 100 |
|
| David@0 | 101 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 102 |
[invocation setSelector:aSelector]; |
| David@0 | 103 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 104 |
[invocation setArgument:&argument2 atIndex:3]; |
| David@0 | 105 |
|
| David@0 | 106 |
[self performSelectorOnMainThread:@selector(handleInvocation:) |
| David@0 | 107 |
withObject:invocation |
| David@0 | 108 |
waitUntilDone:YES]; |
| David@0 | 109 |
|
| David@0 | 110 |
[invocation getReturnValue:&returnValue]; |
| David@0 | 111 |
|
| David@0 | 112 |
} else { |
| David@0 | 113 |
returnValue = nil; |
| David@0 | 114 |
[self mainPerformSelector:aSelector withObject:argument1 withObject:argument2 waitUntilDone:NO]; |
| David@0 | 115 |
} |
| David@0 | 116 |
|
| David@0 | 117 |
return returnValue; |
| David@0 | 118 |
} |
| David@0 | 119 |
|
| David@0 | 120 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 waitUntilDone:(BOOL)flag |
| David@0 | 121 |
{ |
| David@0 | 122 |
NSInvocation *invocation; |
| David@0 | 123 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 124 |
|
| David@0 | 125 |
[invocation setSelector:aSelector]; |
| David@0 | 126 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 127 |
[invocation setArgument:&argument2 atIndex:3]; |
| David@0 | 128 |
[invocation retainArguments]; |
| David@0 | 129 |
|
| David@0 | 130 |
[self performSelectorOnMainThread:@selector(handleInvocation:) withObject:invocation waitUntilDone:flag]; |
| David@0 | 131 |
} |
| David@0 | 132 |
|
| David@0 | 133 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 |
| David@0 | 134 |
{ |
| David@0 | 135 |
[self mainPerformSelector:aSelector withObject:argument1 withObject:argument2 withObject:argument3 waitUntilDone:NO]; |
| David@0 | 136 |
} |
| David@0 | 137 |
//Perform a selector on the main thread, taking 0-3 arguments, and return its return value |
| David@0 | 138 |
- (id)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 returnValue:(BOOL)flag |
| David@0 | 139 |
{ |
| David@0 | 140 |
id returnValue; |
| David@0 | 141 |
|
| David@0 | 142 |
if (flag) { |
| David@0 | 143 |
NSInvocation *invocation; |
| David@0 | 144 |
|
| David@0 | 145 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 146 |
[invocation setSelector:aSelector]; |
| David@0 | 147 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 148 |
[invocation setArgument:&argument2 atIndex:3]; |
| David@0 | 149 |
[invocation setArgument:&argument3 atIndex:4]; |
| David@0 | 150 |
|
| David@0 | 151 |
[self performSelectorOnMainThread:@selector(handleInvocation:) |
| David@0 | 152 |
withObject:invocation |
| David@0 | 153 |
waitUntilDone:YES]; |
| David@0 | 154 |
|
| David@0 | 155 |
[invocation getReturnValue:&returnValue]; |
| David@0 | 156 |
|
| David@0 | 157 |
} else { |
| David@0 | 158 |
returnValue = nil; |
| David@0 | 159 |
[self mainPerformSelector:aSelector withObject:argument1 withObject:argument2 withObject:argument3 waitUntilDone:NO]; |
| David@0 | 160 |
} |
| David@0 | 161 |
|
| David@0 | 162 |
return returnValue; |
| David@0 | 163 |
} |
| David@0 | 164 |
|
| David@0 | 165 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 waitUntilDone:(BOOL)flag |
| David@0 | 166 |
{ |
| David@0 | 167 |
NSInvocation *invocation; |
| David@0 | 168 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 169 |
|
| David@0 | 170 |
[invocation setSelector:aSelector]; |
| David@0 | 171 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 172 |
[invocation setArgument:&argument2 atIndex:3]; |
| David@0 | 173 |
[invocation setArgument:&argument3 atIndex:4]; |
| David@0 | 174 |
[invocation retainArguments]; |
| David@0 | 175 |
|
| David@0 | 176 |
[self performSelectorOnMainThread:@selector(handleInvocation:) withObject:invocation waitUntilDone:flag]; |
| David@0 | 177 |
} |
| David@0 | 178 |
|
| David@0 | 179 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 withObject:(id)argument4 |
| David@0 | 180 |
{ |
| David@0 | 181 |
[self mainPerformSelector:aSelector withObject:argument1 withObject:argument2 withObject:argument3 withObject:argument4 waitUntilDone:NO]; |
| David@0 | 182 |
} |
| David@0 | 183 |
- (void)mainPerformSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 withObject:(id)argument4 waitUntilDone:(BOOL)flag |
| David@0 | 184 |
{ |
| David@0 | 185 |
NSInvocation *invocation; |
| David@0 | 186 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 187 |
|
| David@0 | 188 |
[invocation setSelector:aSelector]; |
| David@0 | 189 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 190 |
[invocation setArgument:&argument2 atIndex:3]; |
| David@0 | 191 |
[invocation setArgument:&argument3 atIndex:4]; |
| David@0 | 192 |
[invocation setArgument:&argument4 atIndex:5]; |
| David@0 | 193 |
[invocation retainArguments]; |
| David@0 | 194 |
|
| David@0 | 195 |
[self performSelectorOnMainThread:@selector(handleInvocation:) withObject:invocation waitUntilDone:flag]; |
| David@0 | 196 |
} |
| David@0 | 197 |
|
| David@0 | 198 |
//nil terminated |
| David@0 | 199 |
- (void)mainPerformSelector:(SEL)aSelector withObjects:(id)argument1, ... |
| David@0 | 200 |
{ |
| David@0 | 201 |
NSInvocation *invocation; |
| David@0 | 202 |
va_list args; |
| David@0 | 203 |
int index = 2; |
| David@0 | 204 |
|
| David@0 | 205 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 206 |
[invocation setSelector:aSelector]; |
| David@0 | 207 |
[invocation setArgument:&argument1 atIndex:index++]; |
| David@0 | 208 |
|
| David@0 | 209 |
va_start(args, argument1); |
| David@0 | 210 |
|
| David@0 | 211 |
id anArgument; |
| David@0 | 212 |
while ((anArgument = va_arg(args, id))) { |
| David@0 | 213 |
[invocation setArgument:&anArgument atIndex:index++]; |
| David@0 | 214 |
} |
| David@0 | 215 |
|
| David@0 | 216 |
va_end(args); |
| David@0 | 217 |
|
| David@0 | 218 |
[invocation retainArguments]; |
| David@0 | 219 |
|
| David@0 | 220 |
[self performSelectorOnMainThread:@selector(handleInvocation:) withObject:invocation waitUntilDone:NO]; |
| David@0 | 221 |
} |
| David@0 | 222 |
|
| David@0 | 223 |
- (void)performSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 afterDelay:(NSTimeInterval)delay |
| David@0 | 224 |
{ |
| David@0 | 225 |
NSInvocation *invocation; |
| David@0 | 226 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 227 |
|
| David@0 | 228 |
[invocation setSelector:aSelector]; |
| David@0 | 229 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 230 |
[invocation setArgument:&argument2 atIndex:3]; |
| David@0 | 231 |
[invocation retainArguments]; |
| David@0 | 232 |
|
| David@0 | 233 |
[self performSelector:@selector(handleInvocation:) withObject:invocation afterDelay:delay]; |
| David@0 | 234 |
} |
| David@0 | 235 |
|
| David@0 | 236 |
- (void)performSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 afterDelay:(NSTimeInterval)delay |
| David@0 | 237 |
{ |
| David@0 | 238 |
NSInvocation *invocation; |
| David@0 | 239 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| David@0 | 240 |
|
| David@0 | 241 |
[invocation setSelector:aSelector]; |
| David@0 | 242 |
[invocation setArgument:&argument1 atIndex:2]; |
| David@0 | 243 |
[invocation setArgument:&argument2 atIndex:3]; |
| David@0 | 244 |
[invocation setArgument:&argument3 atIndex:4]; |
| David@0 | 245 |
[invocation retainArguments]; |
| David@0 | 246 |
|
| David@0 | 247 |
[self performSelector:@selector(handleInvocation:) withObject:invocation afterDelay:delay]; |
| David@0 | 248 |
} |
| David@0 | 249 |
|
| wixardy@2118 | 250 |
- (void)performSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 |
| wixardy@2118 | 251 |
{ |
| wixardy@2118 | 252 |
NSInvocation *invocation; |
| wixardy@2118 | 253 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| wixardy@2118 | 254 |
|
| wixardy@2118 | 255 |
[invocation setSelector:aSelector]; |
| wixardy@2118 | 256 |
[invocation setArgument:&argument1 atIndex:2]; |
| wixardy@2118 | 257 |
[invocation setArgument:&argument2 atIndex:3]; |
| wixardy@2118 | 258 |
[invocation setArgument:&argument3 atIndex:4]; |
| wixardy@2118 | 259 |
[invocation retainArguments]; |
| wixardy@2118 | 260 |
|
| wixardy@2118 | 261 |
[self performSelector:@selector(handleInvocation:) withObject:invocation]; |
| wixardy@2118 | 262 |
} |
| wixardy@2118 | 263 |
|
| zacw@2848 | 264 |
- (void)performSelector:(SEL)aSelector withObject:(id)argument1 withObject:(id)argument2 withObject:(id)argument3 withObject:(id)argument4 afterDelay:(NSTimeInterval)delay |
| zacw@2848 | 265 |
{ |
| zacw@2848 | 266 |
NSInvocation *invocation; |
| zacw@2848 | 267 |
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]]; |
| zacw@2848 | 268 |
|
| zacw@2848 | 269 |
[invocation setSelector:aSelector]; |
| zacw@2848 | 270 |
[invocation setArgument:&argument1 atIndex:2]; |
| zacw@2848 | 271 |
[invocation setArgument:&argument2 atIndex:3]; |
| zacw@2848 | 272 |
[invocation setArgument:&argument3 atIndex:4]; |
| zacw@2848 | 273 |
[invocation setArgument:&argument4 atIndex:5]; |
| zacw@2848 | 274 |
[invocation retainArguments]; |
| zacw@2848 | 275 |
|
| zacw@2848 | 276 |
[self performSelector:@selector(handleInvocation:) withObject:invocation afterDelay:delay]; |
| zacw@2848 | 277 |
} |
| zacw@2848 | 278 |
|
| David@0 | 279 |
- (void)handleInvocation:(NSInvocation *)anInvocation |
| David@0 | 280 |
{ |
| David@0 | 281 |
[anInvocation invokeWithTarget:self]; |
| David@0 | 282 |
} |
| David@0 | 283 |
|
| David@0 | 284 |
@end |