SynthesizeSingleton definition header


/ Published in: Objective C
Save to your folder(s)

This class is used to turn an object into a singleton.

Usage : SYNTHESIZE_SINGLETON_FOR_CLASS(ClassName);

Place in your implementation.

thank you Matt Gallagher


Copy this code and paste it in your HTML
  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. #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
  16. \
  17. static classname *shared##classname = nil; \
  18. \
  19. + (classname *)shared##classname \
  20. { \
  21. @synchronized(self) \
  22. { \
  23. if (shared##classname == nil) \
  24. { \
  25. shared##classname = [[self alloc] init]; \
  26. } \
  27. } \
  28. \
  29. return shared##classname; \
  30. } \
  31. \
  32. + (id)allocWithZone:(NSZone *)zone \
  33. { \
  34. @synchronized(self) \
  35. { \
  36. if (shared##classname == nil) \
  37. { \
  38. shared##classname = [super allocWithZone:zone]; \
  39. return shared##classname; \
  40. } \
  41. } \
  42. \
  43. return nil; \
  44. } \
  45. \
  46. - (id)copyWithZone:(NSZone *)zone \
  47. { \
  48. return self; \
  49. } \
  50. \
  51. - (id)retain \
  52. { \
  53. return self; \
  54. } \
  55. \
  56. - (NSUInteger)retainCount \
  57. { \
  58. return NSUIntegerMax; \
  59. } \
  60. \
  61. - (void)release \
  62. { \
  63. } \
  64. \
  65. - (id)autorelease \
  66. { \
  67. return self; \
  68. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.