19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#import "OFObject.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"
#import <objc/objc-api.h>
#ifdef __objc_INCLUDE_GNU
#import <objc/sarray.h>
#else
#import <objc/runtime.h>
#endif
struct pre_ivar {
void **memchunks;
size_t memchunks_size;
size_t retain_count;
};
|
|
|
|
>
|
|
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#import "OFObject.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"
#import <objc/objc-api.h>
#ifdef OF_APPLE_RUNTIME
#import <objc/runtime.h>
#endif
#ifdef OF_GNU_RUNTIME
#import <objc/sarray.h>
#endif
struct pre_ivar {
void **memchunks;
size_t memchunks_size;
size_t retain_count;
};
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
+ (void)initialize
{
}
+ alloc
{
OFObject *instance;
#ifdef __objc_INCLUDE_GNU
size_t isize = class_get_instance_size(self);
#else
size_t isize = class_getInstanceSize(self);
#endif
if ((instance = malloc(isize + PRE_IVAR_ALIGN)) == NULL) {
alloc_failed_exception.isa = [OFAllocFailedException class];
@throw (OFAllocFailedException*)&alloc_failed_exception;
}
|
|
|
|
|
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
+ (void)initialize
{
}
+ alloc
{
OFObject *instance;
#ifdef OF_APPLE_RUNTIME
size_t isize = class_getInstanceSize(self);
#else
size_t isize = class_get_instance_size(self);
#endif
if ((instance = malloc(isize + PRE_IVAR_ALIGN)) == NULL) {
alloc_failed_exception.isa = [OFAllocFailedException class];
@throw (OFAllocFailedException*)&alloc_failed_exception;
}
|
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
|
+ (Class)class
{
return self;
}
+ (const char*)className
{
#ifdef __objc_INCLUDE_GNU
return class_get_class_name(self);
#else
return class_getName(self);
#endif
}
+ (BOOL)instancesRespondToSelector: (SEL)selector
{
#ifdef __objc_INCLUDE_GNU
return class_get_instance_method(self, selector) != METHOD_NULL;
#else
return class_respondsToSelector(self, selector);
#endif
}
+ (BOOL)conformsToProtocol: (Protocol*)protocol
{
#ifdef __objc_INCLUDE_GNU
Class c;
struct objc_protocol_list *pl;
size_t i;
for (c = self; c != Nil; c = class_get_super_class(c))
for (pl = c->protocols; pl != NULL; pl = pl->next)
for (i = 0; i < pl->count; i++)
if ([pl->list[i] conformsToProtocol: protocol])
return YES;
return NO;
#else
Class c;
for (c = self; c != Nil; c = class_getSuperclass(c))
if (class_conformsToProtocol(c, protocol))
return YES;
return NO;
#endif
}
+ (IMP)instanceMethodForSelector: (SEL)selector
{
#ifdef __objc_INCLUDE_GNU
return method_get_imp(class_get_instance_method(self, selector));
#else
return class_getMethodImplementation(self, selector);
#endif
}
+ (IMP)setImplementation: (IMP)newimp
forMethod: (SEL)selector
{
#ifdef __objc_INCLUDE_GNU
Method_t method = class_get_instance_method(self, selector);
IMP oldimp;
if (method == NULL)
@throw [OFInvalidArgumentException newWithClass: self
selector: _cmd];
if ((oldimp = method_get_imp(method)) == (IMP)0 || newimp == (IMP)0)
@throw [OFInvalidArgumentException newWithClass: self
selector: _cmd];
method->method_imp = newimp;
/* Update the dtable if necessary */
if (sarray_get_safe(((Class)self)->dtable,
(sidx)method->method_name->sel_id))
sarray_at_put_safe(((Class)self)->dtable,
(sidx)method->method_name->sel_id, method->method_imp);
return oldimp;
#else
Method method;
if ((method = class_getInstanceMethod(self, selector)) == NULL)
@throw [OFInvalidArgumentException newWithClass: self
selector: _cmd];
return method_setImplementation(method, newimp);
#endif
}
+ (IMP)replaceMethod: (SEL)selector
withMethodFromClass: (Class)class;
{
IMP newimp;
#ifdef __objc_INCLUDE_GNU
newimp = method_get_imp(class_get_instance_method(class, selector));
#else
newimp = class_getMethodImplementation(class, selector);
#endif
return [self setImplementation: newimp
forMethod: selector];
}
- init
{
return self;
}
- (Class)class
{
return isa;
}
- (const char*)className
{
#ifdef __objc_INCLUDE_GNU
return object_get_class_name(self);
#else
return class_getName(isa);
#endif
}
- (BOOL)isKindOfClass: (Class)class
{
Class iter;
#ifdef __objc_INCLUDE_GNU
for (iter = isa; iter != Nil; iter = class_get_super_class(iter))
#else
for (iter = isa; iter != Nil; iter = class_getSuperclass(iter))
#endif
if (iter == class)
return YES;
return NO;
}
- (BOOL)respondsToSelector: (SEL)selector
{
#ifdef __objc_INCLUDE_GNU
if (object_is_instance(self))
return class_get_instance_method(isa, selector) != METHOD_NULL;
else
return class_get_class_method(isa, selector) != METHOD_NULL;
#else
return class_respondsToSelector(isa, selector);
#endif
}
- (BOOL)conformsToProtocol: (Protocol*)protocol
{
return [isa conformsToProtocol: protocol];
}
- (IMP)methodForSelector: (SEL)selector
{
#ifdef __objc_INCLUDE_GNU
if (object_is_instance(self))
return method_get_imp(class_get_instance_method(isa, selector));
else
return method_get_imp(class_get_class_method(isa, selector));
#else
return class_getMethodImplementation(isa, selector);
#endif
}
- (BOOL)isEqual: (id)obj
{
/* Classes containing data should reimplement this! */
return (self == obj ? YES : NO);
|
|
|
|
|
|
|
>
>
|
>
>
>
>
>
>
<
<
<
<
<
<
<
<
|
|
|
>
>
|
>
>
>
>
>
>
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
>
>
<
<
|
>
>
<
<
|
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
|
+ (Class)class
{
return self;
}
+ (const char*)className
{
#ifdef OF_APPLE_RUNTIME
return class_getName(self);
#else
return class_get_class_name(self);
#endif
}
+ (BOOL)instancesRespondToSelector: (SEL)selector
{
#ifdef OF_APPLE_RUNTIME
return class_respondsToSelector(self, selector);
#else
return class_get_instance_method(self, selector) != METHOD_NULL;
#endif
}
+ (BOOL)conformsToProtocol: (Protocol*)protocol
{
#ifdef OF_APPLE_RUNTIME
Class c;
for (c = self; c != Nil; c = class_getSuperclass(c))
if (class_conformsToProtocol(c, protocol))
return YES;
return NO;
#else
Class c;
struct objc_protocol_list *pl;
size_t i;
for (c = self; c != Nil; c = class_get_super_class(c))
for (pl = c->protocols; pl != NULL; pl = pl->next)
for (i = 0; i < pl->count; i++)
if ([pl->list[i] conformsToProtocol: protocol])
return YES;
return NO;
#endif
}
+ (IMP)instanceMethodForSelector: (SEL)selector
{
#ifdef OF_APPLE_RUNTIME
return class_getMethodImplementation(self, selector);
#else
return method_get_imp(class_get_instance_method(self, selector));
#endif
}
+ (IMP)setImplementation: (IMP)newimp
forMethod: (SEL)selector
{
#ifdef OF_APPLE_RUNTIME
Method method;
if ((method = class_getInstanceMethod(self, selector)) == NULL)
@throw [OFInvalidArgumentException newWithClass: self
selector: _cmd];
return method_setImplementation(method, newimp);
#else
Method_t method = class_get_instance_method(self, selector);
IMP oldimp;
if (method == NULL)
@throw [OFInvalidArgumentException newWithClass: self
selector: _cmd];
if ((oldimp = method_get_imp(method)) == (IMP)0 || newimp == (IMP)0)
@throw [OFInvalidArgumentException newWithClass: self
selector: _cmd];
method->method_imp = newimp;
/* Update the dtable if necessary */
if (sarray_get_safe(((Class)self)->dtable,
(sidx)method->method_name->sel_id))
sarray_at_put_safe(((Class)self)->dtable,
(sidx)method->method_name->sel_id, method->method_imp);
return oldimp;
#endif
}
+ (IMP)replaceMethod: (SEL)selector
withMethodFromClass: (Class)class;
{
IMP newimp;
#ifdef OF_APPLE_RUNTIME
newimp = class_getMethodImplementation(class, selector);
#else
newimp = method_get_imp(class_get_instance_method(class, selector));
#endif
return [self setImplementation: newimp
forMethod: selector];
}
- init
{
return self;
}
- (Class)class
{
return isa;
}
- (const char*)className
{
#ifdef OF_APPLE_RUNTIME
return class_getName(isa);
#else
return object_get_class_name(self);
#endif
}
- (BOOL)isKindOfClass: (Class)class
{
Class iter;
#ifdef OF_APPLE_RUNTIME
for (iter = isa; iter != Nil; iter = class_getSuperclass(iter))
#else
for (iter = isa; iter != Nil; iter = class_get_super_class(iter))
#endif
if (iter == class)
return YES;
return NO;
}
- (BOOL)respondsToSelector: (SEL)selector
{
#ifdef OF_APPLE_RUNTIME
return class_respondsToSelector(isa, selector);
#else
if (object_is_instance(self))
return class_get_instance_method(isa, selector) != METHOD_NULL;
else
return class_get_class_method(isa, selector) != METHOD_NULL;
#endif
}
- (BOOL)conformsToProtocol: (Protocol*)protocol
{
return [isa conformsToProtocol: protocol];
}
- (IMP)methodForSelector: (SEL)selector
{
#ifdef OF_APPLE_RUNTIME
return class_getMethodImplementation(isa, selector);
#else
if (object_is_instance(self))
return method_get_imp(class_get_instance_method(isa, selector));
else
return method_get_imp(class_get_class_method(isa, selector));
#endif
}
- (BOOL)isEqual: (id)obj
{
/* Classes containing data should reimplement this! */
return (self == obj ? YES : NO);
|