-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCWValueObject+Private.m
276 lines (251 loc) · 9.22 KB
/
CWValueObject+Private.m
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
//
// CWValueObject+Private.m
// HessianKit
//
// Copyright 2008-2009 Fredrik Olsson, Cocoway. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "CWValueObject+Private.h"
#import <objc/runtime.h>
static NSMutableDictionary* _generatedClasses = nil;
static NSString* CWPropertyNameFromSelector(SEL aSelector) {
NSString* propertyName = NSStringFromSelector(aSelector);
NSUInteger propertyNameLength = [propertyName length];
if ([propertyName hasSuffix:@":"]) {
if (propertyNameLength > 4 && [propertyName hasPrefix:@"set"]) {
NSString* firstChar = [[propertyName substringWithRange:NSMakeRange(3, 1)] lowercaseString];
propertyName = [propertyName substringWithRange:NSMakeRange(4, [propertyName length] - 5)];
propertyName = [firstChar stringByAppendingString:propertyName];
} else {
[NSException raise:NSInternalInconsistencyException format:@"Invalid setter selector"];
}
} else {
if (propertyNameLength > 2 && [propertyName hasPrefix:@"is"]) {
NSString* firstChar = [propertyName substringWithRange:NSMakeRange(2, 1)];
NSString* lowercaseFirstChar = [firstChar lowercaseString];
if (![firstChar isEqualToString:lowercaseFirstChar]) {
propertyName = [propertyName substringFromIndex:3];
propertyName = [firstChar stringByAppendingString:propertyName];
}
}
}
[propertyName retain];
return [propertyName autorelease];
}
static SEL CWSetterSelectorFromPropertyName(NSString* propertyName) {
NSString* firstChar = [[propertyName substringToIndex:1] uppercaseString];
NSString* restOfName = [propertyName substringFromIndex:1];
propertyName = [NSString stringWithFormat:@"set%@%@:", firstChar, restOfName];
SEL aSelector = NSSelectorFromString(propertyName);
return aSelector;
}
static BOOL CWAddProtocolImplementationsToClass(Class aClass, Protocol* aProtocol) {
BOOL success = YES;
unsigned int count = 0;
objc_property_t* propertyList = protocol_copyPropertyList(aProtocol, &count);
for (int index = 0; success && index < count; index++) {
objc_property_t property = propertyList[index];
NSString* propertyName = [NSString stringWithCString:property_getName(property) encoding:NSASCIIStringEncoding];
char type = property_getAttributes(property)[1];
SEL getterSEL;
SEL setterSEL;
if (type == @encode(BOOL)[0]) {
getterSEL = @selector(priv_boolValue);
setterSEL = @selector(priv_setBoolValue:);
} else if (type == @encode(int32_t)[0]) {
getterSEL = @selector(priv_int32Value);
setterSEL = @selector(priv_setInt32Value:);
} else if (type == @encode(int64_t)[0]) {
getterSEL = @selector(priv_int64Value);
setterSEL = @selector(priv_setInt64Value:);
} else if (type == @encode(float)[0]) {
getterSEL = @selector(priv_floatValue);
setterSEL = @selector(priv_setFloatValue:);
} else if (type == @encode(double)[0]) {
getterSEL = @selector(priv_doubleValue);
setterSEL = @selector(priv_setDoubleValue:);
} else if (type == @encode(id)[0]) {
getterSEL = @selector(priv_objectValue);
NSString* propertyAttrs = [NSString stringWithCString:property_getAttributes(property) encoding:NSASCIIStringEncoding];
if ([propertyAttrs hasSuffix:@",C"]) {
setterSEL = @selector(priv_setObjectValueCopy:);
} else {
setterSEL = @selector(priv_setObjectValue:);
}
} else {
NSLog(@"Unknown type %s for property %@ in protocol %@", type, propertyName, NSStringFromProtocol(aProtocol));
success = NO;
}
if (success) {
IMP anIMP = [CWValueObject instanceMethodForSelector:getterSEL];
const char* types = method_getTypeEncoding(class_getInstanceMethod([CWValueObject class], getterSEL));
class_addMethod(aClass, NSSelectorFromString(propertyName), anIMP, types);
anIMP = [CWValueObject instanceMethodForSelector:setterSEL];
types = method_getTypeEncoding(class_getInstanceMethod([CWValueObject class], setterSEL));
success = class_addMethod(aClass, CWSetterSelectorFromPropertyName(propertyName), anIMP, types);
}
}
free(propertyList);
if (success) {
Protocol** protocolList = protocol_copyProtocolList(aProtocol, &count);
for (int index = 0; index < count; index++) {
Protocol* aChildProtocol = protocolList[index];
success = CWAddProtocolImplementationsToClass(aClass, aChildProtocol);
}
free(protocolList);
}
return success;
}
static NSMutableArray* CWAllPropertyNamesForProtocol(Protocol* aProtocol) {
NSMutableArray* names = [NSMutableArray array];
unsigned int count = 0;
objc_property_t* propertyList = protocol_copyPropertyList(aProtocol, &count);
for (int index = 0; index < count; index++) {
objc_property_t property = propertyList[index];
NSString* propertyName = [NSString stringWithCString:property_getName(property) encoding:NSASCIIStringEncoding];
[names addObject:propertyName];
}
free(propertyList);
Protocol** protocolList = protocol_copyProtocolList(aProtocol, &count);
for (int index = 0; index < count; index++) {
Protocol* aChildProtocol = protocolList[index];
[names addObjectsFromArray:CWAllPropertyNamesForProtocol(aChildProtocol)];
}
free(protocolList);
return names;
}
@implementation CWValueObject (Private)
+(void)initialize;
{
if (self == [CWValueObject class]) {
_generatedClasses = [[NSMutableDictionary alloc] init];
}
}
-(BOOL)priv_boolValue;
{
NSNumber* value = [_instanceVariables objectForKey:CWPropertyNameFromSelector(_cmd)];
if (value) {
return [value boolValue];
} else {
return NO;
}
}
-(void)priv_setBoolValue:(BOOL)value;
{
[_instanceVariables setObject:[NSNumber numberWithBool:value] forKey:CWPropertyNameFromSelector(_cmd)];
}
-(int32_t)priv_int32Value;
{
NSNumber* value = [_instanceVariables objectForKey:CWPropertyNameFromSelector(_cmd)];
if (value) {
return [value intValue];
} else {
return 0;
}
}
-(void)priv_setInt32Value:(int32_t)value;
{
[_instanceVariables setObject:[NSNumber numberWithInt:value] forKey:CWPropertyNameFromSelector(_cmd)];
}
-(int64_t)priv_int64Value;
{
NSNumber* value = [_instanceVariables objectForKey:CWPropertyNameFromSelector(_cmd)];
if (value) {
return [value longLongValue];
} else {
return 0;
}
}
-(void)priv_setInt64Value:(int64_t)value;
{
[_instanceVariables setObject:[NSNumber numberWithLongLong:value] forKey:CWPropertyNameFromSelector(_cmd)];
}
-(float)priv_floatValue;
{
NSNumber* value = [_instanceVariables objectForKey:CWPropertyNameFromSelector(_cmd)];
if (value) {
return [value floatValue];
} else {
return 0.0f;
}
}
-(void)priv_setFloatValue:(float)value;
{
[_instanceVariables setObject:[NSNumber numberWithFloat:value] forKey:CWPropertyNameFromSelector(_cmd)];
}
-(double)priv_doubleValue;
{
NSNumber* value = [_instanceVariables objectForKey:CWPropertyNameFromSelector(_cmd)];
if (value) {
return [value doubleValue];
} else {
return 0.0;
}
}
-(void)priv_setDoubleValue:(double)value;
{
[_instanceVariables setObject:[NSNumber numberWithDouble:value] forKey:CWPropertyNameFromSelector(_cmd)];
}
-(id)priv_objectValue;
{
id value = [_instanceVariables objectForKey:CWPropertyNameFromSelector(_cmd)];
if (value) {
if ([value isKindOfClass:[NSNull class]]) {
value = nil;
}
}
return value;
}
-(void)priv_setObjectValue:(id)object;
{
if (!object) {
object = [NSNull null];
}
[_instanceVariables setObject:object forKey:CWPropertyNameFromSelector(_cmd)];
}
-(void)priv_setObjectValueCopy:(id)object;
{
if (!object) {
[_instanceVariables setObject:object forKey:CWPropertyNameFromSelector(_cmd)];
} else {
object = [object copy];
[_instanceVariables setObject:object forKey:CWPropertyNameFromSelector(_cmd)];
[object release];
}
}
-(NSArray*)allPropertyNames;
{
return CWAllPropertyNamesForProtocol(_protocol);
}
-(Class)classForProtocol:(Protocol*)aProtocol;
{
Class aClass = NSClassFromString([_generatedClasses objectForKey:NSStringFromProtocol(aProtocol)]);
if (!aClass) {
NSString* className = NSStringFromClass([CWValueObject class]);
NSString* protocolName = NSStringFromProtocol(aProtocol);
NSString* newClassName = [[NSString alloc] initWithFormat:@"%@%@", className, protocolName];
aClass = objc_allocateClassPair([CWValueObject class], [newClassName cStringUsingEncoding:NSASCIIStringEncoding], 0);
if (aClass) {
if (CWAddProtocolImplementationsToClass(aClass, aProtocol) && class_addProtocol(aClass, aProtocol)) {
objc_registerClassPair(aClass);
[_generatedClasses setObject:NSStringFromClass(aClass) forKey:NSStringFromProtocol(aProtocol)];
} else {
objc_disposeClassPair(aClass);
aClass = Nil;
}
}
[newClassName release];
}
return aClass;
}
@end