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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
| //
// SynthesizeSingleton.h
//
// Modified by Karl Stenerud starting 16/04/2010.
// - Moved the swizzle code to allocWithZone so that non-default init methods may be
// used to initialize the singleton.
// - Added "lesser" singleton which allows other instances besides sharedInstance to be created.
// - Added guard ifndef so that this file can be used in multiple library distributions.
// - Made singleton variable name class-specific so that it can be used on multiple classes
// within the same compilation module.
//
// Modified by CJ Hanson on 26/02/2010.
// This version of Matt's code uses method_setImplementaiton() to dynamically
// replace the +sharedInstance method with one that does not use @synchronized
//
// Based on code by Matt Gallagher from CocoaWithLove
//
// Created by Matt Gallagher on 20/10/08.
// Copyright 2009 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file without charge in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#ifndef SYNTHESIZE_SINGLETON_FOR_CLASS
#import <objc/runtime.h>
#pragma mark -
#pragma mark Singleton
/* Synthesize Singleton For Class
*
* Creates a singleton interface for the specified class with the following methods:
*
* + (MyClass*) sharedInstance;
* + (void) purgeSharedInstance;
*
* Calling sharedInstance will instantiate the class and swizzle some methods to ensure
* that only a single instance ever exists.
* Calling purgeSharedInstance will destroy the shared instance and return the swizzled
* methods to their former selves.
*
*
* Usage:
*
* MyClass.h:
* ========================================
* #import "SynthesizeSingleton.h"
*
* @interface MyClass: SomeSuperclass
* {
* ...
* }
* SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(MyClass);
*
* @end
* ========================================
*
*
* MyClass.m:
* ========================================
* #import "MyClass.h"
*
* // This line is optional. Use it if you've enabled GCC_WARN_UNDECLARED_SELECTOR
* SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(MyClass);
*
* @implementation MyClass
*
* SYNTHESIZE_SINGLETON_FOR_CLASS(MyClass);
*
* ...
*
* @end
* ========================================
*
*
* Note: Calling alloc manually will also initialize the singleton, so you
* can call a more complex init routine to initialize the singleton like so:
*
* [[MyClass alloc] initWithParam:firstParam secondParam:secondParam];
*
* Just be sure to make such a call BEFORE you call "sharedInstance" in
* your program.
*/
#define SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(SS_CLASSNAME) \
\
+ (SS_CLASSNAME*) sharedInstance; \
+ (void) purgeSharedInstance;
#define SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(SS_CLASSNAME) \
@interface SS_CLASSNAME (SynthesizeSingletonPrivate) \
- (NSUInteger)retainCountDoNothing; \
- (NSUInteger)retainCountDoSomething; \
- (void)releaseDoNothing; \
- (void)releaseDoSomething; \
- (id)autoreleaseDoNothing; \
- (id)autoreleaseDoSomething; \
@end
#define SYNTHESIZE_SINGLETON_FOR_CLASS(SS_CLASSNAME) \
\
static volatile SS_CLASSNAME* _##SS_CLASSNAME##_sharedInstance = nil; \
\
+ (volatile SS_CLASSNAME*) sharedInstanceNoSynch \
{ \
return (volatile SS_CLASSNAME*) _##SS_CLASSNAME##_sharedInstance; \
} \
\
+ (volatile SS_CLASSNAME*) sharedInstanceSynch \
{ \
@synchronized(self) \
{ \
if(nil == _##SS_CLASSNAME##_sharedInstance) \
{ \
_##SS_CLASSNAME##_sharedInstance = [[self alloc] init]; \
} \
} \
return (volatile SS_CLASSNAME*) _##SS_CLASSNAME##_sharedInstance; \
} \
\
+ (volatile SS_CLASSNAME*) sharedInstance \
{ \
return (volatile SS_CLASSNAME*)[self sharedInstanceSynch]; \
} \
\
+ (id)allocWithZone:(NSZone*) zone \
{ \
@synchronized(self) \
{ \
if (nil == _##SS_CLASSNAME##_sharedInstance) \
{ \
_##SS_CLASSNAME##_sharedInstance = [super allocWithZone:zone]; \
if(nil != _##SS_CLASSNAME##_sharedInstance) \
{ \
Method newSharedInstanceMethod = class_getClassMethod(self, @selector(sharedInstanceNoSynch)); \
method_setImplementation(class_getClassMethod(self, @selector(sharedInstance)), method_getImplementation(newSharedInstanceMethod)); \
method_setImplementation(class_getInstanceMethod(self, @selector(retainCount)), class_getMethodImplementation(self, @selector(retainCountDoNothing))); \
method_setImplementation(class_getInstanceMethod(self, @selector(release)), class_getMethodImplementation(self, @selector(releaseDoNothing))); \
method_setImplementation(class_getInstanceMethod(self, @selector(autorelease)), class_getMethodImplementation(self, @selector(autoreleaseDoNothing))); \
} \
} \
} \
return (id)_##SS_CLASSNAME##_sharedInstance; \
} \
\
+ (void)purgeSharedInstance \
{ \
@synchronized(self) \
{ \
if(nil != _##SS_CLASSNAME##_sharedInstance) \
{ \
Method newSharedInstanceMethod = class_getClassMethod(self, @selector(sharedInstanceSynch)); \
method_setImplementation(class_getClassMethod(self, @selector(sharedInstance)), method_getImplementation(newSharedInstanceMethod)); \
method_setImplementation(class_getInstanceMethod(self, @selector(retainCount)), class_getMethodImplementation(self, @selector(retainCountDoSomething))); \
method_setImplementation(class_getInstanceMethod(self, @selector(release)), class_getMethodImplementation(self, @selector(releaseDoSomething))); \
method_setImplementation(class_getInstanceMethod(self, @selector(autorelease)), class_getMethodImplementation(self, @selector(autoreleaseDoSomething))); \
[_##SS_CLASSNAME##_sharedInstance release]; \
_##SS_CLASSNAME##_sharedInstance = nil; \
} \
} \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
NSAssert1(1==0, @"SynthesizeSingleton: %@ ERROR: -(NSUInteger)retainCount method did not get swizzled.", self); \
return NSUIntegerMax; \
} \
\
- (NSUInteger)retainCountDoNothing \
{ \
return NSUIntegerMax; \
} \
- (NSUInteger)retainCountDoSomething \
{ \
return [super retainCount]; \
}\
\
- (oneway void)release \
{ \
NSAssert1(1==0, @"SynthesizeSingleton: %@ ERROR: -(void)release method did not get swizzled.", self); \
} \
\
- (void)releaseDoNothing{} \
\
- (void)releaseDoSomething \
{ \
@synchronized(self) \
{ \
[super release]; \
} \
} \
\
- (id)autorelease \
{ \
NSAssert1(1==0, @"SynthesizeSingleton: %@ ERROR: -(id)autorelease method did not get swizzled.", self); \
return self; \
} \
\
- (id)autoreleaseDoNothing \
{ \
return self; \
} \
\
- (id)autoreleaseDoSomething \
{ \
return [super autorelease]; \
}
#pragma mark -
#pragma mark Lesser Singleton
/* A lesser singleton has a shared instance, but can also be instantiated on its own.
*
* For a lesser singleton, you still use SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(),
* but use SYNTHESIZE_LESSER_SINGLETON_FOR_CLASS() in the implementation file.
* You must specify which creation methods are to initialize the shared instance
* (besides "sharedInstance") via CALL_LESSER_SINGLETON_INIT_METHOD()
*
* Example:
*
* MyClass.h:
* ========================================
* #import "SynthesizeSingleton.h"
*
* @interface MyClass: SomeSuperclass
* {
* int value;
* ...
* }
* SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(MyClass);
*
* + (void) initSharedInstanceWithValue:(int) value;
*
* - (id) initWithValue:(int) value;
*
* @end
* ========================================
*
*
* MyClass.m:
* ========================================
* #import "MyClass.h"
*
* // This line is optional. Use it if you've enabled GCC_WARN_UNDECLARED_SELECTOR
* SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(MyClass);
*
* @implementation MyClass
*
* SYNTHESIZE_LESSER_SINGLETON_FOR_CLASS(MyClass);
*
* + (void) initSharedInstanceWithValue:(int) value
* {
* CALL_LESSER_SINGLETON_INIT_METHOD(MyClass, initWithValue:value);
* }
*
* ...
*
* @end
* ========================================
*
*
* Note: CALL_LESSER_SINGLETON_INIT_METHOD() will not work if your
* init call contains commas. If you need commas (such as for varargs),
* or other more complex initialization, use the PRE and POST macros:
*
* + (void) initSharedInstanceComplex
* {
* CALL_LESSER_SINGLETON_INIT_METHOD_PRE(MyClass);
*
* int firstNumber = [self getFirstNumberSomehow];
* _sharedInstance = [[self alloc] initWithValues:firstNumber, 2, 3, 4, -1];
*
* CALL_LESSER_SINGLETON_INIT_METHOD_POST(MyClass);
* }
*/
#define SYNTHESIZE_LESSER_SINGLETON_FOR_CLASS(SS_CLASSNAME) \
\
static volatile SS_CLASSNAME* _##SS_CLASSNAME##_sharedInstance = nil; \
\
+ (SS_CLASSNAME*) sharedInstanceNoSynch \
{ \
return (SS_CLASSNAME*) _##SS_CLASSNAME##_sharedInstance; \
} \
\
+ (SS_CLASSNAME*) sharedInstanceSynch \
{ \
@synchronized(self) \
{ \
if(nil == _##SS_CLASSNAME##_sharedInstance) \
{ \
_##SS_CLASSNAME##_sharedInstance = [[self alloc] init]; \
if(_##SS_CLASSNAME##_sharedInstance) \
{ \
Method newSharedInstanceMethod = class_getClassMethod(self, @selector(sharedInstanceNoSynch)); \
method_setImplementation(class_getClassMethod(self, @selector(sharedInstance)), method_getImplementation(newSharedInstanceMethod)); \
} \
} \
} \
return (SS_CLASSNAME*) _##SS_CLASSNAME##_sharedInstance; \
} \
\
+ (volatile SS_CLASSNAME*)sharedInstance \
{ \
return (volatile SS_CLASSNAME*) [self sharedInstanceSynch]; \
} \
\
+ (void)purgeSharedInstance \
{ \
@synchronized(self) \
{ \
Method newSharedInstanceMethod = class_getClassMethod(self, @selector(sharedInstanceSynch)); \
method_setImplementation(class_getClassMethod(self, @selector(sharedInstance)), method_getImplementation(newSharedInstanceMethod)); \
[_##SS_CLASSNAME##_sharedInstance release]; \
_##SS_CLASSNAME##_sharedInstance = nil; \
} \
}
#define CALL_LESSER_SINGLETON_INIT_METHOD_PRE(SS_CLASSNAME) \
@synchronized(self) \
{ \
if(nil == _##SS_CLASSNAME##_sharedInstance) \
{
#define CALL_LESSER_SINGLETON_INIT_METHOD_POST(SS_CLASSNAME) \
if(_##SS_CLASSNAME##_sharedInstance) \
{ \
Method newSharedInstanceMethod = class_getClassMethod(self, @selector(sharedInstanceNoSynch)); \
method_setImplementation(class_getClassMethod(self, @selector(sharedInstance)), method_getImplementation(newSharedInstanceMethod)); \
} \
} \
}
#define CALL_LESSER_SINGLETON_INIT_METHOD(SS_CLASSNAME,__INIT_CALL__) \
CALL_LESSER_SINGLETON_INIT_METHOD_PRE(SS_CLASSNAME); \
_##SS_CLASSNAME##_sharedInstance = [[self alloc] __INIT_CALL__]; \
CALL_LESSER_SINGLETON_INIT_METHOD_POST(SS_CLASSNAME)
#endif /* SYNTHESIZE_SINGLETON_FOR_CLASS */
|