-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOCVField.m
184 lines (160 loc) · 3.34 KB
/
OCVField.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
//
// OCVField.m
// ObjCViz
//
// Created by Olivier Gutknecht on 12/17/06.
// Copyright 2006 No Distance. See LICENSE.txt file.
//
#import "OCVField.h"
#include <objc/objc-class.h>
@implementation OCVField
{
Ivar instanceVariable;
}
-(id)initWithIVar:(Ivar)ivar
{
self = [super init];
if (self)
{
instanceVariable = ivar;
}
return self;
}
-(void)dealloc
{
instanceVariable = NULL;
[super dealloc];
}
- (const char *)cName
{
return ivar_getName(instanceVariable);
}
- (NSString*)name
{
return [NSString stringWithUTF8String: [self cName]];
}
- (const char *)objcType
{
return ivar_getTypeEncoding(instanceVariable);
}
- (char)typeEncoding
{
return [self objcType][0];
}
- (NSString *) typeForCode
{
char fc = [self typeEncoding];
NSString *rs;
switch (fc) {
case '@' :
rs = @"id";
break;
case '#' :
rs = @"Class";
break;
case ':' :
rs = @"SEL";
break;
case 'c' :
rs = @"char";
break;
case 'C' :
rs = @"unsigned char";
break;
case 's' :
rs = @"short";
break;
case 'S' :
rs = @"unsigned short";
break;
case 'i' :
rs = @"int";
break;
case 'I' :
rs = @"unsigned int";
break;
case 'l' :
rs = @"long";
break;
case 'L' :
rs = @"unsigned long";
break;
case 'q' :
rs = @"long long";
break;
case 'Q' :
rs = @"unsigned long long";
break;
case 'f' :
rs = @"float";
break;
case 'd' :
rs = @"double";
break;
case 'v' :
rs = @"void";
break;
case '*' :
case '%' : // _C_ATOM
rs = @"char *";
break;
default :
rs = @"UNKNOWN";
break;
}
return rs;
}
- (BOOL)isPrimitive
{
char encoding = [self typeEncoding];
BOOL isObject = (encoding == '@');
return (!isObject);
}
-(id)valueForObject:(id)o
{
if ([self isPrimitive])
return nil;
id res;
res = object_getIvar(o, instanceVariable);
return res;
}
-(NSString*)primitiveValueDescriptionForObject:(id)anObject
{
id value = [anObject valueForKey: [self name]];
return [NSString stringWithFormat: @"%@", value];
}
-(NSString*)description
{
return [NSString stringWithFormat:@"%@ %@",[self typeForCode],[self name]];
}
@end
@implementation NSObject (OCVReflect)
- (NSArray*)OCV_fieldsForClass:(Class)klass
{
NSMutableArray* array = [NSMutableArray array];
int i;
Ivar rtIvar;
unsigned int ivarCount = 0;
Class superclass = Nil;
Ivar *ivars = class_copyIvarList(klass, &ivarCount);
if (ivars!= NULL && (ivarCount>0)) {
for ( i = 0; i < ivarCount; ++i ) {
rtIvar = ivars[i];
OCVField* t = [[OCVField alloc] initWithIVar:rtIvar];
[array addObject:t];
[t release];
}
free(ivars);
ivars = NULL;
}
superclass = class_getSuperclass(klass);
if (superclass!=Nil)
[array addObjectsFromArray:[self OCV_fieldsForClass:superclass]];
return array;
}
- (NSArray*)OCV_fields
{
Class klass = [self class];
return [self OCV_fieldsForClass:klass];
}
@end