-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathCKSingleton.h
71 lines (70 loc) · 1.34 KB
/
CKSingleton.h
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
#define $shared(Klass) [Klass shared##Klass]
#ifdef ALLOW_ALLOC_INIT_FOR_SINGLETONS
#define $singleton(Klass)\
\
static Klass *shared##Klass = nil;\
\
+ (Klass *)shared##Klass {\
@synchronized(self) {\
if(shared##Klass == nil) {\
shared##Klass = [[super allocWithZone:NULL] initSingleton];\
}\
}\
return shared##Klass;\
}\
\
- (id)init {\
if((self = [super init])) {\
[self initSingleton];\
}\
return self;\
}
#else
#define $singleton(Klass)\
\
static Klass *shared##Klass = nil;\
\
+ (Klass *)shared##Klass {\
@synchronized(self) {\
if(shared##Klass == nil) {\
shared##Klass = [[super allocWithZone:NULL] init];\
}\
}\
return shared##Klass;\
}\
\
+ (id)allocWithZone:(NSZone *)zone {\
return [[self shared##Klass] retain];\
}\
\
- (id)copyWithZone:(NSZone *)zone {\
return self;\
}\
\
- (id)retain {\
return self;\
}\
\
- (NSUInteger)retainCount {\
return NSUIntegerMax;\
}\
\
- (void)release {}\
\
- (id)autorelease {\
return self;\
}\
\
+ (BOOL)isShared##Klass##Present {\
return shared##Klass != nil;\
}\
\
- (id)init {\
if(![[self class] isShared##Klass##Present]) {\
if((self = [super init])) {\
[self initSingleton];\
}\
}\
return self;\
}
#endif