forked from davekeck/EBFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEBSingleton.m
More file actions
65 lines (52 loc) · 1.56 KB
/
Copy pathEBSingleton.m
File metadata and controls
65 lines (52 loc) · 1.56 KB
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
#import "EBSingleton.h"
#import "EBAssert.h"
#import "EBUtilities.h"
@implementation EBSingleton
static NSMutableDictionary *gEntries = nil;
#pragma mark - Creation -
+ (void)initialize
{
static dispatch_once_t initToken;
dispatch_once(&initToken,
^{
gEntries = [NSMutableDictionary new];
});
}
+ (instancetype)sharedInstance
{
return [self sharedInstanceForClass: self];
}
+ (instancetype)sharedInstanceForClass: (Class)cls
{
@synchronized(gEntries)
{
id result = [gEntries objectForKey: (id)cls];
if (!result)
{
result = [[cls alloc] initWithCallToSuper: YES];
EBAssertOrRecover(result, return nil);
/* We set the entry in our map *before* we callout to -initSingleton, so that attempts to acquire the shared
instance of 'cls' within -initSingleton return a value. */
[gEntries setObject: result forKey: (id)cls];
[result initSingleton];
}
return result;
}
}
- (instancetype)init
{
/* Verify that -init hasn't been overridden by a subclass (EBSingleton subclasses must use -initSingleton.) */
EBAssertOrBail([EBSingleton instanceMethodForSelector: @selector(init)] == [[self class] instanceMethodForSelector: @selector(init)]);
return [self initWithCallToSuper: NO];
}
- (instancetype)initWithCallToSuper: (BOOL)callToSuper
{
if (callToSuper)
return [super init];
else
return [[self class] sharedInstance];
}
- (void)initSingleton
{
}
@end