blob: 4cd9a967db21fe4a9e62894c701a06e2c4d101c5 (
plain)
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
|
/* NSDictionary+HBAdditions.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "NSDictionary+HBAdditions.h"
#pragma mark - NSDictionary to hb_dict_t
@implementation NSObject (HBValueAdditions)
- (hb_value_t *)hb_value
{
return NULL;
}
@end
@implementation NSNumber (HBValueAdditions)
- (hb_value_t *)hb_value
{
hb_value_t *result = NULL;
const char *objCType = self.objCType;
if((strcmp(objCType, @encode(float))) == 0 ||
(strcmp(objCType, @encode(double))) == 0)
{
result = hb_value_double(self.doubleValue);
}
else if((strcmp(objCType, @encode(BOOL))) == 0)
{
result = hb_value_bool(self.boolValue);
}
else
{
result = hb_value_int(self.integerValue);
}
return result;
}
@end
@implementation NSString (HBValueAdditions)
- (nullable hb_value_t *)hb_value
{
return hb_value_string(self.UTF8String);
}
@end
@implementation NSArray (HBValueAdditions)
- (hb_value_array_t *)hb_value
{
hb_value_array_t *result = hb_value_array_init();
for (id obj in self)
{
hb_value_t *val = [obj hb_value];
if (val)
{
hb_value_array_append(result, val);
}
}
return result;
}
@end
@implementation NSDictionary (HBValueAdditions)
- (instancetype)initWithHBDict:(const hb_dict_t *)dict
{
self = [self init];
if (self)
{
self = convertDictToObjcType(dict);
}
return self;
}
- (hb_dict_t *)hb_value
{
hb_dict_t *result = hb_dict_init();
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop)
{
NSAssert([key isKindOfClass:[NSString class]], @"[NSDictionary+HBAdditions] unsupported key type");
hb_value_t *val = [obj hb_value];
if (val)
{
hb_dict_set(result, [key UTF8String], val);
}
}];
return result;
}
#pragma mark - hb_dict_t to NSDictionary
static id valueToObjcValue(const hb_value_t *val)
{
hb_value_type_t val_type = hb_value_type(val);
id result = nil;
switch (val_type)
{
case HB_VALUE_TYPE_INT:
result = @(hb_value_get_int(val));
break;
case HB_VALUE_TYPE_DOUBLE:
result = @(hb_value_get_double(val));
break;
case HB_VALUE_TYPE_BOOL:
result = @((BOOL)hb_value_get_bool(val));
break;
case HB_VALUE_TYPE_STRING:
result = @(hb_value_get_string(val));
break;
case HB_VALUE_TYPE_DICT:
result = convertDictToObjcType(val);
break;
case HB_VALUE_TYPE_ARRAY:
result = convertArrayToObjcType(val);
break;
case HB_VALUE_TYPE_NULL:
result = [NSNull null];
break;
default:
break;
}
return result;
}
static NSDictionary * convertDictToObjcType(const hb_dict_t *dict)
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
hb_dict_iter_t iter;
for (iter = hb_dict_iter_init(dict);
iter != HB_DICT_ITER_DONE;
iter = hb_dict_iter_next(dict, iter))
{
const NSString *key = @(hb_dict_iter_key(iter));
const hb_value_t *val = hb_dict_iter_value(iter);
id objcType = valueToObjcValue(val);
if (objcType)
{
result[key] = objcType;
}
}
return [result copy];
}
static NSArray * convertArrayToObjcType(const hb_value_array_t *array)
{
size_t count = hb_value_array_len(array);
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
for (int ii = 0; ii < count; ii++)
{
const hb_value_t *val = hb_value_array_get(array, ii);
id objcType = valueToObjcValue(val);
if (objcType)
{
[result addObject:objcType];
}
}
return [result copy];
}
@end
|