Posted By


nesium on 11/26/08

Tagged


Statistics


Viewed 799 times
Favorited by 0 user(s)

Synthesize singleton


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //
  2. // SynthesizeSingleton.h
  3. //
  4. // Created by Matt Gallagher on 20/10/08.
  5. //
  6.  
  7. #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname)
  8.  
  9. static classname *shared##classname = nil;
  10.  
  11. + (classname *)shared##classname
  12. {
  13. @synchronized(self)
  14. {
  15. if (shared##classname == nil)
  16. {
  17. [[self alloc] init];
  18. }
  19. }
  20.  
  21. return shared##classname;
  22. }
  23.  
  24. + (id)allocWithZone:(NSZone *)zone
  25. {
  26. @synchronized(self)
  27. {
  28. if (shared##classname == nil)
  29. {
  30. shared##classname = [super allocWithZone:zone];
  31. return shared##classname;
  32. }
  33. }
  34.  
  35. return nil;
  36. }
  37.  
  38. - (id)copyWithZone:(NSZone *)zone
  39. {
  40. return self;
  41. }
  42.  
  43. - (id)retain
  44. {
  45. return self;
  46. }
  47.  
  48. - (NSUInteger)retainCount
  49. {
  50. return NSUIntegerMax;
  51. }
  52.  
  53. - (void)release
  54. {
  55. }
  56.  
  57. - (id)autorelease
  58. {
  59. return self;
  60. }
  61.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.