From 62fc71463d6f0aacc01348848af1265752a876c4 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Wed, 17 Jun 2009 06:42:06 +0000 Subject: [PATCH 1/1] 20 minutes of typing. --- UICaboodle/{Internals.h => UCInternal.h} | 0 UICaboodle/{UICaboodle.h => UCPlatform.h} | 3 ++ UICaboodle/UCString.h | 16 ++++++ UICaboodle/UCYield.h | 64 +++++++++++++++++++++++ 4 files changed, 83 insertions(+) rename UICaboodle/{Internals.h => UCInternal.h} (100%) rename UICaboodle/{UICaboodle.h => UCPlatform.h} (93%) create mode 100644 UICaboodle/UCYield.h diff --git a/UICaboodle/Internals.h b/UICaboodle/UCInternal.h similarity index 100% rename from UICaboodle/Internals.h rename to UICaboodle/UCInternal.h diff --git a/UICaboodle/UICaboodle.h b/UICaboodle/UCPlatform.h similarity index 93% rename from UICaboodle/UICaboodle.h rename to UICaboodle/UCPlatform.h index f9a4eb3e..0940406c 100644 --- a/UICaboodle/UICaboodle.h +++ b/UICaboodle/UCPlatform.h @@ -44,3 +44,6 @@ while (false) #define _packed \ __attribute__((packed)) + +//#define _finline __attribute__((force_inline)) +#define _finline inline diff --git a/UICaboodle/UCString.h b/UICaboodle/UCString.h index 77ee6a1b..d776728a 100644 --- a/UICaboodle/UCString.h +++ b/UICaboodle/UCString.h @@ -3,8 +3,14 @@ #import +@interface NSString (UIKit) +- (NSString *) stringByAddingPercentEscapes; +- (NSString *) stringByReplacingCharacter:(unsigned short)arg0 withCharacter:(unsigned short)arg1; +@end + @interface NSString (UICaboodle) + (NSString *) stringWithDataSize:(double)size; +- (NSString *) stringByAddingPercentEscapesIncludingReserved; @end @implementation NSString (UICaboodle) @@ -21,6 +27,16 @@ return [NSString stringWithFormat:@"%.1f%s", size, powers_[power]]; } +- (NSString *) stringByAddingPercentEscapesIncludingReserved { + return [(id)CFURLCreateStringByAddingPercentEscapes( + kCFAllocatorDefault, + (CFStringRef) self, + NULL, + CFSTR(";/?:@&=+$,"), + kCFStringEncodingUTF8 + ) autorelease]; +} + @end #endif/*UICABOODLE_UCSTRING_H*/ diff --git a/UICaboodle/UCYield.h b/UICaboodle/UCYield.h new file mode 100644 index 00000000..dc75f8f4 --- /dev/null +++ b/UICaboodle/UCYield.h @@ -0,0 +1,64 @@ +@interface NSObject (UICaboodle) +- (id) yieldToSelector:(SEL)selector withObject:(id)object; +- (id) yieldToSelector:(SEL)selector; +@end + +@implementation NSObject (UICaboodle) + +- (void) doNothing { +} + +- (void) _yieldToContext:(NSMutableArray *)context { _pooled + SEL selector(reinterpret_cast([[context objectAtIndex:0] pointerValue])); + id object([[context objectAtIndex:1] nonretainedObjectValue]); + volatile bool &stopped(*reinterpret_cast([[context objectAtIndex:2] pointerValue])); + + /* XXX: deal with exceptions */ + id value([self performSelector:selector withObject:object]); + + NSMethodSignature *signature([self methodSignatureForSelector:selector]); + [context removeAllObjects]; + if ([signature methodReturnLength] != 0 && value != nil) + [context addObject:value]; + + stopped = true; + + [self + performSelectorOnMainThread:@selector(doNothing) + withObject:nil + waitUntilDone:NO + ]; +} + +- (id) yieldToSelector:(SEL)selector withObject:(id)object { + /*return [self performSelector:selector withObject:object];*/ + + volatile bool stopped(false); + + NSMutableArray *context([NSMutableArray arrayWithObjects: + [NSValue valueWithPointer:selector], + [NSValue valueWithNonretainedObject:object], + [NSValue valueWithPointer:const_cast(&stopped)], + nil]); + + NSThread *thread([[[NSThread alloc] + initWithTarget:self + selector:@selector(_yieldToContext:) + object:context + ] autorelease]); + + [thread start]; + + NSRunLoop *loop([NSRunLoop currentRunLoop]); + NSDate *future([NSDate distantFuture]); + + while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]); + + return [context count] == 0 ? nil : [context objectAtIndex:0]; +} + +- (id) yieldToSelector:(SEL)selector { + return [self yieldToSelector:selector withObject:nil]; +} + +@end -- 2.45.2