/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMObjectSingleton.h
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// 21// This file has been kept around for compatibility with apps relying on its macro, 22// but given how simple this is, there is not a compelling reason for any app to 23// use this macro. 24// 25// For a reasonable discussion of Objective-C singletons, see 26// http://eschatologist.net/blog/?p=178 27// 28// Sample usage: 29// 30// GTMOBJECT_SINGLETON_BOILERPLATE(SomeUsefulManager, sharedSomeUsefulManager) 31// (with no trailing semicolon) 32// 33 34#include <AvailabilityMacros.h> 35 36#if (defined(__IPHONE_7_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0)) \ 37 || (defined(MAC_OS_X_VERSION_10_9) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9)) 38#error "GTMOBJECT_SINGLETON_BOILERPLATE is deprecated; change this in your sources to a class method using dispatch_once." 39#endif 40 41#if NS_BLOCKS_AVAILABLE 42 43#define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \ 44+ (_object_name_ *)_shared_obj_name_ { \ 45 static _object_name_ *obj; \ 46 static dispatch_once_t onceToken; \ 47 dispatch_once(&onceToken, ^{ \ 48 obj = [[self alloc] init]; \ 49 }); \ 50 return obj; \ 51} 52 53#else 54 55#define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \ 56+ (_object_name_ *)_shared_obj_name_ { \ 57 static _object_name_ *obj; \ 58 if (obj == nil) { \ 59 obj = [[self alloc] init]; \ 60 } \ 61 return obj; \ 62} 63 64#endif // NS_BLOCKS_AVAILABLE