-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXObject.m
More file actions
160 lines (129 loc) · 3.75 KB
/
XObject.m
File metadata and controls
160 lines (129 loc) · 3.75 KB
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
#import <Foundation/Foundation.h>
#import </usr/include/objc/objc-class.h>
#import "XObject.h"
#import "XArray.h"
#import "XHash.h"
#import "XString.h"
/*
#import <Foundation/Foundation.h>
@interface XObject : NSObject {
id _attributes;
id _delegations;
}
@end
*/
// todo: z: need to make sure that XObject implements all of the Key-Value Coding interface correcty (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/doc/uid/20000471-BABEHECF)
// e.g. attrReaders (without accessors won't be set correct in initWithHash:)
@implementation XObject
// initializer/class factory methods/constructors/destructor
- (id) init {
if (self = [super init]) {
_attributes = [[XHash alloc] init];
_delegations = [[XHash alloc] init];
[_delegations add:@"specificDelegations" value:[XHash empty]];
[_delegations add:@"delegates" value:[XArray empty]];
}
return self;
}
// todo: z: needs to get _attributes and _delegations
- (id) initWithCoder: (id)coder {
return [self init];
}
- (void) dealloc {
[_attributes release];
[_delegations release];
[super dealloc];
}
// public class methods
+ (id) attrAccessors: (id)firstItem, ... {
va_list argList;
va_start(argList, firstItem);
id array = [XArray with:firstItem vaList:argList];
va_end(argList);
return [self _attrAccessors:array];
}
+ (id) attrReaders: (id)firstItem, ... {
va_list argList;
va_start(argList, firstItem);
id readableAttributes = [XArray with:firstItem vaList:argList];
va_end(argList);
return [self _attrReaders:readableAttributes];
}
+ (id) attrWriters: (id)firstItem, ... {
va_list argList;
va_start(argList, firstItem);
id writableAttributes = [XArray with:firstItem vaList:argList];
va_end(argList);
return [self _attrWriters:writableAttributes];
}
// public instance methods
- (void) encodeWithCoder: (id)coder {
// todo: z: needs to encode _attributes and _delegations
}
- (id) setValue: (id)value forUndefinedKey: (id)key {
// NSLog(@"NSObject+X: not going to setValue %@ for key %@", value, key);
return self;
}
// protected class methods
+ (id) _attrAccessors: (id)accessors {
[self _attrReaders:accessors];
[self _attrWriters:accessors];
return self;
}
+ (id) _attrReaders: (id)readers {
for (id item in readers) {
[self aliasInstanceMethod:@"_getHashAttrByCmd" as:item];
}
return self;
}
+ (id) _attrWriters: (id)writers {
for (id item in writers) {
[self aliasInstanceMethod:@"_setHashAttrByCmd:" as:[XString withFormat:@"set%@:", [item asPascalCase], nil]];
}
return self;
}
// protected instance methods
- (id) _attributes {
return _attributes;
}
// z: undefined behavior if method is not of the form setX:
// todo: z: kill this here
- (id) _attributeSetterWithoutSetAndColon: (id)method {
NSRange range;
range.location = 3;
range.length = [method length] - 4;
return [[method substringWithRange:range] asCamelCase];
}
- (id) _delegates {
return [_delegations get:@"delegates"];
}
- (BOOL) _hasDelegations {
return YES;
}
- (id) _getHashAttrByCmd {
id key = NSStringFromSelector(_cmd);
if ([[self _attributes] containsKey:key]) {
return [[self _attributes] get:key];
}
return nil;
}
- (id) _setHashAttrByCmd: (id)value {
char buffer[128] = "";
const char* selectorName = sel_getName(_cmd);
int length = strlen(selectorName);
// todo: z: what about memory consequences?
strncpy(buffer, selectorName+3, length - 4);
buffer[0] += 32;
id key = [NSString stringWithUTF8String:buffer];
if (value) {
[[self _attributes] set:key to:value];
}
else {
[[self _attributes] remove:key];
}
return self;
}
- (id) _delegations {
return [_delegations get:@"specificDelegations"];
}
@end