-
Notifications
You must be signed in to change notification settings - Fork 56
/
LxDBAnything.h
434 lines (331 loc) · 16.3 KB
/
LxDBAnything.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//
// LxDBAnything.h
// LxDBAnythingDemo
//
// Created by DeveloperLx on 15/10/24.
// Copyright © 2015年 DeveloperLx. All rights reserved.
//
#ifndef LxDBAnything_h
#define LxDBAnything_h
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#pragma mark------------------------------- interface -------------------------------
#if TARGET_OS_IPHONE
#define LxEdgeInsets UIEdgeInsets
#define LxOffset UIOffset
#define valueWithLxOffset valueWithUIOffset
#define valueWithLxEdgeInsets valueWithUIEdgeInsets
#elif TARGET_OS_MAC
#define LxEdgeInsets NSEdgeInsets
#define LxOffset NSOffset
#define valueWithLxOffset valueWithNSOffset
#define valueWithLxEdgeInsets valueWithNSEdgeInsets
#endif
#define stringify __STRING
#define LxType(var) __typeof__((var))
#define LxBox(var) __lx_box(@encode(LxType(var)), (var))
#define LxBoxToString(var) [LxBox(var) description]
#define LxTypeStringOfVar(var) __lx_type_string_for_var(@encode(LxType(var)), (var))
static NSDictionary *LxDictionaryFromObject(NSObject *object);
static NSString *LxJsonFromObject(NSObject *object);
static NSString *LxXmlFromObject(NSObject *object);
static NSString *LxViewHierarchyDescription(UIView *view);
#ifdef DEBUG
#define LxPrintf(fmt, ...) printf("📍%s + %d🎈 %s\n", __PRETTY_FUNCTION__, __LINE__, [[NSString stringWithFormat:fmt, ##__VA_ARGS__]UTF8String])
#define LxDBAnyVar(var) LxPrintf(@"%s = %@", #var, LxBox(var))
#define LxPrintAnything(x) printf("📍%s + %d🎈 %s\n", __PRETTY_FUNCTION__, __LINE__, #x)
#define LxDBObjectAsJson(obj) printf("📍%s + %d🎈 %s\n", __PRETTY_FUNCTION__, __LINE__, __lx_json_db_object_string(obj).UTF8String)
#define LxDBObjectAsXml(obj) printf("📍%s + %d🎈 %s\n", __PRETTY_FUNCTION__, __LINE__, __lx_xml_db_object_string(obj).UTF8String)
#define LxDBViewHierarchy(view) printf("📍%s + %d🎈%s =\n%s\n", __PRETTY_FUNCTION__, __LINE__, #view, LxViewHierarchyDescription(view).UTF8String)
#else
#define LxPrintf(fmt, ...)
#define LxDBAnyVar(any)
#define LxPrintAnything(x)
#define LxDBObjectAsJson(obj)
#define LxDBObjectAsXml(obj)
#define LxDBViewHierarchy(view)
#endif
#pragma mark ------------------------------- implementation -------------------------------
static inline id __lx_box(const char *type, ...) {
va_list variable_param_list;
va_start(variable_param_list, type);
id object = nil;
if (strcmp(type, @encode(id)) == 0) {
id param = va_arg(variable_param_list, id);
object = param;
} else if (strcmp(type, @encode(CGPoint)) == 0) {
CGPoint param = (CGPoint)va_arg(variable_param_list, CGPoint);
object = [NSValue valueWithCGPoint:param];
} else if (strcmp(type, @encode(CGSize)) == 0) {
CGSize param = (CGSize)va_arg(variable_param_list, CGSize);
object = [NSValue valueWithCGSize:param];
} else if (strcmp(type, @encode(CGVector)) == 0) {
CGVector param = (CGVector)va_arg(variable_param_list, CGVector);
object = [NSValue valueWithCGVector:param];
} else if (strcmp(type, @encode(CGRect)) == 0) {
CGRect param = (CGRect)va_arg(variable_param_list, CGRect);
object = [NSValue valueWithCGRect:param];
} else if (strcmp(type, @encode(NSRange)) == 0) {
NSRange param = (NSRange)va_arg(variable_param_list, NSRange);
object = [NSValue valueWithRange:param];
} else if (strcmp(type, @encode(CFRange)) == 0) {
CFRange param = (CFRange)va_arg(variable_param_list, CFRange);
object = [NSValue value:¶m withObjCType:type];
} else if (strcmp(type, @encode(CGAffineTransform)) == 0) {
CGAffineTransform param = (CGAffineTransform)va_arg(variable_param_list, CGAffineTransform);
object = [NSValue valueWithCGAffineTransform:param];
} else if (strcmp(type, @encode(CATransform3D)) == 0) {
CATransform3D param = (CATransform3D)va_arg(variable_param_list, CATransform3D);
object = [NSValue valueWithCATransform3D:param];
} else if (strcmp(type, @encode(SEL)) == 0) {
SEL param = (SEL)va_arg(variable_param_list, SEL);
object = NSStringFromSelector(param);
} else if (strcmp(type, @encode(Class)) == 0) {
Class param = (Class)va_arg(variable_param_list, Class);
object = NSStringFromClass(param);
} else if (strcmp(type, @encode(LxOffset)) == 0) {
LxOffset param = (LxOffset)va_arg(variable_param_list, LxOffset);
object = [NSValue valueWithLxOffset:param];
} else if (strcmp(type, @encode(LxEdgeInsets)) == 0) {
LxEdgeInsets param = (LxEdgeInsets)va_arg(variable_param_list, LxEdgeInsets);
object = [NSValue valueWithLxEdgeInsets:param];
} else if (strcmp(type, @encode(short)) == 0) {
short param = (short)va_arg(variable_param_list, int);
object = @(param);
} else if (strcmp(type, @encode(int)) == 0) {
int param = (int)va_arg(variable_param_list, int);
object = @(param);
} else if (strcmp(type, @encode(long)) == 0) {
long param = (long)va_arg(variable_param_list, long);
object = @(param);
} else if (strcmp(type, @encode(long long)) == 0) {
long long param = (long long)va_arg(variable_param_list, long long);
object = @(param);
} else if (strcmp(type, @encode(float)) == 0) {
float param = (float)va_arg(variable_param_list, double);
object = @(param);
} else if (strcmp(type, @encode(double)) == 0) {
double param = (double)va_arg(variable_param_list, double);
object = @(param);
} else if (strcmp(type, @encode(BOOL)) == 0) {
BOOL param = (BOOL)va_arg(variable_param_list, int);
object = param ? @"YES" : @"NO";
} else if (strcmp(type, @encode(bool)) == 0) {
bool param = (bool)va_arg(variable_param_list, int);
object = param ? @"true" : @"false";
} else if (strcmp(type, @encode(char)) == 0) {
char param = (char)va_arg(variable_param_list, int);
object = [NSString stringWithFormat:@"%c", param];
} else if (strcmp(type, @encode(unsigned short)) == 0) {
unsigned short param = (unsigned short)va_arg(variable_param_list, unsigned int);
object = @(param);
} else if (strcmp(type, @encode(unsigned int)) == 0) {
unsigned int param = (unsigned int)va_arg(variable_param_list, unsigned int);
object = @(param);
} else if (strcmp(type, @encode(unsigned long)) == 0) {
unsigned long param = (unsigned long)va_arg(variable_param_list, unsigned long);
object = @(param);
} else if (strcmp(type, @encode(unsigned long long)) == 0) {
unsigned long long param = (unsigned long long)va_arg(variable_param_list, unsigned long long);
object = @(param);
} else if (strcmp(type, @encode(unsigned char)) == 0) {
unsigned char param = (unsigned char)va_arg(variable_param_list, unsigned int);
object = [NSString stringWithFormat:@"%c", param];
} else {
void *param = (void *)va_arg(variable_param_list, void *);
object = [NSString stringWithFormat:@"%p", param];
}
va_end(variable_param_list);
return object;
}
static inline char __lx_first_char_for_string(const char *string) {
if (strlen(string) > 0) {
return string[0];
} else {
return '\0';
}
}
static inline char __lx_last_char_for_string(const char *string) {
if (strlen(string) > 0) {
return string[strlen(string) - 1];
} else {
return '\0';
}
}
static inline NSString *__lx_type_string_for_var(const char *type, ...) {
va_list variable_param_list;
va_start(variable_param_list, type);
NSString *typeString = nil;
if (strcmp(type, @encode(id)) == 0) {
id param = va_arg(variable_param_list, id);
typeString = NSStringFromClass([param class]);
} else if (strcmp(type, @encode(CGPoint)) == 0) {
typeString = @stringify(CGPoint);
} else if (strcmp(type, @encode(CGSize)) == 0) {
typeString = @stringify(CGSize);
} else if (strcmp(type, @encode(CGVector)) == 0) {
typeString = @stringify(CGVector);
} else if (strcmp(type, @encode(CGRect)) == 0) {
typeString = @stringify(CGRect);
} else if (strcmp(type, @encode(NSRange)) == 0) {
typeString = @stringify(NSRange);
} else if (strcmp(type, @encode(CFRange)) == 0) {
typeString = @stringify(CFRange);
} else if (strcmp(type, @encode(CGAffineTransform)) == 0) {
typeString = @stringify(CGAffineTransform);
} else if (strcmp(type, @encode(CATransform3D)) == 0) {
typeString = @stringify(CATransform3D);
} else if (strcmp(type, @encode(SEL)) == 0) {
typeString = @stringify(SEL);
} else if (strcmp(type, @encode(Class)) == 0) {
typeString = @stringify(Class);
} else if (strcmp(type, @encode(LxOffset)) == 0) {
typeString = @stringify(LxOffset);
} else if (strcmp(type, @encode(LxEdgeInsets)) == 0) {
typeString = @stringify(LxEdgeInsets);
} else if (strcmp(type, @encode(short)) == 0) {
typeString = @stringify(short);
} else if (strcmp(type, @encode(int)) == 0) {
typeString = @stringify(int);
} else if (strcmp(type, @encode(long)) == 0) {
typeString = @stringify(long);
} else if (strcmp(type, @encode(long long)) == 0) {
typeString = @stringify(long long);
} else if (strcmp(type, @encode(float)) == 0) {
typeString = @stringify(float);
} else if (strcmp(type, @encode(double)) == 0) {
typeString = @stringify(double);
} else if (strcmp(type, @encode(long double)) == 0) {
typeString = @stringify(long double);
} else if (strcmp(type, @encode(BOOL)) == 0) {
typeString = @stringify(BOOL);
} else if (strcmp(type, @encode(bool)) == 0) {
typeString = @stringify(bool);
} else if (strcmp(type, @encode(char)) == 0) {
typeString = @stringify(char);
} else if (strcmp(type, @encode(unsigned short)) == 0) {
typeString = @stringify(unsigned short);
} else if (strcmp(type, @encode(unsigned int)) == 0) {
typeString = @stringify(unsigned int);
} else if (strcmp(type, @encode(unsigned long)) == 0) {
typeString = @stringify(unsigned long);
} else if (strcmp(type, @encode(unsigned long long)) == 0) {
typeString = @stringify(unsigned long long);
} else if (strcmp(type, @encode(unsigned char)) == 0) {
typeString = @stringify(unsigned char);
} else if (strcmp(type, @encode(char *)) == 0) {
typeString = @stringify(char *);
} else if (strcmp(type, @encode(void)) == 0) {
typeString = @stringify(void);
} else if (strcmp(type, @encode(void *)) == 0) {
typeString = @stringify(void *);
} else if (__lx_first_char_for_string(type) == '[' && __lx_last_char_for_string(type) == ']') {
typeString = @stringify(array);
} else if (__lx_first_char_for_string(type) == '{' && __lx_last_char_for_string(type) == '}') {
typeString = @stringify(struct);
} else if (__lx_first_char_for_string(type) == '(' && __lx_last_char_for_string(type) == ')') {
typeString = @stringify(union);
} else if (__lx_first_char_for_string(type) == '^') {
typeString = @stringify(pointer);
} else if (__lx_first_char_for_string(type) == 'b') {
typeString = @stringify(bit_field);
} else if (strcmp(type, "?") == 0) {
typeString = @stringify(unknown_type);
} else {
typeString = @"LxDBAnything:Can not distinguish temporarily!😂";
}
va_end(variable_param_list);
return typeString;
}
static NSObject *__lx_stringify_object_value(NSObject *object) {
if ([object isKindOfClass:[NSArray class]]) {
NSMutableArray *arrayObject = [NSMutableArray array];
for (id obj in (NSArray *)object) {
[arrayObject addObject:__lx_stringify_object_value(obj)];
}
return [NSArray arrayWithArray:arrayObject];
} else if ([object isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *dictionaryObject = [NSMutableDictionary dictionary];
[(NSDictionary *)object enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL *_Nonnull stop) {
[dictionaryObject setValue:__lx_stringify_object_value(obj) forKey:LxBoxToString(key)];
}];
return [NSDictionary dictionaryWithDictionary:dictionaryObject];
} else if ([object isKindOfClass:[NSSet class]]) {
NSMutableArray *arrayObject = [NSMutableArray array];
for (id obj in (NSSet *)object) {
[arrayObject addObject:__lx_stringify_object_value(obj)];
}
return [NSArray arrayWithArray:arrayObject];
} else if ([object isKindOfClass:[NSOrderedSet class]]) {
NSMutableArray *arrayObject = [NSMutableArray array];
for (id obj in (NSOrderedSet *)object) {
[arrayObject addObject:__lx_stringify_object_value(obj)];
}
return [NSArray arrayWithArray:arrayObject];
} else {
return LxBoxToString(object);
}
}
static NSDictionary *LxDictionaryFromObject(NSObject *object) {
NSCAssert([object isKindOfClass:[NSObject class]], ([NSString stringWithFormat:@"LxDBAnything:%@ type error!", object]));
NSMutableDictionary *objectDictionary = [NSMutableDictionary dictionary];
unsigned int outCount = 0;
objc_property_t *propertyList = class_copyPropertyList([object class], &outCount);
for (int i = 0; i < outCount; i++) {
objc_property_t property = propertyList[i];
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
if ([object respondsToSelector:NSSelectorFromString(propertyName)]) {
@try {
NSObject *propertyValue = [object valueForKey:propertyName];
[objectDictionary setValue:__lx_stringify_object_value(propertyValue) forKey:propertyName];
}
@catch (NSException *exception) {
LxDBAnyVar(exception); //
[objectDictionary setValue:nil forKey:propertyName];
}
}
}
free(propertyList);
return [NSDictionary dictionaryWithDictionary:objectDictionary];
}
static NSString *__lx_json_db_object_string(NSObject *object) {
return [NSString stringWithFormat:@"%@ = %@", object.description, LxJsonFromObject(object)];
}
static NSString *__lx_xml_db_object_string(NSObject *object) {
return [NSString stringWithFormat:@"%@ = %@", object.description, LxXmlFromObject(object)];
}
static NSString *LxJsonFromObject(NSObject *object) {
NSError *error = nil;
NSDictionary *objectDictionary = LxDictionaryFromObject(object);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:objectDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
}
static NSString *LxXmlFromObject(NSObject *object) {
NSError *error = nil;
NSDictionary *objectDictionary = LxDictionaryFromObject(object);
NSData *xmlData = [NSPropertyListSerialization dataWithPropertyList:objectDictionary format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
NSString *xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
return xmlString;
}
static NSString *__lx_view_hierarchy_description(UIView *view, NSInteger depth) {
static NSString *unitIndentSpaceString = @" ";
NSString *indentSpaceString = @" ";
for (int i = 0; i < depth; i++) {
indentSpaceString = [indentSpaceString stringByAppendingString:unitIndentSpaceString];
}
NSString *viewHierarchyDescription = [NSString stringWithFormat:@"%zi#%@%@\n", depth, indentSpaceString, view];
depth++;
for (UIView *subview in view.subviews) {
viewHierarchyDescription = [viewHierarchyDescription stringByAppendingString:__lx_view_hierarchy_description(subview, depth)];
}
return viewHierarchyDescription;
}
static NSString *LxViewHierarchyDescription(UIView *view) {
NSCAssert([view isKindOfClass:[UIView class]], @"");
NSString *viewHierarchyDescription = @"";
NSInteger depth = 0;
viewHierarchyDescription = [viewHierarchyDescription stringByAppendingString:__lx_view_hierarchy_description(view, depth)];
return viewHierarchyDescription;
}
#endif /* LxDBAnything_h */