1/*
2 * Copyright (c) 2011-2012 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#endif
27
28/*!
29 * @header
30 *
31 * @preprocinfo
32 * By default, libSystem objects such as GCD and XPC objects are declared as
33 * Objective-C types when building with an Objective-C compiler. This allows
34 * them to participate in ARC, in RR management by the Blocks runtime and in
35 * leaks checking by the static analyzer, and enables them to be added to Cocoa
36 * collections.
37 *
38 * NOTE: this requires explicit cancellation of dispatch sources and xpc
39 *       connections whose handler blocks capture the source/connection object,
40 *       resp. ensuring that such captures do not form retain cycles (e.g. by
41 *       declaring the source as __weak).
42 *
43 * To opt-out of this default behavior, add -DOS_OBJECT_USE_OBJC=0 to your
44 * compiler flags.
45 *
46 * This mode requires a platform with the modern Objective-C runtime, the
47 * Objective-C GC compiler option to be disabled, and at least a Mac OS X 10.8
48 * or iOS 6.0 deployment target.
49 */
50
51#ifndef OS_OBJECT_HAVE_OBJC_SUPPORT
52#if defined(__OBJC__) && defined(__OBJC2__) && !defined(__OBJC_GC__) && ( \
53		__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_8 || \
54		__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0)
55#define OS_OBJECT_HAVE_OBJC_SUPPORT 1
56#else
57#define OS_OBJECT_HAVE_OBJC_SUPPORT 0
58#endif
59#endif
60
61#if OS_OBJECT_HAVE_OBJC_SUPPORT
62#ifndef OS_OBJECT_USE_OBJC
63#define OS_OBJECT_USE_OBJC 1
64#endif
65#elif defined(OS_OBJECT_USE_OBJC) && OS_OBJECT_USE_OBJC
66/* Unsupported platform for OS_OBJECT_USE_OBJC=1 */
67#undef OS_OBJECT_USE_OBJC
68#define OS_OBJECT_USE_OBJC 0
69#else
70#define OS_OBJECT_USE_OBJC 0
71#endif
72
73#if OS_OBJECT_USE_OBJC
74#import <objc/NSObject.h>
75#define OS_OBJECT_CLASS(name) OS_##name
76#define OS_OBJECT_DECL(name, ...) \
77		@protocol OS_OBJECT_CLASS(name) __VA_ARGS__ \
78		@end \
79		typedef NSObject<OS_OBJECT_CLASS(name)> *name##_t
80#define OS_OBJECT_DECL_SUBCLASS(name, super) \
81		OS_OBJECT_DECL(name, <OS_OBJECT_CLASS(super)>)
82#if defined(__has_attribute)
83#if __has_attribute(ns_returns_retained)
84#define OS_OBJECT_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
85#else
86#define OS_OBJECT_RETURNS_RETAINED
87#endif
88#else
89#define OS_OBJECT_RETURNS_RETAINED
90#endif
91#if defined(__has_feature)
92#if __has_feature(objc_arc)
93#define OS_OBJECT_BRIDGE __bridge
94#else
95#define OS_OBJECT_BRIDGE
96#endif
97#else
98#define OS_OBJECT_BRIDGE
99#endif
100#ifndef OS_OBJECT_USE_OBJC_RETAIN_RELEASE
101#if defined(__clang_analyzer__)
102#define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
103#elif defined(__has_feature)
104#if __has_feature(objc_arc)
105#define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
106#else
107#define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
108#endif
109#else
110#define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
111#endif
112#endif
113#else
114/*! @parseOnly */
115#define OS_OBJECT_RETURNS_RETAINED
116/*! @parseOnly */
117#define OS_OBJECT_BRIDGE
118#define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
119#endif
120
121#endif
122