-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFConvenience.h
315 lines (270 loc) · 11.6 KB
/
FConvenience.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
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
// Frequently used macros for uncluttering things (Import in your PCH)
#ifndef _FCONV_H_
#define _FCONV_H_
#pragma mark - C -
#include <pthread.h>
#include <stdio.h>
#pragma mark Debug Utils
#ifdef DEBUG
# define DEBUG_ON YES
# define CrashHere() { *(int *)0 = 0xDEADBEEF; }
# define IfDebug(body...) body
#else
# define DEBUG_ON NO
# define CrashHere()
# define IfDebug(body...)
#endif
#pragma mark Looping
#define unless(body...) if(!(body))
#define until(body...) while(!(body))
#pragma mark Numerical Ranges
#define PASTE(a,b) a##b
#if !defined(BETWEEN)
# define __BETWEEN(val, low, high, c) ({ \
__typeof(val) const PASTE(__val,c) = (val); \
PASTE(__val,c) > (low) && PASTE(__val,c) < (high); \
})
# define BETWEEN(val, low, high) __BETWEEN((val), (low), (high), __COUNTER__)
#endif
#if !defined(INRANGE)
# define __INRANGE(val, low, high, c) ({ \
__typeof(val) const PASTE(__val,c) = (val); \
PASTE(__val,c) >= (low) && PASTE(__val,c) <= (high); \
})
# define INRANGE(val, low, high) __INRANGE((val), (low), (high), __COUNTER__)
#endif
#if !defined(CLAMP)
# define CLAMP(val, min, max) MAX((min), MIN((val), (max)))
#endif
#define POWOF2(n) ({ __typeof(n) __n = (n); (__n != 0) && !(__n & (__n - 1)); })
#if defined(TARGET_OS_IPHONE) && !FCONVENIENCE_USE_DOUBLE
typedef float FFloat;
#else
typedef double FFloat;
#endif
typedef struct _FFloatRange {
FFloat location, length;
} FFloatRange;
static inline FFloat FFloatRangeMax(FFloatRange const aRange) {
return aRange.location + aRange.length;
}
#pragma mark - Objective-C -
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
# include <UIKit/UIKit.h>
#endif
#if CGFLOAT_IS_DOUBLE
# define CGFLOAT_EPSILON DBL_EPSILON
# define cgfabs fabs
#else
# define CGFLOAT_EPSILON FLT_EPSILON
# define cgfabs fabsf
#endif
#define CGRectGetCenter(r) ({ \
CGRect __r = (r); \
(CGPoint) { CGRectGetMidX(__r), CGRectGetMidY(__r) }; \
})
#define _NSCompare(x, y, c) ({ \
__typeof(x) PASTE(__x,c) = (x); \
__typeof(y) PASTE(__y,c) = (y); \
PASTE(__x,c) > PASTE(__y,c) ? NSOrderedDescending \
: PASTE(__x,c) < PASTE(__y,c) ? NSOrderedAscending \
: NSOrderedSame; \
})
#define NSCompare(x,y) _NSCompare((x), (y), __COUNTER__)
#pragma mark GCD Utilities
#if __has_extension(blocks)
# include <dispatch/dispatch.h>
# define GlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
# define MainQueue dispatch_get_main_queue()
# define Once(body...) do { \
static dispatch_once_t __token; \
dispatch_once(&__token, ##body); \
} while(0)
static inline void Async(dispatch_block_t const blk) { dispatch_async(GlobalQueue, blk); }
static inline void AsyncOnMain(dispatch_block_t const blk) { dispatch_async(MainQueue, blk); }
static inline void SyncOnMain(dispatch_block_t const blk) {
if(pthread_main_np())
blk();
else
dispatch_sync(MainQueue, blk);
}
static inline void AfterDelay(float const seconds, dispatch_block_t const blk) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC),
MainQueue, blk);
}
# define Memoize(x...) ({ \
static __typeof(({ x; })) __memoized_x; \
Once(^{ __memoized_x = ({ x; }); }); \
__memoized_x; \
})
#endif
#pragma mark Logging
enum FLogLevel {
FLogLevel_Emergency,
FLogLevel_Alert,
FLogLevel_Critical,
FLogLevel_Err,
FLogLevel_Warning,
FLogLevel_Notice
};
void _FLog(enum FLogLevel aLevel,
const char * aFile,
int aLine,
NSString *aFormat, ...) NS_FORMAT_FUNCTION(4,5);
#define Log(lvl, msg...) _FLog((lvl), __FILE__, __LINE__, ##msg)
#define LogError(msg...) Log(FLogLevel_Err, ##msg)
#define LogWarning(msg...) Log(FLogLevel_Warning, ##msg)
#define LogNotice(msg...) Log(FLogLevel_Notice, ##msg)
#define _CheckOSErr(shouldAssert, error, fmt, params...) do { \
OSStatus __err = (error); \
if(__err) { \
LogError(@"OSErr %d: " fmt, (int)__err, ##params); \
if(shouldAssert) \
assert(__err == noErr); \
} \
} while(0)
#ifdef DEBUG
# define CheckOSErr(err, fmt, params...) _CheckOSErr(true, err, fmt, ##params)
# define glError() do { \
const char *errStr = NULL; \
GLenum err; \
while((err = glGetError()) != GL_NO_ERROR) { \
switch(err) { \
case GL_INVALID_ENUM: errStr = "GL_INVALID_ENUM"; break; \
case GL_INVALID_VALUE: errStr = "GL_INVALID_VALUE"; break; \
case GL_OUT_OF_MEMORY: errStr = "GL_OUT_OF_MEMORY"; break; \
case GL_INVALID_OPERATION: errStr = "GL_INVALID_OPERATION"; break; \
case GL_INVALID_FRAMEBUFFER_OPERATION: \
errStr = "GL_INVALID_FRAMEBUFFER_OPERATION"; break; \
default: errStr = "UNKNOWN"; \
} \
LogError(@"glError(0x%04x): %s caught at %s:%u\n", \
err, errStr, __FILE__, __LINE__); \
} \
NSCAssert(errStr == NULL, @"OpenGL Error"); \
} while(0)
#else
# define CheckOSErr(err, fmt, params...) _CheckOSErr(false, err, fmt, ##params)
# define glError()
#endif
#pragma mark Shorthands
#define Unavailable(reason) __attribute__((unavailable(reason)))
#define NotificationCenter [NSNotificationCenter defaultCenter]
#define Bundle [NSBundle mainBundle]
#define Workspace [NSWorkspace sharedWorkspace]
#define FileManager [NSFileManager defaultManager]
#define Defaults [NSUserDefaults standardUserDefaults]
#define NSFormat(fmt...) [NSString stringWithFormat:fmt]
#define NSMutableFormat(fmt...) [NSMutableString stringWithFormat:fmt]
#define Predicate(fmt...) [NSPredicate predicateWithFormat:fmt]
#define FetchReq(name) [NSFetchRequest fetchRequestWithEntityName:(name)]
#define ThreadDict [[NSThread currentThread] threadDictionary]
#define NSNullToNil(x) ({ \
__typeof(x) __x = (x); \
[[NSNull null] isEqual:__x] ? nil : __x; \
})
#if TARGET_OS_IPHONE
# define Device [UIDevice currentDevice]
# define UIApp [UIApplication sharedApplication]
# define CacheDir ({ \
NSArray * const cacheURLs = [FileManager URLsForDirectory:NSCachesDirectory \
inDomains:NSUserDomainMask]; \
NSCAssert([cacheURLs count] > 0, @"Couldn't get cache directory path"); \
(NSString *)[cacheURLs[0] path]; \
})
#endif
#define DefineThreadLocal(name) \
static void __ThreadLocalCleanup_##name##__(void *ptr) { id const __unused obj = (__bridge_transfer id)ptr; } \
static pthread_key_t __ThreadLocal_##name##__
#define InitThreadLocal(name) \
Once(^{ pthread_key_create(&__ThreadLocal_##name##__, &__ThreadLocalCleanup_##name##__); })
#define ReadThreadLocal(name) \
(__bridge id)pthread_getspecific(__ThreadLocal_##name##__)
#define WriteThreadLocal(name, value) ({ \
id const __unused __oldValue__ = (__bridge_transfer id)pthread_getspecific(__ThreadLocal_##name##__); \
id const __value__ = (value); \
pthread_setspecific(__ThreadLocal_##name##__, (__bridge_retained void *)__value__); \
__value__; \
})
#pragma mark Boxing
#define OVERLOADABLE __attribute((overloadable))
static inline NSNumber * OVERLOADABLE FBox(char x) { return @(x); }
static inline NSNumber * OVERLOADABLE FBox(short x) { return @(x); }
static inline NSNumber * OVERLOADABLE FBox(int x) { return @(x); }
static inline NSNumber * OVERLOADABLE FBox(long x) { return @(x); }
static inline NSNumber * OVERLOADABLE FBox(float x) { return @(x); }
static inline NSNumber * OVERLOADABLE FBox(double x) { return @(x); }
static inline NSString * OVERLOADABLE FBox(char *x) { return @(x); }
static inline NSValue * OVERLOADABLE FBox(NSRange x) { return [NSValue valueWithRange:x]; }
#if TARGET_OS_IPHONE
static inline NSValue * OVERLOADABLE FBox(CGRect x) { return [NSValue valueWithCGRect:x]; }
static inline NSValue * OVERLOADABLE FBox(CGPoint x) { return [NSValue valueWithCGPoint:x]; }
static inline NSValue * OVERLOADABLE FBox(CGSize x) { return [NSValue valueWithCGSize:x]; }
static inline NSValue * OVERLOADABLE FBox(CGAffineTransform x) { return [NSValue valueWithCGAffineTransform:x]; }
static inline NSValue * OVERLOADABLE FBox(CATransform3D x) { return [NSValue valueWithCATransform3D:x]; }
static inline NSValue * OVERLOADABLE FBox(UIOffset x) { return [NSValue valueWithUIOffset:x]; }
static inline NSValue * OVERLOADABLE FBox(UIEdgeInsets x) { return [NSValue valueWithUIEdgeInsets:x]; }
# ifdef CMTIMERANGE_H
static inline NSValue * OVERLOADABLE FBox(CMTimeRange x) { return [NSValue valueWithCMTimeRange:x]; }
static inline NSValue * OVERLOADABLE FBox(CMTimeMapping x) { return [NSValue valueWithCMTimeMapping:x]; }
# endif
# ifdef MK_EXTERN
static inline NSValue * OVERLOADABLE FBox(CLLocationCoordinate2D x) { return [NSValue valueWithMKCoordinate:x]; }
static inline NSValue * OVERLOADABLE FBox(MKCoordinateSpan x) { return [NSValue valueWithMKCoordinateSpan:x]; }
# endif
#else
static inline NSValue * OVERLOADABLE FBox(NSRect x) { return [NSValue valueWithRect:x]; }
static inline NSValue * OVERLOADABLE FBox(NSPoint x) { return [NSValue valueWithPoint:x]; }
static inline NSValue * OVERLOADABLE FBox(NSSize x) { return [NSValue valueWithSize:x]; }
#endif
#undef OVERLOADABLE
#pragma mark Device Detection (iOS)
#if TARGET_OS_IPHONE
# define UIIdiomString() ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? @"ipad" : @"iphone")
# define DeviceIsIPad() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
# define SixOrOlder() (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1)
#endif
#pragma mark Drawing Utils
#if TARGET_OS_IPHONE
# define LetPath(__path, body...) ({ \
UIBezierPath * const path = (__path); \
do { body; } while(0); \
path; \
})
# define RGBA(r,g,b,a) [UIColor colorWithRed:(r) green:(g) blue:(b) alpha:(a)]
# define HSBA(h,s,b,a) [UIColor colorWithHue:(h) saturation:(s) brightness:(b) alpha:(a)]
#else
# define RGBA(r,g,b,a) [NSColor colorWithCalibratedRed:(r) green:(g) blue:(b) alpha:(a)]
# define HSBA(h,s,b,a) [NSColor colorWithCalibratedHue:(h) saturation:(s) brightness:(b) alpha:(a)]
# define LetPath(__path, body...) ({ \
NSBezierPath * const path = (__path); \
do { body; } while(0); \
path; \
})
#endif
#define RGB(r,g,b) RGBA((r), (g), (b), 1)
#define HSB(h,s,b) HSBA((h), (s), (b), 1)
#define GRAY(b) ({ __typeof(b) b_ = (b); RGB(b_,b_,b_); })
#define StrokePath(__path, body...) [LetPath(__path, body) stroke]
#define FillPath(__path, body...) [LetPath(__path, body) fill]
#pragma mark Animation Utils
#if TARGET_OS_IPHONE
# define WithDur UIView animateWithDuration // Use like: [WithDur:0.3 animations:^{...}]
# define UIAppGoSlowmo() [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] layer] setSpeed:.1f]
# define SetVolume(vol) \
[[MPMusicPlayerController applicationMusicPlayer] setVolume:(vol)];
UIImage *FScreenshot(float aScale);
#endif
#pragma mark Subscripts
@interface NSUserDefaults (Subscripts)
- (id)objectForKeyedSubscript:(id)aKey;
- (void)setObject:(id)aObj forKeyedSubscript:(id)aKey;
@end
@interface NSCache (Subscripts)
- (id)objectForKeyedSubscript:(id)aKey;
- (void)setObject:(id)aObj forKeyedSubscript:(id)aKey;
@end
#endif // __OBJC__
#endif // _FCONV_H_