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