object.h 8.4 KB

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