Protocol.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 1999-2001, 2005-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. Protocol.h
  25. Copyright 1991-1996 NeXT Software, Inc.
  26. */
  27. #include "objc-private.h"
  28. #undef id
  29. #undef Class
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <mach-o/dyld.h>
  33. #include <mach-o/ldsyms.h>
  34. #include "Protocol.h"
  35. #include "NSObject.h"
  36. // __IncompleteProtocol is used as the return type of objc_allocateProtocol().
  37. // Old ABI uses NSObject as the superclass even though Protocol uses Object
  38. // because the R/R implementation for class Protocol is added at runtime
  39. // by CF, so __IncompleteProtocol would be left without an R/R implementation
  40. // otherwise, which would break ARC.
  41. @interface __IncompleteProtocol : NSObject @end
  42. @implementation __IncompleteProtocol
  43. #if __OBJC2__
  44. // fixme hack - make __IncompleteProtocol a non-lazy class
  45. + (void) load { }
  46. #endif
  47. @end
  48. @implementation Protocol
  49. #if __OBJC2__
  50. // fixme hack - make Protocol a non-lazy class
  51. + (void) load { }
  52. #endif
  53. - (BOOL) conformsTo: (Protocol *)aProtocolObj
  54. {
  55. return protocol_conformsToProtocol(self, aProtocolObj);
  56. }
  57. - (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel
  58. {
  59. #if !__OBJC2__
  60. return lookup_protocol_method((struct old_protocol *)self, aSel,
  61. YES/*required*/, YES/*instance*/,
  62. YES/*recursive*/);
  63. #else
  64. return method_getDescription(protocol_getMethod((struct protocol_t *)self,
  65. aSel, YES, YES, YES));
  66. #endif
  67. }
  68. - (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel
  69. {
  70. #if !__OBJC2__
  71. return lookup_protocol_method((struct old_protocol *)self, aSel,
  72. YES/*required*/, NO/*instance*/,
  73. YES/*recursive*/);
  74. #else
  75. return method_getDescription(protocol_getMethod((struct protocol_t *)self,
  76. aSel, YES, NO, YES));
  77. #endif
  78. }
  79. - (const char *)name
  80. {
  81. return protocol_getName(self);
  82. }
  83. - (BOOL)isEqual:other
  84. {
  85. #if __OBJC2__
  86. // check isKindOf:
  87. Class cls;
  88. Class protoClass = objc_getClass("Protocol");
  89. for (cls = object_getClass(other); cls; cls = cls->superclass) {
  90. if (cls == protoClass) break;
  91. }
  92. if (!cls) return NO;
  93. // check equality
  94. return protocol_isEqual(self, other);
  95. #else
  96. return [other isKindOf:[Protocol class]] && [self conformsTo: other] && [other conformsTo: self];
  97. #endif
  98. }
  99. #if __OBJC2__
  100. - (NSUInteger)hash
  101. {
  102. return 23;
  103. }
  104. #else
  105. - (unsigned)hash
  106. {
  107. return 23;
  108. }
  109. #endif
  110. @end