aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMObjectSingleton.h
blob: 4763b68733363fd3062fb50bc532573bb1d37cb5 (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
65
66
67
68
69
70
71
//
//  GTMObjectSingleton.h
//  Macro to implement methods 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.
//

#import "GTMDefines.h"

/// This macro implements the various methods needed to make a safe singleton.
//
/// This Singleton pattern was taken from:
/// http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html
///
/// Sample usage:
///
/// GTMOBJECT_SINGLETON_BOILERPLATE(SomeUsefulManager, sharedSomeUsefulManager)
/// (with no trailing semicolon)
///
#define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \
static _object_name_ *z##_shared_obj_name_ = nil;  \
+ (_object_name_ *)_shared_obj_name_ {             \
  @synchronized(self) {                            \
    if (z##_shared_obj_name_ == nil) {             \
      /* Note that 'self' may not be the same as _object_name_ */                               \
      /* first assignment done in allocWithZone but we must reassign in case init fails */      \
      z##_shared_obj_name_ = [[self alloc] init];                                               \
      _GTMDevAssert((z##_shared_obj_name_ != nil), @"didn't catch singleton allocation");       \
    }                                              \
  }                                                \
  return z##_shared_obj_name_;                     \
}                                                  \
+ (id)allocWithZone:(NSZone *)zone {               \
  @synchronized(self) {                            \
    if (z##_shared_obj_name_ == nil) {             \
      z##_shared_obj_name_ = [super allocWithZone:zone]; \
      return z##_shared_obj_name_;                 \
    }                                              \
  }                                                \
                                                   \
  /* We can't return the shared instance, because it's been init'd */ \
  _GTMDevAssert(NO, @"use the singleton API, not alloc+init");        \
  return nil;                                      \
}                                                  \
- (id)retain {                                     \
  return self;                                     \
}                                                  \
- (NSUInteger)retainCount {                        \
  return NSUIntegerMax;                            \
}                                                  \
- (void)release {                                  \
}                                                  \
- (id)autorelease {                                \
  return self;                                     \
}                                                  \
- (id)copyWithZone:(NSZone *)zone {                \
  return self;                                     \
}                                                  \