Skip to content

Instantly share code, notes, and snippets.

@Vanson
Forked from alexlee002/Singleton_Template.h
Last active January 17, 2017 03:34
Show Gist options
  • Select an option

  • Save Vanson/e6d6ffc7248135b5dbf1 to your computer and use it in GitHub Desktop.

Select an option

Save Vanson/e6d6ffc7248135b5dbf1 to your computer and use it in GitHub Desktop.

Revisions

  1. @alexlee002 alexlee002 renamed this gist Apr 13, 2015. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion gistfile1.m → Singleton_Template.h
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,10 @@
    //
    // Singleton_Template.h
    //
    // Created by Alex Lee on 3/11/15.
    //


    #undef AS_SINGLETON
    #define AS_SINGLETON \
    + (instancetype)sharedInstance; \
    @@ -8,7 +15,12 @@ + (void)destroy;
    # define SYNTHESIZE_SINGLETON \
    static id __singleton_instance__ = nil; \
    + (instancetype)sharedInstance { \
    return [[self alloc] init]; \
    @synchronized(self) { \
    if (__singleton_instance__) { \
    return __singleton_instance__; \
    } \
    } \
    return [[self alloc] init]; \
    } \
    \
    + (instancetype)allocWithZone:(struct _NSZone *)zone { \
  2. @alexlee002 alexlee002 created this gist Mar 11, 2015.
    72 changes: 72 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #undef AS_SINGLETON
    #define AS_SINGLETON \
    + (instancetype)sharedInstance; \
    + (void)destroy;

    #if __has_feature(objc_arc)
    # undef SYNTHESIZE_SINGLETON
    # define SYNTHESIZE_SINGLETON \
    static id __singleton_instance__ = nil; \
    + (instancetype)sharedInstance { \
    return [[self alloc] init]; \
    } \
    \
    + (instancetype)allocWithZone:(struct _NSZone *)zone { \
    static dispatch_once_t onceToken; \
    dispatch_once( &onceToken, ^{ __singleton_instance__ = [super allocWithZone:zone]; } ); \
    return __singleton_instance__; \
    } \
    \
    + (instancetype)alloc { \
    return [self allocWithZone:NULL]; \
    } \
    \
    + (instancetype)new { \
    return [self allocWithZone:NULL]; \
    } \
    \
    - (id)copy { return self; } \
    - (id)mutableCopy { return self; } \
    \
    + (void)destroy { \
    __singleton_instance__ = nil; \
    }
    #else
    # undef SYNTHESIZE_SINGLETON
    # define SYNTHESIZE_SINGLETON \
    static id __singleton_instance__ = nil; \
    + (instancetype)sharedInstance { \
    return [[self alloc] init]; \
    } \
    \
    + (instancetype)allocWithZone:(struct _NSZone *)zone { \
    static dispatch_once_t onceToken; \
    dispatch_once( &onceToken, ^{ __singleton_instance__ = [super allocWithZone:zone]; } ); \
    return __singleton_instance__; \
    } \
    \
    + (instancetype)alloc { \
    return [self allocWithZone:NULL]; \
    } \
    \
    + (instancetype)new { \
    return [self allocWithZone:NULL]; \
    } \
    \
    - (id)copy { return self; } \
    - (id)mutableCopy { return self; } \
    \
    + (id)copyWithZone:(struct _NSZone *) { return self; } \
    + (id)mutableCopyWithZone:(struct _NSZone *) { return self; } \
    \
    - (instancetype)retain { return self; } \
    - (oneway void)release {} \
    - (instancetype)autorelease { return self; }\
    - (NSUInteger)retainCount { return NSUIntegerMax; } \
    \
    + (void)destroy { \
    [__singleton_instance__ dealloc]; \
    __singleton_instance__ = nil; \
    } \

    #endif