/
Objection.m
75 lines (65 loc) · 2.2 KB
/
Objection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#import "Objection.h"
#import <pthread.h>
static NSMutableDictionary *gObjectionContext;
static pthread_mutex_t gObjectionMutex;
static ObjectionInjector *gGlobalInjector;
@implementation Objection
+ (ObjectionInjector *) createInjector:(ObjectionModule *)aModule {
pthread_mutex_lock(&gObjectionMutex);
@try {
return [[[ObjectionInjector alloc] initWithContext:gObjectionContext andModule:aModule] autorelease];
}
@finally {
pthread_mutex_unlock(&gObjectionMutex);
}
}
+ (ObjectionInjector *) createInjector {
pthread_mutex_lock(&gObjectionMutex);
@try {
return [[[ObjectionInjector alloc] initWithContext:gObjectionContext] autorelease];
}
@finally {
pthread_mutex_unlock(&gObjectionMutex);
}
}
+ (void)initialize {
if (self == [Objection class]) {
gObjectionContext = [[NSMutableDictionary alloc] init];
pthread_mutexattr_t mutexattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&gObjectionMutex, &mutexattr);
pthread_mutexattr_destroy(&mutexattr);
}
}
+ (void) registerClass:(Class)theClass lifeCycle:(ObjectionInstantiationRule)lifeCycle {
pthread_mutex_lock(&gObjectionMutex);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (lifeCycle != ObjectionInstantiationRuleSingleton && lifeCycle != ObjectionInstantiationRuleEverytime) {
@throw [NSException exceptionWithName:@"ObjectionInjectorException" reason:@"Invalid Instantiation Rule" userInfo:nil];
}
if (theClass && [gObjectionContext objectForKey:NSStringFromClass(theClass)] == nil) {
[gObjectionContext setObject:[ObjectionEntry entryWithClass:theClass lifeCycle:lifeCycle] forKey:NSStringFromClass(theClass)];
}
[pool drain];
pthread_mutex_unlock(&gObjectionMutex);
}
+ (void) reset {
pthread_mutex_lock(&gObjectionMutex);
[gObjectionContext removeAllObjects];
pthread_mutex_unlock(&gObjectionMutex);
}
+ (void)setGlobalInjector:(ObjectionInjector *)anInjector {
if (gGlobalInjector != anInjector) {
[gGlobalInjector release];
gGlobalInjector = [anInjector retain];
}
}
+ (ObjectionInjector *) globalInjector {
@try {
return gGlobalInjector;
}
@finally {
}
}
@end