Object.mm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. /*
  24. Object.m
  25. Copyright 1988-1996 NeXT Software, Inc.
  26. */
  27. #include "objc-private.h"
  28. #undef id
  29. #undef Class
  30. typedef struct objc_class *Class;
  31. typedef struct objc_object *id;
  32. #if __OBJC2__
  33. @implementation Object
  34. + (id)initialize
  35. {
  36. return self;
  37. }
  38. + (id)class
  39. {
  40. return self;
  41. }
  42. -(id) retain
  43. {
  44. return _objc_rootRetain(self);
  45. }
  46. -(void) release
  47. {
  48. _objc_rootRelease(self);
  49. }
  50. -(id) autorelease
  51. {
  52. return _objc_rootAutorelease(self);
  53. }
  54. +(id) retain
  55. {
  56. return self;
  57. }
  58. +(void) release
  59. {
  60. }
  61. +(id) autorelease
  62. {
  63. return self;
  64. }
  65. @end
  66. // __OBJC2__
  67. #else
  68. // not __OBJC2__
  69. #include <stdlib.h>
  70. #include <stdarg.h>
  71. #include <string.h>
  72. #include <malloc/malloc.h>
  73. #include "Object.h"
  74. #include "Protocol.h"
  75. #include "objc-runtime.h"
  76. // Error Messages
  77. static const char
  78. _errShouldHaveImp[] = "should have implemented the '%s' method.",
  79. _errShouldNotImp[] = "should NOT have implemented the '%s' method.",
  80. _errLeftUndone[] = "method '%s' not implemented",
  81. _errBadSel[] = "method %s given invalid selector %s",
  82. _errDoesntRecognize[] = "does not recognize selector %c%s";
  83. @implementation Object
  84. + (id)initialize
  85. {
  86. return self;
  87. }
  88. - (id)awake
  89. {
  90. return self;
  91. }
  92. + (id)poseAs: aFactory
  93. {
  94. return class_poseAs(self, aFactory);
  95. }
  96. + (id)new
  97. {
  98. id newObject = (*_alloc)((Class)self, 0);
  99. Class metaClass = self->ISA();
  100. if (class_getVersion(metaClass) > 1)
  101. return [newObject init];
  102. else
  103. return newObject;
  104. }
  105. + (id)alloc
  106. {
  107. return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());
  108. }
  109. + (id)allocFromZone:(void *) z
  110. {
  111. return (*_zoneAlloc)((Class)self, 0, z);
  112. }
  113. - (id)init
  114. {
  115. return self;
  116. }
  117. - (const char *)name
  118. {
  119. return class_getName(isa);
  120. }
  121. + (const char *)name
  122. {
  123. return class_getName((Class)self);
  124. }
  125. - (unsigned)hash
  126. {
  127. return (unsigned)(((uintptr_t)self) >> 2);
  128. }
  129. - (BOOL)isEqual:anObject
  130. {
  131. return anObject == self;
  132. }
  133. - (id)free
  134. {
  135. return (*_dealloc)(self);
  136. }
  137. + (id)free
  138. {
  139. return nil;
  140. }
  141. - (id)self
  142. {
  143. return self;
  144. }
  145. -(id)class
  146. {
  147. return (id)isa;
  148. }
  149. + (id)class
  150. {
  151. return self;
  152. }
  153. - (void *)zone
  154. {
  155. void *z = malloc_zone_from_ptr(self);
  156. return z ? z : malloc_default_zone();
  157. }
  158. + (id)superclass
  159. {
  160. return self->superclass;
  161. }
  162. - (id)superclass
  163. {
  164. return isa->superclass;
  165. }
  166. + (int) version
  167. {
  168. return class_getVersion((Class)self);
  169. }
  170. + (id)setVersion: (int) aVersion
  171. {
  172. class_setVersion((Class)self, aVersion);
  173. return self;
  174. }
  175. - (BOOL)isKindOf:aClass
  176. {
  177. Class cls;
  178. for (cls = isa; cls; cls = cls->superclass)
  179. if (cls == (Class)aClass)
  180. return YES;
  181. return NO;
  182. }
  183. - (BOOL)isMemberOf:aClass
  184. {
  185. return isa == (Class)aClass;
  186. }
  187. - (BOOL)isKindOfClassNamed:(const char *)aClassName
  188. {
  189. Class cls;
  190. for (cls = isa; cls; cls = cls->superclass)
  191. if (strcmp(aClassName, class_getName(cls)) == 0)
  192. return YES;
  193. return NO;
  194. }
  195. - (BOOL)isMemberOfClassNamed:(const char *)aClassName
  196. {
  197. return strcmp(aClassName, class_getName(isa)) == 0;
  198. }
  199. + (BOOL)instancesRespondTo:(SEL)aSelector
  200. {
  201. return class_respondsToMethod((Class)self, aSelector);
  202. }
  203. - (BOOL)respondsTo:(SEL)aSelector
  204. {
  205. return class_respondsToMethod(isa, aSelector);
  206. }
  207. - (id)copy
  208. {
  209. return [self copyFromZone: [self zone]];
  210. }
  211. - (id)copyFromZone:(void *)z
  212. {
  213. return (*_zoneCopy)(self, 0, z);
  214. }
  215. - (IMP)methodFor:(SEL)aSelector
  216. {
  217. return class_lookupMethod(isa, aSelector);
  218. }
  219. + (IMP)instanceMethodFor:(SEL)aSelector
  220. {
  221. return class_lookupMethod(self, aSelector);
  222. }
  223. - (id)perform:(SEL)aSelector
  224. {
  225. if (aSelector)
  226. return ((id(*)(id, SEL))objc_msgSend)(self, aSelector);
  227. else
  228. return [self error:_errBadSel, sel_getName(_cmd), aSelector];
  229. }
  230. - (id)perform:(SEL)aSelector with:anObject
  231. {
  232. if (aSelector)
  233. return ((id(*)(id, SEL, id))objc_msgSend)(self, aSelector, anObject);
  234. else
  235. return [self error:_errBadSel, sel_getName(_cmd), aSelector];
  236. }
  237. - (id)perform:(SEL)aSelector with:obj1 with:obj2
  238. {
  239. if (aSelector)
  240. return ((id(*)(id, SEL, id, id))objc_msgSend)(self, aSelector, obj1, obj2);
  241. else
  242. return [self error:_errBadSel, sel_getName(_cmd), aSelector];
  243. }
  244. - (id)subclassResponsibility:(SEL)aSelector
  245. {
  246. return [self error:_errShouldHaveImp, sel_getName(aSelector)];
  247. }
  248. - (id)notImplemented:(SEL)aSelector
  249. {
  250. return [self error:_errLeftUndone, sel_getName(aSelector)];
  251. }
  252. - (id)doesNotRecognize:(SEL)aMessage
  253. {
  254. return [self error:_errDoesntRecognize,
  255. class_isMetaClass(isa) ? '+' : '-', sel_getName(aMessage)];
  256. }
  257. - (id)error:(const char *)aCStr, ...
  258. {
  259. va_list ap;
  260. va_start(ap,aCStr);
  261. (*_error)(self, aCStr, ap);
  262. _objc_error (self, aCStr, ap); /* In case (*_error)() returns. */
  263. va_end(ap);
  264. return nil;
  265. }
  266. - (void) printForDebugger:(void *)stream
  267. {
  268. }
  269. - (id)write:(void *) stream
  270. {
  271. return self;
  272. }
  273. - (id)read:(void *) stream
  274. {
  275. return self;
  276. }
  277. - (id)forward: (SEL) sel : (marg_list) args
  278. {
  279. return [self doesNotRecognize: sel];
  280. }
  281. /* this method is not part of the published API */
  282. - (unsigned)methodArgSize:(SEL)sel
  283. {
  284. Method method = class_getInstanceMethod((Class)isa, sel);
  285. if (! method) return 0;
  286. return method_getSizeOfArguments(method);
  287. }
  288. - (id)performv: (SEL) sel : (marg_list) args
  289. {
  290. unsigned size;
  291. // Messages to nil object always return nil
  292. if (! self) return nil;
  293. // Calculate size of the marg_list from the method's
  294. // signature. This looks for the method in self
  295. // and its superclasses.
  296. size = [self methodArgSize: sel];
  297. // If neither self nor its superclasses implement
  298. // it, forward the message because self might know
  299. // someone who does. This is a "chained" forward...
  300. if (! size) return [self forward: sel: args];
  301. // Message self with the specified selector and arguments
  302. return objc_msgSendv (self, sel, size, args);
  303. }
  304. /* Testing protocol conformance */
  305. - (BOOL) conformsTo: (Protocol *)aProtocolObj
  306. {
  307. return [(id)isa conformsTo:aProtocolObj];
  308. }
  309. + (BOOL) conformsTo: (Protocol *)aProtocolObj
  310. {
  311. Class cls;
  312. for (cls = self; cls; cls = cls->superclass)
  313. {
  314. if (class_conformsToProtocol(cls, aProtocolObj)) return YES;
  315. }
  316. return NO;
  317. }
  318. /* Looking up information for a method */
  319. - (struct objc_method_description *) descriptionForMethod:(SEL)aSelector
  320. {
  321. Class cls;
  322. struct objc_method_description *m;
  323. /* Look in the protocols first. */
  324. for (cls = isa; cls; cls = cls->superclass)
  325. {
  326. if (cls->ISA()->version >= 3)
  327. {
  328. struct objc_protocol_list *protocols =
  329. (struct objc_protocol_list *)cls->protocols;
  330. while (protocols)
  331. {
  332. int i;
  333. for (i = 0; i < protocols->count; i++)
  334. {
  335. Protocol *p = protocols->list[i];
  336. if (class_isMetaClass(cls))
  337. m = [p descriptionForClassMethod:aSelector];
  338. else
  339. m = [p descriptionForInstanceMethod:aSelector];
  340. if (m) {
  341. return m;
  342. }
  343. }
  344. if (cls->ISA()->version <= 4)
  345. break;
  346. protocols = protocols->next;
  347. }
  348. }
  349. }
  350. /* Then try the class implementations. */
  351. for (cls = isa; cls; cls = cls->superclass) {
  352. void *iterator = 0;
  353. int i;
  354. struct objc_method_list *mlist;
  355. while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
  356. for (i = 0; i < mlist->method_count; i++)
  357. if (mlist->method_list[i].method_name == aSelector) {
  358. m = (struct objc_method_description *)&mlist->method_list[i];
  359. return m;
  360. }
  361. }
  362. }
  363. return 0;
  364. }
  365. + (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSelector
  366. {
  367. Class cls;
  368. /* Look in the protocols first. */
  369. for (cls = self; cls; cls = cls->superclass)
  370. {
  371. if (cls->ISA()->version >= 3)
  372. {
  373. struct objc_protocol_list *protocols =
  374. (struct objc_protocol_list *)cls->protocols;
  375. while (protocols)
  376. {
  377. int i;
  378. for (i = 0; i < protocols->count; i++)
  379. {
  380. Protocol *p = protocols->list[i];
  381. struct objc_method_description *m;
  382. if ((m = [p descriptionForInstanceMethod:aSelector]))
  383. return m;
  384. }
  385. if (cls->ISA()->version <= 4)
  386. break;
  387. protocols = protocols->next;
  388. }
  389. }
  390. }
  391. /* Then try the class implementations. */
  392. for (cls = self; cls; cls = cls->superclass) {
  393. void *iterator = 0;
  394. int i;
  395. struct objc_method_list *mlist;
  396. while ( (mlist = class_nextMethodList( cls, &iterator )) ) {
  397. for (i = 0; i < mlist->method_count; i++)
  398. if (mlist->method_list[i].method_name == aSelector) {
  399. struct objc_method_description *m;
  400. m = (struct objc_method_description *)&mlist->method_list[i];
  401. return m;
  402. }
  403. }
  404. }
  405. return 0;
  406. }
  407. /* Obsolete methods (for binary compatibility only). */
  408. + (id)superClass
  409. {
  410. return [self superclass];
  411. }
  412. - (id)superClass
  413. {
  414. return [self superclass];
  415. }
  416. - (BOOL)isKindOfGivenName:(const char *)aClassName
  417. {
  418. return [self isKindOfClassNamed: aClassName];
  419. }
  420. - (BOOL)isMemberOfGivenName:(const char *)aClassName
  421. {
  422. return [self isMemberOfClassNamed: aClassName];
  423. }
  424. - (struct objc_method_description *) methodDescFor:(SEL)aSelector
  425. {
  426. return [self descriptionForMethod: aSelector];
  427. }
  428. + (struct objc_method_description *) instanceMethodDescFor:(SEL)aSelector
  429. {
  430. return [self descriptionForInstanceMethod: aSelector];
  431. }
  432. - (id)findClass:(const char *)aClassName
  433. {
  434. return objc_lookUpClass(aClassName);
  435. }
  436. - (id)shouldNotImplement:(SEL)aSelector
  437. {
  438. return [self error:_errShouldNotImp, sel_getName(aSelector)];
  439. }
  440. @end
  441. #endif