-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCWValueObject.m
108 lines (97 loc) · 3.33 KB
/
CWValueObject.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
//
// CWValueObject.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.h"
#import "CWValueObject+Private.h"
#import <objc/runtime.h>
@implementation CWValueObject
@synthesize protocol = _protocol;
- (void)dealloc
{
[_instanceVariables release];
_instanceVariables = nil;
[super dealloc];
}
+(CWValueObject*)valueObjectWithProtocol:(Protocol*)aProtocol;
{
CWValueObject* valueObject = [[CWValueObject alloc] initWithProtocol:aProtocol];
if (valueObject) {
return [valueObject autorelease];
} else {
return nil;
}
}
-(id)initWithProtocol:(Protocol*)aProtocol;
{
Class aClass = [self classForProtocol:aProtocol];
[self release];
self = nil;
if (aClass) {
self = class_createInstance(aClass, 0);
self = [self init];
self->_protocol = aProtocol;
self->_instanceVariables = [[NSMutableDictionary alloc] init];
}
return self;
}
-(id)initWithCoder:(NSCoder *)decoder;
{
if (![decoder allowsKeyedCoding]) {
[NSException raise:NSInvalidArgumentException format:@"Only KeyedCoding is supported"];
}
if (self) {
NSArray* propertyNames = [self allPropertyNames];
for (NSString* propertyName in propertyNames) {
id value = [decoder decodeObjectForKey:propertyName];
[self setValue:value forKey:propertyName];
}
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)encoder;
{
if (![encoder allowsKeyedCoding]) {
[NSException raise:NSInvalidArgumentException format:@"Only KeyedCoding is supported"];
}
NSArray* propertyNames = [self allPropertyNames];
for (NSString* propertyName in propertyNames) {
id value = [self valueForKey:propertyName];
objc_property_t property = protocol_getProperty(self.protocol, [propertyName cStringUsingEncoding:NSASCIIStringEncoding], YES, YES);
char type = property_getAttributes(property)[1];
if (type == @encode(BOOL)[0]) {
[encoder encodeBool:[value boolValue] forKey:propertyName];
} else if (type == @encode(int32_t)[0]) {
[encoder encodeInt32:[value intValue] forKey:propertyName];
} else if (type == @encode(int64_t)[0]) {
[encoder encodeInt64:[value longLongValue] forKey:propertyName];
} else if (type == @encode(float)[0]) {
[encoder encodeFloat:[value floatValue] forKey:propertyName];
} else if (type == @encode(double)[0]) {
[encoder encodeDouble:[value doubleValue] forKey:propertyName];
} else if (type == @encode(id)[0]){
[encoder encodeObject:value forKey:propertyName];
} else {
[NSException raise:NSInternalInconsistencyException
format:@"Unknown type %s for property %@ in protocol %@", type, propertyName, NSStringFromProtocol(self.protocol)];
}
}
}
-(NSString*)description;
{
return [_instanceVariables description];
}
@end