-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMacroX.h
119 lines (100 loc) · 2.79 KB
/
MacroX.h
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
//
// MacroX.h
//
// Copyright (c) 2014 Anthony Shoumikhin. All rights reserved under MIT license.
// mailto:[email protected]
//
#define _QUOTE_(__text__) #__text__
/**
TODO(message)
A macro to mark a line of code with a "TODO" prefixed compiler warning.
*/
#define TODO(_text_) _Pragma(_QUOTE_(message("TODO: "_text_)))
/**
FIXME(message)
A macro to mark a line of code with a "FIXME" prefixed compiler warning.
*/
#define FIXME(_text_) _Pragma(_QUOTE_(message("FIXME: "_text_)))
/**
SHOW_ALERT(title, message, delegate, cancelButtonTitle, ...)
A macro to create and show a UIAlertView with the given title, message,
deleagate and arbitrary number of buttons with specified titles.
*/
#define SHOW_ALERT(_title_, _message_, _delegate_, _cancelButtonTitle_, ...) \
({ \
UIAlertView *_; \
[_ = [UIAlertView.alloc initWithTitle:(_title_) message:(_message_) delegate:(_delegate_) cancelButtonTitle:(_cancelButtonTitle_) otherButtonTitles:__VA_ARGS__, nil] show], _; \
})
/**
SYNTHESIZE_STATIC_PROPERTY(type, name, ...)
A macro to rapidly implement a read-only static property of a given type and name.
*/
#define SYNTHESIZE_STATIC_PROPERTY(_type_, _name_, ...) \
+ (_type_)_name_ \
{ \
static _type_ _name_; \
static dispatch_once_t once; \
\
dispatch_once(&once, \
^ \
{ \
_type_ (^evaluate)() = ^{ do { __VA_ARGS__ } while(0); }; \
_name_ = evaluate(); \
}); \
\
return _name_; \
}
/**
SYNTHESIZE_SINGLETON_FOR_CLASS(classname)
A macro to synthesize singleton boilerplate code for a given class.
Place it under the @implementation section of the class.
A single instance by default is accessible via `shared` method.
At the first time it calls a default `init` constructor and creates an
instance. Each next time the same instance is returned.
*/
#import <objc/runtime.h>
#if __has_feature(objc_arc)
#define _SYNTHESIZE_SINGLETON_RETAIN_METHODS_
#else
#define _SYNTHESIZE_SINGLETON_RETAIN_METHODS_ \
- (instancetype)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return NSUIntegerMax; \
} \
\
- (oneway void)release {} \
\
- (instancetype)autorelease \
{ \
return self; \
}
#endif
#define _SYNTHESIZE_SINGLETON_FOR_CLASS_WITH_ACCESSOR_(_classname_, _accessor_) \
\
+ (instancetype)_accessor_ \
{ \
static _classname_ *_accessor_; \
static dispatch_once_t once; \
\
dispatch_once(&once, ^{ _accessor_ = [[super allocWithZone:nil] init]; }); \
\
return _accessor_; \
} \
\
+ (instancetype)allocWithZone:(NSZone *)zone \
{ \
return self._accessor_; \
} \
\
- (instancetype)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
\
_SYNTHESIZE_SINGLETON_RETAIN_METHODS_
#define SYNTHESIZE_SINGLETON_FOR_CLASS(_classname_) _SYNTHESIZE_SINGLETON_FOR_CLASS_WITH_ACCESSOR_(_classname_, shared)