/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMObjectSingleton.h

http://macfuse.googlecode.com/ · C++ Header · 64 lines · 25 code · 8 blank · 31 comment · 5 complexity · d4991a173da8eb84cb5a7d28ea1d646a MD5 · raw file

  1. //
  2. // GTMObjectSingleton.h
  3. // Macro to implement a creation method for a singleton
  4. //
  5. // Copyright 2005-2008 Google Inc.
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. // use this file except in compliance with the License. You may obtain a copy
  9. // of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. //
  19. //
  20. // This file has been kept around for compatibility with apps relying on its macro,
  21. // but given how simple this is, there is not a compelling reason for any app to
  22. // use this macro.
  23. //
  24. // For a reasonable discussion of Objective-C singletons, see
  25. // http://eschatologist.net/blog/?p=178
  26. //
  27. // Sample usage:
  28. //
  29. // GTMOBJECT_SINGLETON_BOILERPLATE(SomeUsefulManager, sharedSomeUsefulManager)
  30. // (with no trailing semicolon)
  31. //
  32. #include <AvailabilityMacros.h>
  33. #if (defined(__IPHONE_7_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0)) \
  34. || (defined(MAC_OS_X_VERSION_10_9) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9))
  35. #error "GTMOBJECT_SINGLETON_BOILERPLATE is deprecated; change this in your sources to a class method using dispatch_once."
  36. #endif
  37. #if NS_BLOCKS_AVAILABLE
  38. #define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \
  39. + (_object_name_ *)_shared_obj_name_ { \
  40. static _object_name_ *obj; \
  41. static dispatch_once_t onceToken; \
  42. dispatch_once(&onceToken, ^{ \
  43. obj = [[self alloc] init]; \
  44. }); \
  45. return obj; \
  46. }
  47. #else
  48. #define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \
  49. + (_object_name_ *)_shared_obj_name_ { \
  50. static _object_name_ *obj; \
  51. if (obj == nil) { \
  52. obj = [[self alloc] init]; \
  53. } \
  54. return obj; \
  55. }
  56. #endif // NS_BLOCKS_AVAILABLE