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