hank
2019-01-22 ab662912a378edb0878538b40a531434dbbe6792
commit | author | age
655e66 1 /*
H 2  * Copyright (C) 2017 Twitter, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
a0a843 18 #import <TwitterCore/TWTRConstants.h>
H 19
20 #ifdef __OBJC__
21
22 #define TWTRParameterAssertSettingError(condition, errorPointer)                                                                                                                                                 \
23     NSParameterAssert((condition));                                                                                                                                                                              \
24     if (!(condition)) {                                                                                                                                                                                          \
25         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition);                                                                                                  \
26         if (errorPointer != NULL) {                                                                                                                                                                              \
27             *errorPointer = [NSError errorWithDomain:TWTRErrorDomain code:TWTRErrorCodeMissingParameter userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Missing parameter %s", #condition]}]; \
28         }                                                                                                                                                                                                        \
29     }
30
31 #define TWTRParameterAssertOrReturnValue(condition, returnValue)                                                \
32     NSParameterAssert((condition));                                                                             \
33     if (!(condition)) {                                                                                         \
34         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition); \
35         return returnValue;                                                                                     \
36     }
37
38 #define TWTRParameterAssertOrReturnNil(condition)                                                               \
39     NSParameterAssert((condition));                                                                             \
40     if (!(condition)) {                                                                                         \
41         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition); \
42         return nil;                                                                                             \
43     }
44
45 #define TWTRParameterAssertOrReturn(condition)                                                                  \
46     NSParameterAssert((condition));                                                                             \
47     if (!(condition)) {                                                                                         \
48         NSLog(@"[TwitterKit] %@ Invalid parameter not satisfying: %s", NSStringFromSelector(_cmd), #condition); \
49         return;                                                                                                 \
50     }
51
52 #define TWTRAssertMainThread()                                                                         \
53     if (![NSThread isMainThread]) {                                                                    \
54         [NSException raise:NSInternalInconsistencyException format:@"Need to be on the main thread."]; \
55         return;                                                                                        \
56     }
57
58 // Check a single argument, and call a completion block if it's missing
59 #define TWTRCheckArgumentWithCompletion(condition, completion)     \
60     TWTRParameterAssertOrReturn(completion);                       \
61     NSError *parameterError;                                       \
62     TWTRParameterAssertSettingError((condition), &parameterError); \
63     if (parameterError) {                                          \
64         completion(nil, nil, parameterError);                      \
65         return;                                                    \
66     }
67
68 #define TWTRCheckArgumentWithCompletion2(condition, completion)    \
69     TWTRParameterAssertOrReturn(completion);                       \
70     NSError *parameterError;                                       \
71     TWTRParameterAssertSettingError((condition), &parameterError); \
72     if (parameterError) {                                          \
73         completion(nil, parameterError);                           \
74         return;                                                    \
75     }
76
77 #endif