hank
2017-06-14 a0a84333e64f1e94ae9d0f69545037c60e781842
commit | author | age
a0a843 1 //
H 2 //  TWTRAssertionMacros.h
3 //  TwitterKit
4 //
5 //  Created by Kang Chen on 3/5/15.
6 //  Copyright (c) 2015 Twitter. All rights reserved.
7 //
8
9 #import <TwitterCore/TWTRConstants.h>
10
11 #ifdef __OBJC__
12
13 #define TWTRParameterAssertSettingError(condition, errorPointer)                                                                                                                                                 \
14     NSParameterAssert((condition));                                                                                                                                                                              \
15     if (!(condition)) {                                                                                                                                                                                          \
16         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition);                                                                                                  \
17         if (errorPointer != NULL) {                                                                                                                                                                              \
18             *errorPointer = [NSError errorWithDomain:TWTRErrorDomain code:TWTRErrorCodeMissingParameter userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Missing parameter %s", #condition]}]; \
19         }                                                                                                                                                                                                        \
20     }
21
22 #define TWTRParameterAssertOrReturnValue(condition, returnValue)                                                \
23     NSParameterAssert((condition));                                                                             \
24     if (!(condition)) {                                                                                         \
25         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition); \
26         return returnValue;                                                                                     \
27     }
28
29 #define TWTRParameterAssertOrReturnNil(condition)                                                               \
30     NSParameterAssert((condition));                                                                             \
31     if (!(condition)) {                                                                                         \
32         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition); \
33         return nil;                                                                                             \
34     }
35
36 #define TWTRParameterAssertOrReturn(condition)                                                                  \
37     NSParameterAssert((condition));                                                                             \
38     if (!(condition)) {                                                                                         \
39         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition); \
40         return;                                                                                                 \
41     }
42
43 #define TWTRAssertMainThread()                                                                         \
44     if (![NSThread isMainThread]) {                                                                    \
45         [NSException raise:NSInternalInconsistencyException format:@"Need to be on the main thread."]; \
46         return;                                                                                        \
47     }
48
49 // Check a single argument, and call a completion block if it's missing
50 #define TWTRCheckArgumentWithCompletion(condition, completion)     \
51     TWTRParameterAssertOrReturn(completion);                       \
52     NSError *parameterError;                                       \
53     TWTRParameterAssertSettingError((condition), &parameterError); \
54     if (parameterError) {                                          \
55         completion(nil, nil, parameterError);                      \
56         return;                                                    \
57     }
58
59 #define TWTRCheckArgumentWithCompletion2(condition, completion)    \
60     TWTRParameterAssertOrReturn(completion);                       \
61     NSError *parameterError;                                       \
62     TWTRParameterAssertSettingError((condition), &parameterError); \
63     if (parameterError) {                                          \
64         completion(nil, parameterError);                           \
65         return;                                                    \
66     }
67
68 #endif