forked from steipete/PSFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSMacros.h
288 lines (239 loc) · 13.1 KB
/
PSMacros.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
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
//
// PSMacros.h
//
// Created by Peter Steinberger on 03.05.10.
// Contains stuff from Dirk Holtwick - thanks for sharing!
//
// compiler help
#define INVALIDATE_TIMER(__TIMER) { [__TIMER invalidate]; __TIMER = nil; }
#define STRING_IS_EMPTY_OR_NIL( _STRING ) ( _STRING == nil || [_STRING isEmptyOrWhitespace] )
#define VERIFIED_CLASS(className) ((className *) NSClassFromString(@"" # className))
// http://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html
static inline BOOL IsEmpty(id thing) {
return thing == nil ||
([thing isEqual:[NSNull null]]) ||
([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) ||
([thing respondsToSelector:@selector(count)] && [(NSArray *)thing count] == 0);
}
// CGRect
#define PSRectClearCoords(_CGRECT) CGRectMake(0, 0, _CGRECT.size.width, _CGRECT.size.height)
#define CGRectClearCoords(_CGRECT) PSRectClearCoords(_CGRECT) // legacy
// portrait/landscape corrected screen bounds
#define PSAppStatusBarOrientation ([[UIApplication sharedApplication] statusBarOrientation])
#define PSIsPortrait() UIInterfaceOrientationIsPortrait(PSAppStatusBarOrientation)
#define PSIsLandscape() UIInterfaceOrientationIsLandscape(PSAppStatusBarOrientation)
#define UIInterfaceOrientationsAreCounterpart(o1,o2) (UIInterfaceOrientationIsPortrait(o1) && UIInterfaceOrientationIsPortrait(o2) || UIInterfaceOrientationIsLandscape(o1) && UIInterfaceOrientationIsLandscape(o2))
static inline CGRect PSScreenBounds() {
CGRect bounds = [UIScreen mainScreen].bounds;
if (PSIsLandscape()) {
bounds.size.width = [UIScreen mainScreen].bounds.size.height;
bounds.size.height = [UIScreen mainScreen].bounds.size.width;
}
return bounds;
}
// color
#define SETTINGS_TEXT_COLOR RGBCOLOR(57, 85, 135)
#ifndef RGBCOLOR
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#endif
#define XHEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:1.0]
#define XHEXCOLOR_ALPHA(c, a) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:a]
#define MTDegreesToRadian(x) (M_PI * (x) / 180.0)
#define MTRadianToDegrees(x) (M_PI * 180.0 / (x))
// Time Macros
#define MTTimeIntervalMilliseconds(x) (NSTimeInterval)(x / 1000.)
#define MTTimeIntervalSeconds(x) (NSTimeInterval)x
#define MTTimeIntervalMinutes(x) (NSTimeInterval)(x * 60.)
#define MTTimeIntervalHours(x) (NSTimeInterval)(x * 3600.)
#define MTTimeIntervalDays(x) (NSTimeInterval)(x * 3600. * 24.)
// Resource Path used with Folder References
#define MTGetFullPath(_filePath_) [[NSBundle mainBundle] pathForResource:[_filePath_ lastPathComponent] ofType:nil inDirectory:[_filePath_ stringByDeletingLastPathComponent]]
// Short hand NSLocalizedString, doesn't need 2 parameters
#define LocalizedString(s) NSLocalizedString(s,s)
#define _(s) NSLocalizedString(s,s)
// LocalizedString with an additionl parameter for formatting
#define LocalizedStringWithFormat(s,...) [NSString stringWithFormat:NSLocalizedString(s,s),##__VA_ARGS__]
// for function pass entering
#define DDLogFunction() DDLogInfo(@"-- logged --");
// http://www.alexcurylo.com/blog/2010/09/24/the-great-dealloc-debate/
#if DEBUG
#define MCRelease(x) do { [x release]; } while (0)
#else
#define MCRelease(x) [x release], x = nil
#endif
// always nil out for viewDidUnload!
#define MCReleaseNil(x) [x release], x = nil
#define MCReleaseViewNil(x) do { [x removeFromSuperview], [x release], x = nil; } while (0)
#if defined(TARGET_IPHONE_SIMULATOR) && defined(DEBUG)
#define PSSimulateMemoryWarning() CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
#else
#define PSSimulateMemoryWarning()
#endif
// http://code.google.com/p/cocoalumberjack/wiki/XcodeTricks - compiles most log messages out of the release build, but not all!
#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_INFO;
#else
static const int ddLogLevel = LOG_LEVEL_WARN;
#endif
// wrap to have non-retaining self pointers in blocks: safeSelf(dispatch_async(myQ, ^{[self doSomething];});
// use with care! can lead to crashes if self suddelny vanishes...
#define safeSelf(...) do { \
__typeof__(self) __x = self; \
__block __typeof__(self) self = __x; \
__VA_ARGS__; \
} while (0)
// http://www.cimgf.com/2010/05/02/my-current-prefix-pch-file/
// use like PSAssert(anAsset != nil, @"Asset cannot be nil");
#define PSAssertM(condition, message) do { if(condition) { DDLogWarn(@"asserted with %@", message); assert(condition && message); }} while(0)
#define PSAssert(condition) do { if(condition) { DDLogWarn(@"asserted!"); assert(condition); }} while(0)
#define PSAssertReturnNil(condition) do { if(condition) { DDLogWarn(@"PSAssertReturnNil: Condition is true!"); PSAssert(YES); return nil; }} while (0)
#define PSAssertReturnNO(condition) do { if(condition) { DDLogWarn(@"PSAssertReturnNil: Condition is true!"); PSAssert(YES); return NO; }} while (0)
#define PSAssertReturn(condition) do { if(condition) { DDLogWarn(@"PSAssertReturnNil: Condition is true!"); PSAssert(YES); return; }} while (0)
// block asserts for non-debug builds ("the ugly tree")
#ifndef DEBUG
// block classic assert()
#ifndef NDEBUG
#define NDEBUG
#endif
// block Log() macro
#ifndef NO_LOG_MACROS
#define NO_LOG_MACROS
#endif
#ifndef NS_BLOCK_ASSERTIONS
#define NS_BLOCK_ASSERTIONS
#undef NSCAssert
#define NSCAssert(condition, desc, ...) do { } while(0)
#endif
#endif
// performance measurement
// #define PERF 1
#if PERF
# define TSTART { NSTimeInterval __tStart = CFAbsoluteTimeGetCurrent();
# define TSTOP NSLog(@"%s: %f secs", __PRETTY_FUNCTION__, CFAbsoluteTimeGetCurrent() - __tStart); }
# define TSTOPLOG(fmt, ...) NSLog((@"%s\n\n %.8f secs: " fmt @"\n\n"), __PRETTY_FUNCTION__, (CFAbsoluteTimeGetCurrent() - __tStart), ##__VA_ARGS__); }
#else
# define TSTART
# define TSTOP
# define TSTOPLOG
#endif
// app shortcuts
#define XNavigationController [[[UIApplication sharedApplication] delegate] performSelector:@selector(navigationController)]
#define XAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
// compatibility
#define MTApplicationDelegate XAppDelegate
#define UIApp [UIApplication sharedApplication].delegate // deprectated
#define tsPushAnimated(vc) [[[[UIApplication sharedApplication] delegate] performSelector:@selector(navigationController)] pushViewController:vc animated:YES];
// defines
#define XAPPVERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
// filepaths
#define XFILPATH4DOCUMENT(_value) [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:_value]
#define XFILEPATH4BUNDLE(_value) [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_value]
#define XURL4FILEPATH(_value) [NSURL fileURLWithPath:_value]
#define XURL4DOCUMENT(_value) [NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:_value]]
#define XURL4BUNDLE(_value) [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_value]]
// array sorting & fetching
#define XSORTED(arr,by,asc) [arr sortedArrayUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:by ascending:asc] autorelease]]]
#define XSORT(arr,by,asc) [arr sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:by ascending:asc] autorelease]]];
#define XRX(v) ([v isKindOfClass:[NSPredicate class]] ? v : [NSPredicate predicateWithFormat:@"SELF MATCHES %@", v])
// value or nil
#define XINT(value) (value ? [NSNumber numberWithInt:value] : [NSNumber numberWithInt:0])
#define XFLOAT(value) (value ? [NSNumber numberWithDouble:(double)value] : [NSNumber numberWithDouble:(double)0.0])
#define XBOOL(value) (value ? [NSNumber numberWithBool:value] : [NSNumber numberWithBool:NO])
#define XNULL [NSNull null]
#define $false ((NSNumber*)kCFBooleanFalse)
#define $true ((NSNumber*)kCFBooleanTrue)
// even shorter versions
#define $I(value) XINT(value)
#define $F(value) XFLOAT(value)
#define $B(value) XBOOL(value)
#define $S(...) [NSString stringWithFormat: __VA_ARGS__]
// collection shortcuts
#define XDEFAULT(_value, _default) ([[NSNull null] isEqual:(_value)] ? (_default) : (_value))
#define XFMT(...) [NSString stringWithFormat: __VA_ARGS__]
#define XARRAY(...) [NSArray arrayWithObjects: __VA_ARGS__, nil]
#define XDICT(...) [NSDictionary dictionaryWithObjectsAndKeys: __VA_ARGS__, nil]
#define XMARRAY(...) [NSMutableArray arrayWithObjects: __VA_ARGS__, nil]
#define XMDICT(...) [NSMutableDictionary dictionaryWithObjectsAndKeys: __VA_ARGS__, nil]
#define XMCOPY(_obj) [[_obj mutableCopy] autorelease]
#define xcopy(_obj) [[_obj copy] autorelease]
// sorting
#define XLT(a,b) ([a compare:b] == NSOrderedAscending)
#define XGT(a,b) ([a compare:b] == NSOrderedDescending)
#define XEQ(a,b) ([a compare:b] == NSOrderedSame)
// notification center
#define XNC [NSNotificationCenter defaultCenter]
#define XNCADD(n,sel) [[NSNotificationCenter defaultCenter] addObserver:self selector:sel name:n object:nil];
#define XNCREMOVE [[NSNotificationCenter defaultCenter] removeObserver:self];
#define XNCPOST(name) [[NSNotificationCenter defaultCenter] postNotificationName:name object:self];
// selector apply
#define XAPPLY(target, sel) if([(id)target respondsToSelector:@selector(sel)]) { [(id)target performSelector:@selector(sel)]; }
#define XAPPLY1(target, sel, obj) if([(id)target respondsToSelector:@selector(sel:)]) { [(id)target performSelector:@selector(sel:) withObject:obj]; }
#define XAPPLYSEL(target, sel) if([target respondsToSelector:sel]) { [(id)target performSelector:sel]; }
#define XAPPLYSEL1(target, sel, obj) if([target respondsToSelector:sel]) { [(id)target performSelector:sel withObject:obj]; }
#define XDATA2UTF8STRING(data) [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]
#define XUTF8STRING2DATA(s) [s dataUsingEncoding:NSUTF8StringEncoding]
// user defaults
#define XDEL_OBJECT(k) [[NSUserDefaults standardUserDefaults] removeObjectForKey:k];
#define XGET_OBJECT(v) [[NSUserDefaults standardUserDefaults] objectForKey:v]
#define XSET_OBJECT(k,v) [[NSUserDefaults standardUserDefaults] setObject:v forKey:k];
#define XGET_STRING(v) [[NSUserDefaults standardUserDefaults] stringForKey:v]
#define XSET_STRING(k,v) [[NSUserDefaults standardUserDefaults] setObject:v forKey:k];
#define XGET_FLOAT(v) [[NSUserDefaults standardUserDefaults] floatForKey:v]
#define XSET_FLOAT(k,v) [[NSUserDefaults standardUserDefaults] setFloat:v forKey:k];
#define XGET_BOOL(v) [[NSUserDefaults standardUserDefaults] boolForKey:v]
#define XSET_BOOL(k,v) [[NSUserDefaults standardUserDefaults] setBool:v forKey:k];
#define XGET_INT(v) [[NSUserDefaults standardUserDefaults] integerForKey:v]
#define XSET_INT(k,v) [[NSUserDefaults standardUserDefaults] setInteger:v forKey:k];
#define XSYNC [[NSUserDefaults standardUserDefaults] synchronize];
// The following macro is for specifying property (ivar) names to KVC or KVO methods.
// These methods generally take strings, but strings don't get checked for typos
// by the compiler. If you write PROPERTY(fremen) instead of PROPERTY(frame),
// the compiler will immediately complain that it doesn't know the selector
// 'fremen', and thus point out the typo. For this to work, you need to make
// sure the warning -Wunknown-selector is on.
//
// The code that checks here is (theoretically) slower than just using a string
// literal, so what we do is we only do the checking in debug builds. In
// release builds, we use the identifier-stringification-operator "#" to turn
// the given property name into an ObjC string literal.
#if DEBUG
#define PROPERTY(propName) NSStringFromSelector(@selector(propName))
#else
#define PROPERTY(propName) @#propName
#endif
// The following use the same syntax as the ones in GNUstep. Just cuz that's
// the closest we have to a standard for stuff like this.
//
// Create a pool around some code by doing:
// CREATE_AUTORELEASE_POOL(myPool);
// // Use the pool.
// DESTROY(myPool);
//
// ASSIGN() is a neat macro to use inside mutators, DESTROY() is a shorthand
// that lets you release an object and clear its variable in one go.
//
// The do/while(0) stuff is just there so the macro behaves just like any other
// function call, as far as if/else etc. are concerned.
#define CREATE_AUTORELEASE_POOL(pool) NSAutoreleasePool* (pool) = [[NSAutoreleasePool alloc] init]
#define ASSIGN(targ,newval) do {\
NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
(targ) = [(newval) retain];\
[__UKHELPERMACRO_OLDTARG release];\
} while(0)
#define DESTROY(targ) do {\
NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
(targ) = nil;\
[__UKHELPERMACRO_OLDTARG release];\
} while(0)
#define PS_NOT_IMPLEMENTED DDLogError(@"Not yet implemented: %@", _cmd);
// swapper
#define ps_swap(a,b) { \
int c = (a); \
(a) = (b); \
(b) = c; \
}
#define ps_swapf(a,b) { \
float c = (a); \
(a) = (b); \
(b) = c; \
}