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