Differences From Artifact [95a9dc34dd]:
- File
src/OFObject.m
— part of check-in
[54e971a3a6]
at
2010-04-01 20:57:30
on branch trunk
— Improve method replacing when using the Apple runtime.
This prevents replacing the method of a superclass when the method is
not implemented in the class and creates a new method instead. (user: js, size: 15069) [annotate] [blame] [check-ins using]
To Artifact [6d2f924ac1]:
- File src/OFObject.m — part of check-in [8c2755723a] at 2010-04-01 23:51:01 on branch trunk — Add support for ObjFW-RT, the ObjFW Objective C runtime. (user: js, size: 15484) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
19 20 21 22 23 24 25 | #include <assert.h> #import "OFObject.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #import "macros.h" | > | | > < | > | | 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 | #include <assert.h> #import "OFObject.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #import "macros.h" #if defined(OF_OBJFW_RUNTIME) # import <objfw-rt.h> #elif defined(OF_APPLE_RUNTIME) # import <objc/objc-api.h> # import <objc/runtime.h> #elif defined(OF_GNU_RUNTIME) # import <objc/objc-api.h> # import <objc/sarray.h> #endif #ifdef _WIN32 # include <windows.h> #endif #ifdef OF_ATOMIC_OPS # import "atomic.h" #else # import "threading.h" #endif /* A few macros to reduce #ifdefs */ #ifdef OF_GNU_RUNTIME # define class_getInstanceSize class_get_instance_size # define class_getName class_get_class_name # define class_getSuperclass class_get_super_class #endif /// \cond internal struct pre_ivar { |
︙ | ︙ | |||
179 180 181 182 183 184 185 | + (Class)superclass { return class_getSuperclass(self); } + (BOOL)instancesRespondToSelector: (SEL)selector { | | | | | < < < < < < < < > > > > > > > > > > | > > | | 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 | + (Class)superclass { return class_getSuperclass(self); } + (BOOL)instancesRespondToSelector: (SEL)selector { #ifdef OF_GNU_RUNTIME return class_get_instance_method(self, selector) != METHOD_NULL; #else return class_respondsToSelector(self, selector); #endif } + (BOOL)conformsToProtocol: (Protocol*)protocol { #ifdef OF_GNU_RUNTIME 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 { #if defined(OF_OBJFW_RUNTIME) return objc_get_instance_method(self, selector); #elif defined(OF_APPLE_RUNTIME) return class_getMethodImplementation(self, selector); #else return method_get_imp(class_get_instance_method(self, selector)); #endif } + (IMP)setImplementation: (IMP)newimp forClassMethod: (SEL)selector { #if defined(OF_OBJFW_RUNTIME) return objc_replace_class_method(self, selector, newimp); #elif defined(OF_APPLE_RUNTIME) return class_replaceMethod(self->isa, selector, newimp, method_getTypeEncoding(class_getClassMethod(self, selector))); #else Method_t method; IMP oldimp; /* The class method is the instance method of the meta class */ |
︙ | ︙ | |||
257 258 259 260 261 262 263 | } + (IMP)replaceClassMethod: (SEL)selector withClassMethodFromClass: (Class)class; { IMP newimp; | > > | > > | | 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | } + (IMP)replaceClassMethod: (SEL)selector withClassMethodFromClass: (Class)class; { IMP newimp; #if defined(OF_OBJFW_RUNTIME) newimp = objc_get_class_method(class, selector); #elif defined(OF_APPLE_RUNTIME) newimp = method_getImplementation(class_getClassMethod(class, selector)); #else /* The class method is the instance method of the meta class */ newimp = method_get_imp(class_get_instance_method(class->class_pointer, selector)); #endif return [self setImplementation: newimp forClassMethod: selector]; } + (IMP)setImplementation: (IMP)newimp forInstanceMethod: (SEL)selector { #if defined(OF_OBJFW_RUNTIME) return objc_replace_instance_method(self, selector, newimp); #elif defined(OF_APPLE_RUNTIME) return class_replaceMethod(self, selector, newimp, method_getTypeEncoding(class_getInstanceMethod(self, selector))); #else Method_t method = class_get_instance_method(self, selector); IMP oldimp; if (method == NULL) |
︙ | ︙ | |||
305 306 307 308 309 310 311 | } + (IMP)replaceInstanceMethod: (SEL)selector withInstanceMethodFromClass: (Class)class; { IMP newimp; | > > | | 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | } + (IMP)replaceInstanceMethod: (SEL)selector withInstanceMethodFromClass: (Class)class; { IMP newimp; #if defined(OF_OBJFW_RUNTIME) newimp = objc_get_instance_method(class, selector); #elif defined(OF_APPLE_RUNTIME) newimp = class_getMethodImplementation(class, selector); #else newimp = method_get_imp(class_get_instance_method(class, selector)); #endif return [self setImplementation: newimp forInstanceMethod: selector]; |
︙ | ︙ | |||
327 328 329 330 331 332 333 | - (Class)class { return isa; } - (const char*)className { | | | | | < < > > < | < < | 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | - (Class)class { return isa; } - (const char*)className { #ifdef OF_GNU_RUNTIME return object_get_class_name(self); #else return class_getName(isa); #endif } - (BOOL)isKindOfClass: (Class)class { Class iter; for (iter = isa; iter != Nil; iter = class_getSuperclass(iter)) if (iter == class) return YES; return NO; } - (BOOL)respondsToSelector: (SEL)selector { #ifdef OF_GNU_RUNTIME 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 OF_APPLE_RUNTIME return class_getMethodImplementation(isa, selector); #else return objc_msg_lookup(self, selector); #endif } - (BOOL)isEqual: (OFObject*)obj { /* Classes containing data should reimplement this! */ return (self == obj ? YES : NO); |
︙ | ︙ |