]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc.h
b0554cc4ca0abec965cb8328ff812349944d6422
[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 #if ! (defined(__OBJC_GC__) || __has_feature(objc_arc))
97 #define __strong /* empty */
98 #endif
99
100 #if !__has_feature(objc_arc)
101 #define __unsafe_unretained /* empty */
102 #define __autoreleasing /* empty */
103 #endif
104
105
106 /**
107 * Returns the name of the method specified by a given selector.
108 *
109 * @param sel A pointer of type \c SEL. Pass the selector whose name you wish to determine.
110 *
111 * @return A C string indicating the name of the selector.
112 */
113 OBJC_EXPORT const char *sel_getName(SEL sel)
114 __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
115
116 /**
117 * Registers a method with the Objective-C runtime system, maps the method
118 * name to a selector, and returns the selector value.
119 *
120 * @param str A pointer to a C string. Pass the name of the method you wish to register.
121 *
122 * @return A pointer of type SEL specifying the selector for the named method.
123 *
124 * @note You must register a method name with the Objective-C runtime system to obtain the
125 * method’s selector before you can add the method to a class definition. If the method name
126 * has already been registered, this function simply returns the selector.
127 */
128 OBJC_EXPORT SEL sel_registerName(const char *str)
129 __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
130
131 /**
132 * Returns the class name of a given object.
133 *
134 * @param obj An Objective-C object.
135 *
136 * @return The name of the class of which \e obj is an instance.
137 */
138 OBJC_EXPORT const char *object_getClassName(id obj)
139 __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
140
141 /**
142 * Returns a pointer to any extra bytes allocated with an instance given object.
143 *
144 * @param obj An Objective-C object.
145 *
146 * @return A pointer to any extra bytes allocated with \e obj. If \e obj was
147 * not allocated with any extra bytes, then dereferencing the returned pointer is undefined.
148 *
149 * @note This function returns a pointer to any extra bytes allocated with the instance
150 * (as specified by \c class_createInstance with extraBytes>0). This memory follows the
151 * object's ordinary ivars, but may not be adjacent to the last ivar.
152 * @note The returned pointer is guaranteed to be pointer-size aligned, even if the area following
153 * the object's last ivar is less aligned than that. Alignment greater than pointer-size is never
154 * guaranteed, even if the area following the object's last ivar is more aligned than that.
155 * @note In a garbage-collected environment, the memory is scanned conservatively.
156 */
157 OBJC_EXPORT void *object_getIndexedIvars(id obj)
158 __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
159
160 /**
161 * Identifies a selector as being valid or invalid.
162 *
163 * @param sel The selector you want to identify.
164 *
165 * @return YES if selector is valid and has a function implementation, NO otherwise.
166 *
167 * @warning On some platforms, an invalid reference (to invalid memory addresses) can cause
168 * a crash.
169 */
170 OBJC_EXPORT BOOL sel_isMapped(SEL sel)
171 __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
172
173 /**
174 * Registers a method name with the Objective-C runtime system.
175 *
176 * @param str A pointer to a C string. Pass the name of the method you wish to register.
177 *
178 * @return A pointer of type SEL specifying the selector for the named method.
179 *
180 * @note The implementation of this method is identical to the implementation of \c sel_registerName.
181 * @note Prior to OS X version 10.0, this method tried to find the selector mapped to the given name
182 * and returned \c NULL if the selector was not found. This was changed for safety, because it was
183 * observed that many of the callers of this function did not check the return value for \c NULL.
184 */
185 OBJC_EXPORT SEL sel_getUid(const char *str)
186 __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
187
188
189 // Obsolete ARC conversions. Deprecation forthcoming.
190 // Use CFBridgingRetain, CFBridgingRelease, and __bridge casts instead.
191
192 typedef const void* objc_objectptr_t;
193
194 #if __has_feature(objc_arc)
195 # define objc_retainedObject(o) ((__bridge_transfer id)(objc_objectptr_t)(o))
196 # define objc_unretainedObject(o) ((__bridge id)(objc_objectptr_t)(o))
197 # define objc_unretainedPointer(o) ((__bridge objc_objectptr_t)(id)(o))
198 #else
199 # define objc_retainedObject(o) ((id)(objc_objectptr_t)(o))
200 # define objc_unretainedObject(o) ((id)(objc_objectptr_t)(o))
201 # define objc_unretainedPointer(o) ((objc_objectptr_t)(id)(o))
202 #endif
203
204
205 #if !__OBJC2__
206
207 // The following declarations are provided here for source compatibility.
208
209 #if defined(__LP64__)
210 typedef long arith_t;
211 typedef unsigned long uarith_t;
212 # define ARITH_SHIFT 32
213 #else
214 typedef int arith_t;
215 typedef unsigned uarith_t;
216 # define ARITH_SHIFT 16
217 #endif
218
219 typedef char *STR;
220
221 #define ISSELECTOR(sel) sel_isMapped(sel)
222 #define SELNAME(sel) sel_getName(sel)
223 #define SELUID(str) sel_getUid(str)
224 #define NAMEOF(obj) object_getClassName(obj)
225 #define IV(obj) object_getIndexedIvars(obj)
226
227 #endif
228
229 #endif /* _OBJC_OBJC_H_ */