Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Commit

Permalink
Updating GHKit files
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Apr 11, 2009
1 parent 7f710e8 commit 02ef9c0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
8 changes: 7 additions & 1 deletion Libraries/GHKit/GHNSInvocation+Utils.h
Expand Up @@ -35,6 +35,7 @@
//
#define GHConvertVarArgs(object) \
NSMutableArray *arguments = [NSMutableArray array]; \
do { \
id arg; \
va_list args; \
if (object) { \
Expand All @@ -43,7 +44,8 @@ va_start(args, object); \
while ((arg = va_arg(args, id))) \
[arguments addObject:arg]; \
va_end(args); \
}
} \
} while(0);

@interface NSInvocation (GHUtils)

Expand All @@ -63,6 +65,8 @@ va_end(args); \

+ (id)gh_invokeWithTarget:(id)target selector:(SEL)selector arguments:(NSArray *)arguments;

+ (id)gh_invokeWithTarget:(id)target selector:(SEL)selector afterDelay:(NSTimeInterval)delay arguments:(NSArray *)arguments;

/*!
Invoke selector on target on main thread with multiple arguments.
Use [NSNull null] for nil arguments.
Expand All @@ -75,6 +79,8 @@ va_end(args); \

+ (void)gh_invokeTargetOnMainThread:(id)target selector:(SEL)selector waitUntilDone:(BOOL)waitUntilDone arguments:(NSArray *)arguments;

+ (void)gh_invokeTargetOnMainThread:(id)target selector:(SEL)selector waitUntilDone:(BOOL)waitUntilDone afterDelay:(NSTimeInterval)delay arguments:(NSArray *)arguments;

/*!
Create invocation with variable arguments.
Use [NSNull null] for nil arguments.
Expand Down
39 changes: 33 additions & 6 deletions Libraries/GHKit/GHNSInvocation+Utils.m
Expand Up @@ -38,14 +38,22 @@ + (id)gh_invokeWithTarget:(id)target selector:(SEL)selector withObjects:object,
}

+ (id)gh_invokeWithTarget:(id)target selector:(SEL)selector arguments:(NSArray *)arguments {
return [self gh_invokeWithTarget:target selector:selector afterDelay:-1 arguments:arguments];
}

+ (id)gh_invokeWithTarget:(id)target selector:(SEL)selector afterDelay:(NSTimeInterval)delay arguments:(NSArray *)arguments {
BOOL hasReturnValue = NO;
NSInvocation *invocation = [self gh_invocationWithTarget:target selector:selector hasReturnValue:&hasReturnValue arguments:arguments];
[invocation invoke];
if (delay >= 0) {
[invocation performSelector:@selector(invoke) withObject:nil afterDelay:0];
} else {
[invocation invoke];

if (hasReturnValue) {
id retval = NULL;
[invocation getReturnValue:&retval];
return retval;
if (hasReturnValue) {
id retval = NULL;
[invocation getReturnValue:&retval];
return retval;
}
}
return nil;
}
Expand All @@ -60,6 +68,25 @@ + (void)gh_invokeTargetOnMainThread:(id)target selector:(SEL)selector waitUntilD
[invocation gh_invokeOnMainThread:waitUntilDone];
}

+ (void)gh_invokeTargetOnMainThread:(id)target selector:(SEL)selector waitUntilDone:(BOOL)waitUntilDone afterDelay:(NSTimeInterval)delay arguments:(NSArray *)arguments {
NSInvocation *invocation = [self gh_invocationWithTarget:target selector:selector hasReturnValue:nil arguments:arguments];
if (delay >= 0) {
SEL selector = selector = @selector(gh_invokeOnMainThreadAndWaitUntilDone);
if (!waitUntilDone) selector = @selector(gh_invokeOnMainThread);
[invocation performSelector:selector withObject:nil afterDelay:delay];
} else {
[invocation gh_invokeOnMainThread:waitUntilDone];
}
}

- (void)gh_invokeOnMainThread {
[self gh_invokeOnMainThread:NO];
}

- (void)gh_invokeOnMainThreadAndWaitUntilDone {
[self gh_invokeOnMainThread:YES];
}

- (void)gh_invokeOnMainThread:(BOOL)waitUntilDone {
// Retain args, since we are invoking on a separate thread
if (![self argumentsRetained]) [self retainArguments];
Expand All @@ -70,7 +97,7 @@ + (NSInvocation *)gh_invocationWithTarget:target selector:(SEL)selector hasRetur
GHConvertVarArgs(object);
return [self gh_invocationWithTarget:target selector:selector hasReturnValue:hasReturnValue arguments:arguments];
}

+ (NSInvocation *)gh_invocationWithTarget:target selector:(SEL)selector hasReturnValue:(BOOL *)hasReturnValue arguments:(NSArray *)arguments {

if (![target respondsToSelector:selector])
Expand Down
7 changes: 7 additions & 0 deletions Libraries/GHKit/GHNSObject+Invocation.h
Expand Up @@ -57,6 +57,8 @@
*/
- (id)gh_performSelector:(SEL)selector withObjects:object, ...;

- (id)gh_performSelector:(SEL)selector afterDelay:(NSTimeInterval)delay withObjects:object, ...;

/*!
Invoke selector with arguments on main thread.
Does not wait until selector is finished.
Expand All @@ -76,4 +78,9 @@

- (void)gh_performSelector:(SEL)selector onMainThread:(BOOL)onMainThread waitUntilDone:(BOOL)waitUntilDone withObjects:object, ...;

- (void)gh_performSelector:(SEL)selector onMainThread:(BOOL)onMainThread waitUntilDone:(BOOL)waitUntilDone arguments:(NSArray *)arguments;

- (void)gh_performSelector:(SEL)selector onMainThread:(BOOL)onMainThread waitUntilDone:(BOOL)waitUntilDone
afterDelay:(NSTimeInterval)delay arguments:(NSArray *)arguments;

@end
22 changes: 17 additions & 5 deletions Libraries/GHKit/GHNSObject+Invocation.m
Expand Up @@ -5,9 +5,6 @@
// Created by Gabriel Handford on 1/18/09.
// Copyright 2009. All rights reserved.
//
// Created by Gabriel Handford on 1/17/09.
// Copyright 2009. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
Expand Down Expand Up @@ -53,6 +50,11 @@ - (id)gh_performSelector:(SEL)selector withObjects:object, ... {
return [NSInvocation gh_invokeWithTarget:self selector:selector arguments:arguments];
}

- (id)gh_performSelector:(SEL)selector afterDelay:(NSTimeInterval)delay withObjects:object, ... {
GHConvertVarArgs(object);
return [NSInvocation gh_invokeWithTarget:self selector:selector afterDelay:delay arguments:arguments];
}

- (void)gh_performSelectorOnMainThread:(SEL)selector withObjects:object, ... {
GHConvertVarArgs(object);
[NSInvocation gh_invokeTargetOnMainThread:self selector:selector waitUntilDone:NO arguments:arguments];
Expand All @@ -65,10 +67,20 @@ - (void)gh_performSelectorOnMainThread:(SEL)selector waitUntilDone:(BOOL)waitUnt

- (void)gh_performSelector:(SEL)selector onMainThread:(BOOL)onMainThread waitUntilDone:(BOOL)waitUntilDone withObjects:object, ... {
GHConvertVarArgs(object);
[self gh_performSelector:selector onMainThread:onMainThread waitUntilDone:waitUntilDone arguments:arguments];
}

- (void)gh_performSelector:(SEL)selector onMainThread:(BOOL)onMainThread waitUntilDone:(BOOL)waitUntilDone arguments:(NSArray *)arguments {
[self gh_performSelector:selector onMainThread:onMainThread waitUntilDone:waitUntilDone afterDelay:-1 arguments:arguments];
}

- (void)gh_performSelector:(SEL)selector onMainThread:(BOOL)onMainThread waitUntilDone:(BOOL)waitUntilDone
afterDelay:(NSTimeInterval)delay arguments:(NSArray *)arguments {

if (!onMainThread) {
[NSInvocation gh_invokeWithTarget:self selector:selector arguments:arguments];
[NSInvocation gh_invokeWithTarget:self selector:selector afterDelay:delay arguments:arguments];
} else {
[NSInvocation gh_invokeTargetOnMainThread:self selector:selector waitUntilDone:waitUntilDone arguments:arguments];
[NSInvocation gh_invokeTargetOnMainThread:self selector:selector waitUntilDone:waitUntilDone afterDelay:delay arguments:arguments];
}
}

Expand Down

0 comments on commit 02ef9c0

Please sign in to comment.