]> git.saurik.com Git - apple/libdispatch.git/blob - os/object.h
libdispatch-703.1.4.tar.gz
[apple/libdispatch.git] / os / object.h
1 /*
2 * Copyright (c) 2011-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 #ifndef __OS_OBJECT__
22 #define __OS_OBJECT__
23
24 #ifdef __APPLE__
25 #include <Availability.h>
26 #include <TargetConditionals.h>
27 #endif
28 #ifndef __linux__
29 #include <os/base.h>
30 #else
31 #include <os/linux_base.h>
32 #endif
33
34 /*!
35 * @header
36 *
37 * @preprocinfo
38 * By default, libSystem objects such as GCD and XPC objects are declared as
39 * Objective-C types when building with an Objective-C compiler. This allows
40 * them to participate in ARC, in RR management by the Blocks runtime and in
41 * leaks checking by the static analyzer, and enables them to be added to Cocoa
42 * collections.
43 *
44 * NOTE: this requires explicit cancellation of dispatch sources and xpc
45 * connections whose handler blocks capture the source/connection object,
46 * resp. ensuring that such captures do not form retain cycles (e.g. by
47 * declaring the source as __weak).
48 *
49 * To opt-out of this default behavior, add -DOS_OBJECT_USE_OBJC=0 to your
50 * compiler flags.
51 *
52 * This mode requires a platform with the modern Objective-C runtime, the
53 * Objective-C GC compiler option to be disabled, and at least a Mac OS X 10.8
54 * or iOS 6.0 deployment target.
55 */
56
57 #ifndef OS_OBJECT_HAVE_OBJC_SUPPORT
58 #if !defined(__OBJC__) || defined(__OBJC_GC__)
59 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
60 #elif !defined(TARGET_OS_MAC) || !TARGET_OS_MAC
61 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
62 #elif TARGET_OS_IOS && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
63 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
64 #elif TARGET_OS_MAC && !TARGET_OS_IPHONE
65 # if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
66 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
67 # elif defined(__i386__) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12
68 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
69 # else
70 # define OS_OBJECT_HAVE_OBJC_SUPPORT 1
71 # endif
72 #else
73 # define OS_OBJECT_HAVE_OBJC_SUPPORT 1
74 #endif
75 #endif // OS_OBJECT_HAVE_OBJC_SUPPORT
76
77 #if OS_OBJECT_HAVE_OBJC_SUPPORT
78 #ifndef OS_OBJECT_USE_OBJC
79 #define OS_OBJECT_USE_OBJC 1
80 #endif
81 #elif defined(OS_OBJECT_USE_OBJC) && OS_OBJECT_USE_OBJC
82 /* Unsupported platform for OS_OBJECT_USE_OBJC=1 */
83 #undef OS_OBJECT_USE_OBJC
84 #define OS_OBJECT_USE_OBJC 0
85 #else
86 #define OS_OBJECT_USE_OBJC 0
87 #endif
88
89 #ifndef OS_OBJECT_SWIFT3
90 #if defined(SWIFT_SDK_OVERLAY_DISPATCH_EPOCH) && \
91 SWIFT_SDK_OVERLAY_DISPATCH_EPOCH >= 2
92 #define OS_OBJECT_SWIFT3 1
93 #else
94 #define OS_OBJECT_SWIFT3 0
95 #endif // SWIFT_SDK_OVERLAY_DISPATCH_EPOCH >= 2
96 #endif // OS_OBJECT_SWIFT3
97
98 #if OS_OBJECT_USE_OBJC
99 #import <objc/NSObject.h>
100 #if __has_attribute(objc_independent_class)
101 #define OS_OBJC_INDEPENDENT_CLASS __attribute__((objc_independent_class))
102 #endif // __has_attribute(objc_independent_class)
103 #ifndef OS_OBJC_INDEPENDENT_CLASS
104 #define OS_OBJC_INDEPENDENT_CLASS
105 #endif
106 #define OS_OBJECT_CLASS(name) OS_##name
107 #define OS_OBJECT_DECL_PROTOCOL(name, ...) \
108 @protocol OS_OBJECT_CLASS(name) __VA_ARGS__ \
109 @end
110 #define OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL_IMPL(name, proto) \
111 @interface name () <proto> \
112 @end
113 #define OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(name, proto) \
114 OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL_IMPL( \
115 OS_OBJECT_CLASS(name), OS_OBJECT_CLASS(proto))
116 #define OS_OBJECT_DECL_IMPL(name, ...) \
117 OS_OBJECT_DECL_PROTOCOL(name, __VA_ARGS__) \
118 typedef NSObject<OS_OBJECT_CLASS(name)> \
119 * OS_OBJC_INDEPENDENT_CLASS name##_t
120 #define OS_OBJECT_DECL_BASE(name, ...) \
121 @interface OS_OBJECT_CLASS(name) : __VA_ARGS__ \
122 - (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift"); \
123 @end
124 #define OS_OBJECT_DECL_IMPL_CLASS(name, ...) \
125 OS_OBJECT_DECL_BASE(name, ## __VA_ARGS__) \
126 typedef OS_OBJECT_CLASS(name) \
127 * OS_OBJC_INDEPENDENT_CLASS name##_t
128 #define OS_OBJECT_DECL(name, ...) \
129 OS_OBJECT_DECL_IMPL(name, <NSObject>)
130 #define OS_OBJECT_DECL_SUBCLASS(name, super) \
131 OS_OBJECT_DECL_IMPL(name, <OS_OBJECT_CLASS(super)>)
132 #if __has_attribute(ns_returns_retained)
133 #define OS_OBJECT_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
134 #else
135 #define OS_OBJECT_RETURNS_RETAINED
136 #endif
137 #if __has_attribute(ns_consumed)
138 #define OS_OBJECT_CONSUMED __attribute__((__ns_consumed__))
139 #else
140 #define OS_OBJECT_CONSUMED
141 #endif
142 #if __has_feature(objc_arc)
143 #define OS_OBJECT_BRIDGE __bridge
144 #define OS_WARN_RESULT_NEEDS_RELEASE
145 #else
146 #define OS_OBJECT_BRIDGE
147 #define OS_WARN_RESULT_NEEDS_RELEASE OS_WARN_RESULT
148 #endif
149 #if __has_attribute(objc_runtime_visible) && \
150 ((defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
151 __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12) || \
152 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
153 !defined(__TV_OS_VERSION_MIN_REQUIRED) && \
154 !defined(__WATCH_OS_VERSION_MIN_REQUIRED) && \
155 __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0) || \
156 (defined(__TV_OS_VERSION_MIN_REQUIRED) && \
157 __TV_OS_VERSION_MIN_REQUIRED < __TVOS_10_0) || \
158 (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && \
159 __WATCH_OS_VERSION_MIN_REQUIRED < __WATCHOS_3_0))
160 /*
161 * To provide backward deployment of ObjC objects in Swift on pre-10.12
162 * SDKs, OS_object classes can be marked as OS_OBJECT_OBJC_RUNTIME_VISIBLE.
163 * When compiling with a deployment target earlier than OS X 10.12 (iOS 10.0,
164 * tvOS 10.0, watchOS 3.0) the Swift compiler will only refer to this type at
165 * runtime (using the ObjC runtime).
166 */
167 #define OS_OBJECT_OBJC_RUNTIME_VISIBLE __attribute__((objc_runtime_visible))
168 #else
169 #define OS_OBJECT_OBJC_RUNTIME_VISIBLE
170 #endif
171 #ifndef OS_OBJECT_USE_OBJC_RETAIN_RELEASE
172 #if defined(__clang_analyzer__)
173 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
174 #elif __has_feature(objc_arc) && !OS_OBJECT_SWIFT3
175 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
176 #else
177 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
178 #endif
179 #endif
180 #if OS_OBJECT_SWIFT3
181 #define OS_OBJECT_DECL_SWIFT(name) \
182 OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE \
183 OS_OBJECT_DECL_IMPL_CLASS(name, NSObject)
184 #define OS_OBJECT_DECL_SUBCLASS_SWIFT(name, super) \
185 OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE \
186 OS_OBJECT_DECL_IMPL_CLASS(name, OS_OBJECT_CLASS(super))
187 OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE
188 OS_OBJECT_DECL_BASE(object, NSObject);
189 #endif // OS_OBJECT_SWIFT3
190 #else
191 /*! @parseOnly */
192 #define OS_OBJECT_RETURNS_RETAINED
193 /*! @parseOnly */
194 #define OS_OBJECT_CONSUMED
195 /*! @parseOnly */
196 #define OS_OBJECT_BRIDGE
197 /*! @parseOnly */
198 #define OS_WARN_RESULT_NEEDS_RELEASE OS_WARN_RESULT
199 /*! @parseOnly */
200 #define OS_OBJECT_OBJC_RUNTIME_VISIBLE
201 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
202 #endif
203
204 #if OS_OBJECT_SWIFT3
205 #define OS_OBJECT_DECL_CLASS(name) \
206 OS_OBJECT_DECL_SUBCLASS_SWIFT(name, object)
207 #elif OS_OBJECT_USE_OBJC
208 #define OS_OBJECT_DECL_CLASS(name) \
209 OS_OBJECT_DECL(name)
210 #else
211 #define OS_OBJECT_DECL_CLASS(name) \
212 typedef struct name##_s *name##_t
213 #endif
214
215 #define OS_OBJECT_GLOBAL_OBJECT(type, object) ((OS_OBJECT_BRIDGE type)&(object))
216
217 __BEGIN_DECLS
218
219 /*!
220 * @function os_retain
221 *
222 * @abstract
223 * Increment the reference count of an os_object.
224 *
225 * @discussion
226 * On a platform with the modern Objective-C runtime this is exactly equivalent
227 * to sending the object the -[retain] message.
228 *
229 * @param object
230 * The object to retain.
231 *
232 * @result
233 * The retained object.
234 */
235 __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0)
236 OS_EXPORT OS_SWIFT_UNAVAILABLE("Can't be used with ARC")
237 void*
238 os_retain(void *object);
239 #if OS_OBJECT_USE_OBJC
240 #undef os_retain
241 #define os_retain(object) [object retain]
242 #endif
243
244 /*!
245 * @function os_release
246 *
247 * @abstract
248 * Decrement the reference count of a os_object.
249 *
250 * @discussion
251 * On a platform with the modern Objective-C runtime this is exactly equivalent
252 * to sending the object the -[release] message.
253 *
254 * @param object
255 * The object to release.
256 */
257 __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0)
258 OS_EXPORT
259 void OS_SWIFT_UNAVAILABLE("Can't be used with ARC")
260 os_release(void *object);
261 #if OS_OBJECT_USE_OBJC
262 #undef os_release
263 #define os_release(object) [object release]
264 #endif
265
266 __END_DECLS
267
268 #endif