aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMObjectSingleton.h
blob: 09326527fb1074eedf71b1c9af9aaeaaa0ad7bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
//  GTMObjectSingleton.h
//  Macro to implement a creation method for a singleton
//
//  Copyright 2005-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

//
// This file has been kept around for compatibility with apps relying on its macro,
// but given how simple this is, there is not a compelling reason for any app to
// use this macro.
//
//  For a reasonable discussion of Objective-C singletons, see
//  http://eschatologist.net/blog/?p=178
//
// Sample usage:
//
// GTMOBJECT_SINGLETON_BOILERPLATE(SomeUsefulManager, sharedSomeUsefulManager)
// (with no trailing semicolon)
//

#include <AvailabilityMacros.h>

#if (defined(__IPHONE_7_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0)) \
    || (defined(MAC_OS_X_VERSION_10_9) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9))
#error "GTMOBJECT_SINGLETON_BOILERPLATE is deprecated; change this in your sources to a class method using dispatch_once."
#endif

#if NS_BLOCKS_AVAILABLE

#define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \
+ (_object_name_ *)_shared_obj_name_ {     \
  static _object_name_ *obj;               \
  static dispatch_once_t onceToken;        \
  dispatch_once(&onceToken, ^{             \
     obj = [[self alloc] init];            \
  });                                      \
  return obj;                              \
}

#else

#define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \
+ (_object_name_ *)_shared_obj_name_ {     \
  static _object_name_ *obj;               \
  if (obj == nil) {                        \
    obj = [[self alloc] init];             \
  }                                        \
  return obj;                              \
}

#endif  // NS_BLOCKS_AVAILABLE