RQSingleton.h 721 B

12345678910111213141516171819202122232425
  1. #import <Foundation/Foundation.h>
  2. #undef singleton
  3. #define singleton( __class ) \
  4. property (nonatomic, readonly) __class * sharedInstance; \
  5. - (__class *)sharedInstance; \
  6. + (__class *)sharedInstance;
  7. #undef def_singleton
  8. #define def_singleton( __class ) \
  9. dynamic sharedInstance; \
  10. - (__class *)sharedInstance \
  11. { \
  12. return [__class sharedInstance]; \
  13. } \
  14. + (__class *)sharedInstance \
  15. { \
  16. static dispatch_once_t once; \
  17. static __strong id __singleton__ = nil; \
  18. dispatch_once( &once, ^{ __singleton__ = [[self alloc] init]; } ); \
  19. return __singleton__; \
  20. }
  21. @interface NSObject(RQSingleton)
  22. @end