Skip to content

Commit

Permalink
fix: use wrong deployment target guards
Browse files Browse the repository at this point in the history
use CFMutableDictionary instead of NSMapTable for iOS
  • Loading branch information
Kentzo committed Aug 24, 2010
1 parent 1774ed9 commit dc67e53
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
4 changes: 1 addition & 3 deletions MACompoundFuture.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#if TARGET_OS_MAC && !TARGET_IPHONE_SIMULATOR


id MACompoundBackgroundFuture(id (^block)(void));
id MACompoundLazyFuture(id (^block)(void));

#define MACompoundBackgroundFuture(...) ((__typeof((__VA_ARGS__)()))MACompoundBackgroundFuture((id (^)(void))(__VA_ARGS__)))
#define MACompoundLazyFuture(...) ((__typeof((__VA_ARGS__)()))MACompoundLazyFuture((id (^)(void))(__VA_ARGS__)))

#endif
4 changes: 0 additions & 4 deletions MACompoundFuture.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#if TARGET_OS_MAC && !TARGET_IPHONE_SIMULATOR

#import "MAFuture.h"
#import "MAFutureInternal.h"
#import "MAMethodSignatureCache.h"
Expand Down Expand Up @@ -187,5 +185,3 @@ id MACompoundLazyFuture(id (^block)(void))

return [compoundFuture autorelease];
}

#endif
8 changes: 4 additions & 4 deletions MAFuture.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ id MALazyFuture(id (^block)(void));
#define MALazyFuture(...) ((__typeof((__VA_ARGS__)()))MALazyFuture((id (^)(void))(__VA_ARGS__)))


#ifdef __IPHONE_4_0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0

id IKMemoryAwareFuture(id (^block)(void));
id IKMemoryAwareFutureCreate(id (^block)(void));
Expand All @@ -18,5 +18,5 @@ BOOL IKMemoryAwareFutureIsObserving(id future);
#define IKMemoryAwareFuture(...)((__typeof((__VA_ARGS__)()))IKMemoryAwareFuture((id (^)(void))(__VA_ARGS__)))
#define IKMemoryAwareFutureCreate(...)((__typeof((__VA_ARGS__)()))IKMemoryAwareFutureCreate((id (^)(void))(__VA_ARGS__)))

#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
#endif // __IPHONE_4_0
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED
18 changes: 11 additions & 7 deletions MAFuture.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ @implementation _MASimpleFuture
- (id)forwardingTargetForSelector: (SEL)sel
{
LOG(@"%p forwardingTargetForSelector: %@, resolving future", self, NSStringFromSelector(sel));
#if ENABLE_LOGGING
id resolvedFuture = [self resolveFuture];
if (resolvedFuture == nil) {
LOG(@"WARNING: [%@ resolveFuture] has returned nil. You must avoid to return nil objects from the block", NSStringFromClass(isa));
}
return resolvedFuture;
#else
return [self resolveFuture];
#endif
}

#if TARGET_OS_MAC && !TARGET_IPHONE_SIMULATOR

- (NSMethodSignature *)methodSignatureForSelector: (SEL)sel
{
return [[MAMethodSignatureCache sharedCache] cachedMethodSignatureForSelector: sel];
}

#endif

- (void)forwardInvocation: (NSInvocation *)inv
{
// this gets hit if the future resolves to nil
Expand Down Expand Up @@ -114,8 +118,8 @@ id MALazyFuture(id (^block)(void))
return [[[_MALazyBlockFuture alloc] initWithBlock: block] autorelease];
}

#ifdef __IPHONE_4_0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0

@implementation _IKMemoryAwareFuture
@dynamic isObserving;
Expand Down Expand Up @@ -201,5 +205,5 @@ BOOL IKMemoryAwareFutureIsObserving(id future) {
return [future isObserving];
}

#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
#endif // __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_4_0
#endif // __IPHONE_4_0
8 changes: 4 additions & 4 deletions MAFutureInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

@end

#ifdef __IPHONE_4_0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0

@interface _IKMemoryAwareFuture : _MALazyBlockFuture {
BOOL isObserving;
Expand All @@ -30,5 +30,5 @@

@end

#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
#endif // __IPHONE_4_0
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED
7 changes: 4 additions & 3 deletions MAMethodSignatureCache.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#import <Foundation/Foundation.h>

#if TARGET_OS_MAC && !TARGET_IPHONE_SIMULATOR

@interface MAMethodSignatureCache : NSObject
{
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
CFMutableDictionaryRef _cache;
#else
NSMapTable *_cache;
#endif
NSRecursiveLock *_lock;
}

+ (MAMethodSignatureCache *)sharedCache;
- (NSMethodSignature *)cachedMethodSignatureForSelector: (SEL)sel;

@end

#endif
23 changes: 19 additions & 4 deletions MAMethodSignatureCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#import "MAMethodSignatureCache.h"

#if TARGET_OS_MAC && !TARGET_IPHONE_SIMULATOR

@interface NSRecursiveLock (BlockAdditions)

Expand Down Expand Up @@ -36,10 +35,14 @@ - (id)init
{
if((self = [super init]))
{
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
_cache = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);
#else
_cache = [[NSMapTable alloc]
initWithKeyOptions: NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality
valueOptions: NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality
capacity: 0];
#endif
_lock = [[NSRecursiveLock alloc] init];
[[NSNotificationCenter defaultCenter]
addObserver: self
Expand All @@ -52,7 +55,13 @@ - (id)init

- (void)_clearCache
{
[_lock ma_do: ^{ [_cache removeAllObjects]; }];
[_lock ma_do: ^{
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
CFDictionaryRemoveAllValues(_cache);
#else
[_cache removeAllObjects];
#endif
}];
}

- (NSMethodSignature *)_searchAllClassesForSignature: (SEL)sel
Expand Down Expand Up @@ -96,13 +105,21 @@ - (NSMethodSignature *)cachedMethodSignatureForSelector: (SEL)sel
{
__block NSMethodSignature *sig = nil;
[_lock ma_do: ^{
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
sig = CFDictionaryGetValue(_cache, sel);
#else
sig = [_cache objectForKey: (id)sel];
#endif
if(!sig)
{
sig = [self _searchAllClassesForSignature: sel];
if(!sig)
sig = (id)[NSNull null];
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
CFDictionarySetValue(_cache, sel, sig);
#else
[_cache setObject: sig forKey: (id)sel];
#endif
}
}];
if(sig == (id)[NSNull null])
Expand All @@ -111,5 +128,3 @@ - (NSMethodSignature *)cachedMethodSignatureForSelector: (SEL)sel
}

@end

#endif

0 comments on commit dc67e53

Please sign in to comment.