Protocol.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  42. @end
  43. #if __OBJC2__
  44. __attribute__((objc_nonlazy_class))
  45. #endif
  46. @implementation __IncompleteProtocol
  47. @end
  48. #if __OBJC2__
  49. __attribute__((objc_nonlazy_class))
  50. #endif
  51. @implementation Protocol
  52. - (BOOL) conformsTo: (Protocol *)aProtocolObj
  53. {
  54. return protocol_conformsToProtocol(self, aProtocolObj);
  55. }
  56. - (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel
  57. {
  58. #if !__OBJC2__
  59. return lookup_protocol_method((struct old_protocol *)self, aSel,
  60. YES/*required*/, YES/*instance*/,
  61. YES/*recursive*/);
  62. #else
  63. return method_getDescription(protocol_getMethod((struct protocol_t *)self,
  64. aSel, YES, YES, YES));
  65. #endif
  66. }
  67. - (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel
  68. {
  69. #if !__OBJC2__
  70. return lookup_protocol_method((struct old_protocol *)self, aSel,
  71. YES/*required*/, NO/*instance*/,
  72. YES/*recursive*/);
  73. #else
  74. return method_getDescription(protocol_getMethod((struct protocol_t *)self,
  75. aSel, YES, NO, YES));
  76. #endif
  77. }
  78. - (const char *)name
  79. {
  80. return protocol_getName(self);
  81. }
  82. - (BOOL)isEqual:other
  83. {
  84. #if __OBJC2__
  85. // check isKindOf:
  86. Class cls;
  87. Class protoClass = objc_getClass("Protocol");
  88. for (cls = object_getClass(other); cls; cls = cls->superclass) {
  89. if (cls == protoClass) break;
  90. }
  91. if (!cls) return NO;
  92. // check equality
  93. return protocol_isEqual(self, other);
  94. #else
  95. return [other isKindOf:[Protocol class]] && [self conformsTo: other] && [other conformsTo: self];
  96. #endif
  97. }
  98. #if __OBJC2__
  99. - (NSUInteger)hash
  100. {
  101. return 23;
  102. }
  103. #else
  104. - (unsigned)hash
  105. {
  106. return 23;
  107. }
  108. #endif
  109. @end