Advertisement
Guest User

Singleton Macro

a guest
Apr 1st, 2011
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  SynthesizeSingleton.h
  3. //  CocoaWithLove
  4. //
  5. //  Created by Matt Gallagher on 20/10/08.
  6. //  Copyright 2009 Matt Gallagher. All rights reserved.
  7. //
  8. //  Permission is given to use this source code file without charge in any
  9. //  project, commercial or otherwise, entirely at your risk, with the condition
  10. //  that any redistribution (in part or whole) of source code must retain
  11. //  this copyright and permission notice. Attribution in compiled projects is
  12. //  appreciated but not required.
  13. //
  14.  
  15. //  Macro obtained from
  16. //  http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
  17.  
  18. #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
  19.  \
  20. static classname *shared##classname = nil; \
  21.  \
  22. + (classname *)shared##classname \
  23. { \
  24.     @synchronized(self) \
  25.     { \
  26.         if (shared##classname == nil) \
  27.         { \
  28.             shared##classname = [[self alloc] init]; \
  29.         } \
  30.     } \
  31.      \
  32.     return shared##classname; \
  33. } \
  34.  \
  35. + (id)allocWithZone:(NSZone *)zone \
  36. { \
  37.     @synchronized(self) \
  38.     { \
  39.         if (shared##classname == nil) \
  40.         { \
  41.             shared##classname = [super allocWithZone:zone]; \
  42.             return shared##classname; \
  43.         } \
  44.     } \
  45.      \
  46.     return nil; \
  47. } \
  48.  \
  49. - (id)copyWithZone:(NSZone *)zone \
  50. { \
  51.     return self; \
  52. } \
  53.  \
  54. - (id)retain \
  55. { \
  56.     return self; \
  57. } \
  58.  \
  59. - (NSUInteger)retainCount \
  60. { \
  61.     return NSUIntegerMax; \
  62. } \
  63.  \
  64. - (void)release \
  65. { \
  66. } \
  67.  \
  68. - (id)autorelease \
  69. { \
  70.     return self; \
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement