PageRenderTime 68ms CodeModel.GetById 62ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/BlocksKit/NSCache+BlocksKit.m

http://github.com/zwaldowski/BlocksKit
Objective C | 57 lines | 36 code | 17 blank | 4 comment | 5 complexity | 2fa90e7a36d640c213f3de936bbe5368 MD5 | raw file
 1//
 2//  NSCache+BlocksKit.m
 3//  BlocksKit
 4//
 5
 6#import "NSCache+BlocksKit.h"
 7#import "A2BlockDelegate+BlocksKit.h"
 8
 9#pragma mark Custom delegate
10
11@interface A2DynamicNSCacheDelegate : A2DynamicDelegate
12
13@end
14
15@implementation A2DynamicNSCacheDelegate
16
17- (void)cache:(NSCache *)cache willEvictObject:(id)obj {
18	id realDelegate = self.realDelegate;
19	if (realDelegate && [realDelegate respondsToSelector:@selector(cache:willEvictObject:)])
20		[realDelegate cache:cache willEvictObject:obj];
21
22	void (^orig)(NSCache *, id) = [self blockImplementationForMethod:_cmd];
23	if (orig)
24		 orig(cache, obj);
25}
26
27@end
28
29#pragma mark Category
30
31@implementation NSCache (BlocksKit)
32
33@dynamic willEvictBlock;
34
35+ (void)load {
36	@autoreleasepool {
37		[self registerDynamicDelegate];
38		[self linkCategoryBlockProperty:@"willEvictBlock" withDelegateMethod:@selector(cache:willEvictObject:)];
39	}
40}
41
42#pragma mark Methods
43
44- (id)objectForKey:(id)key withGetter:(BKReturnBlock)block {
45	id object = [self objectForKey:key];
46	if (object)
47		return object;
48	
49	if (block) {
50		object = block();
51		[self setObject:object forKey:key];
52	}
53	
54	return object;
55}
56
57@end