Created
February 7, 2014 14:39
-
-
Save jamesnesfield/8863750 to your computer and use it in GitHub Desktop.
singleton macro iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from Urban Airship iOS SDK "1.1.2" | |
#define SINGLETON_INTERFACE(CLASSNAME) \ | |
+ (CLASSNAME*)shared; \ | |
#define SINGLETON_IMPLEMENTATION(CLASSNAME) \ | |
\ | |
static CLASSNAME* g_shared##CLASSNAME = nil; \ | |
\ | |
+ (CLASSNAME*)shared \ | |
{ \ | |
static dispatch_once_t sharedOncePredicate##CLASSNAME; \ | |
\ | |
dispatch_once(&sharedOncePredicate##CLASSNAME, ^{ \ | |
g_shared##CLASSNAME = [[self alloc] init]; \ | |
}); \ | |
return g_shared##CLASSNAME; \ | |
} \ | |
\ | |
+ (id)allocWithZone:(NSZone*)zone \ | |
{ \ | |
static dispatch_once_t allocOncePredicate##CLASSNAME; \ | |
dispatch_once(&allocOncePredicate##CLASSNAME, ^{ \ | |
if (g_shared##CLASSNAME == nil) { \ | |
g_shared##CLASSNAME = [super allocWithZone:zone]; \ | |
} \ | |
}); \ | |
return g_shared##CLASSNAME; \ | |
} \ | |
\ | |
- (id)copyWithZone:(NSZone*)zone \ | |
{ \ | |
return self; \ | |
} \ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment