objc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * objc.h
  25. * Copyright 1988-1996, NeXT Software, Inc.
  26. */
  27. #ifndef _OBJC_OBJC_H_
  28. #define _OBJC_OBJC_H_
  29. #include <sys/types.h> // for __DARWIN_NULL
  30. #include <Availability.h>
  31. #include <objc/objc-api.h>
  32. #include <stdbool.h>
  33. #if !OBJC_TYPES_DEFINED
  34. /// An opaque type that represents an Objective-C class.
  35. typedef struct objc_class *Class;
  36. /// Represents an instance of a class.
  37. struct objc_object {
  38. Class _Nonnull isa OBJC_ISA_AVAILABILITY;
  39. };
  40. /// A pointer to an instance of a class.
  41. typedef struct objc_object *id;
  42. #endif
  43. /// An opaque type that represents a method selector.
  44. typedef struct objc_selector *SEL;
  45. /// A pointer to the function of a method implementation.
  46. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  47. typedef void (*IMP)(void /* id, SEL, ... */ );
  48. #else
  49. typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...);
  50. #endif
  51. /// Type to represent a boolean value.
  52. #if defined(__OBJC_BOOL_IS_BOOL)
  53. // Honor __OBJC_BOOL_IS_BOOL when available.
  54. # if __OBJC_BOOL_IS_BOOL
  55. # define OBJC_BOOL_IS_BOOL 1
  56. # else
  57. # define OBJC_BOOL_IS_BOOL 0
  58. # endif
  59. #else
  60. // __OBJC_BOOL_IS_BOOL not set.
  61. # if TARGET_OS_OSX || TARGET_OS_IOSMAC || ((TARGET_OS_IOS || TARGET_OS_BRIDGE) && !__LP64__ && !__ARM_ARCH_7K)
  62. # define OBJC_BOOL_IS_BOOL 0
  63. # else
  64. # define OBJC_BOOL_IS_BOOL 1
  65. # endif
  66. #endif
  67. #if OBJC_BOOL_IS_BOOL
  68. typedef bool BOOL;
  69. #else
  70. # define OBJC_BOOL_IS_CHAR 1
  71. typedef signed char BOOL;
  72. // BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
  73. // even if -funsigned-char is used.
  74. #endif
  75. #define OBJC_BOOL_DEFINED
  76. #if __has_feature(objc_bool)
  77. #define YES __objc_yes
  78. #define NO __objc_no
  79. #else
  80. #define YES ((BOOL)1)
  81. #define NO ((BOOL)0)
  82. #endif
  83. #ifndef Nil
  84. # if __has_feature(cxx_nullptr)
  85. # define Nil nullptr
  86. # else
  87. # define Nil __DARWIN_NULL
  88. # endif
  89. #endif
  90. #ifndef nil
  91. # if __has_feature(cxx_nullptr)
  92. # define nil nullptr
  93. # else
  94. # define nil __DARWIN_NULL
  95. # endif
  96. #endif
  97. #ifndef __strong
  98. # if !__has_feature(objc_arc)
  99. # define __strong /* empty */
  100. # endif
  101. #endif
  102. #ifndef __unsafe_unretained
  103. # if !__has_feature(objc_arc)
  104. # define __unsafe_unretained /* empty */
  105. # endif
  106. #endif
  107. #ifndef __autoreleasing
  108. # if !__has_feature(objc_arc)
  109. # define __autoreleasing /* empty */
  110. # endif
  111. #endif
  112. /**
  113. * Returns the name of the method specified by a given selector.
  114. *
  115. * @param sel A pointer of type \c SEL. Pass the selector whose name you wish to determine.
  116. *
  117. * @return A C string indicating the name of the selector.
  118. */
  119. OBJC_EXPORT const char * _Nonnull sel_getName(SEL _Nonnull sel)
  120. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  121. /**
  122. * Registers a method with the Objective-C runtime system, maps the method
  123. * name to a selector, and returns the selector value.
  124. *
  125. * @param str A pointer to a C string. Pass the name of the method you wish to register.
  126. *
  127. * @return A pointer of type SEL specifying the selector for the named method.
  128. *
  129. * @note You must register a method name with the Objective-C runtime system to obtain the
  130. * method’s selector before you can add the method to a class definition. If the method name
  131. * has already been registered, this function simply returns the selector.
  132. */
  133. OBJC_EXPORT SEL _Nonnull sel_registerName(const char * _Nonnull str)
  134. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  135. /**
  136. * Returns the class name of a given object.
  137. *
  138. * @param obj An Objective-C object.
  139. *
  140. * @return The name of the class of which \e obj is an instance.
  141. */
  142. OBJC_EXPORT const char * _Nonnull object_getClassName(id _Nullable obj)
  143. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  144. /**
  145. * Returns a pointer to any extra bytes allocated with an instance given object.
  146. *
  147. * @param obj An Objective-C object.
  148. *
  149. * @return A pointer to any extra bytes allocated with \e obj. If \e obj was
  150. * not allocated with any extra bytes, then dereferencing the returned pointer is undefined.
  151. *
  152. * @note This function returns a pointer to any extra bytes allocated with the instance
  153. * (as specified by \c class_createInstance with extraBytes>0). This memory follows the
  154. * object's ordinary ivars, but may not be adjacent to the last ivar.
  155. * @note The returned pointer is guaranteed to be pointer-size aligned, even if the area following
  156. * the object's last ivar is less aligned than that. Alignment greater than pointer-size is never
  157. * guaranteed, even if the area following the object's last ivar is more aligned than that.
  158. * @note In a garbage-collected environment, the memory is scanned conservatively.
  159. */
  160. OBJC_EXPORT void * _Nullable object_getIndexedIvars(id _Nullable obj)
  161. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  162. OBJC_ARC_UNAVAILABLE;
  163. /**
  164. * Identifies a selector as being valid or invalid.
  165. *
  166. * @param sel The selector you want to identify.
  167. *
  168. * @return YES if selector is valid and has a function implementation, NO otherwise.
  169. *
  170. * @warning On some platforms, an invalid reference (to invalid memory addresses) can cause
  171. * a crash.
  172. */
  173. OBJC_EXPORT BOOL sel_isMapped(SEL _Nonnull sel)
  174. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  175. /**
  176. * Registers a method name with the Objective-C runtime system.
  177. *
  178. * @param str A pointer to a C string. Pass the name of the method you wish to register.
  179. *
  180. * @return A pointer of type SEL specifying the selector for the named method.
  181. *
  182. * @note The implementation of this method is identical to the implementation of \c sel_registerName.
  183. * @note Prior to OS X version 10.0, this method tried to find the selector mapped to the given name
  184. * and returned \c NULL if the selector was not found. This was changed for safety, because it was
  185. * observed that many of the callers of this function did not check the return value for \c NULL.
  186. */
  187. OBJC_EXPORT SEL _Nonnull sel_getUid(const char * _Nonnull str)
  188. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  189. typedef const void* objc_objectptr_t;
  190. // Obsolete ARC conversions.
  191. OBJC_EXPORT id _Nullable objc_retainedObject(objc_objectptr_t _Nullable obj)
  192. #if !OBJC_DECLARE_SYMBOLS
  193. OBJC_UNAVAILABLE("use CFBridgingRelease() or a (__bridge_transfer id) cast instead")
  194. #endif
  195. ;
  196. OBJC_EXPORT id _Nullable objc_unretainedObject(objc_objectptr_t _Nullable obj)
  197. #if !OBJC_DECLARE_SYMBOLS
  198. OBJC_UNAVAILABLE("use a (__bridge id) cast instead")
  199. #endif
  200. ;
  201. OBJC_EXPORT objc_objectptr_t _Nullable objc_unretainedPointer(id _Nullable obj)
  202. #if !OBJC_DECLARE_SYMBOLS
  203. OBJC_UNAVAILABLE("use a __bridge cast instead")
  204. #endif
  205. ;
  206. #if !__OBJC2__
  207. // The following declarations are provided here for source compatibility.
  208. #if defined(__LP64__)
  209. typedef long arith_t;
  210. typedef unsigned long uarith_t;
  211. # define ARITH_SHIFT 32
  212. #else
  213. typedef int arith_t;
  214. typedef unsigned uarith_t;
  215. # define ARITH_SHIFT 16
  216. #endif
  217. typedef char *STR;
  218. #define ISSELECTOR(sel) sel_isMapped(sel)
  219. #define SELNAME(sel) sel_getName(sel)
  220. #define SELUID(str) sel_getUid(str)
  221. #define NAMEOF(obj) object_getClassName(obj)
  222. #define IV(obj) object_getIndexedIvars(obj)
  223. #endif
  224. #endif /* _OBJC_OBJC_H_ */