You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
14 KiB

6 years ago
  1. //
  2. // GTMDefines.h
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // 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, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. // ============================================================================
  19. #include <AvailabilityMacros.h>
  20. #include <TargetConditionals.h>
  21. #ifdef __OBJC__
  22. #include <Foundation/NSObjCRuntime.h>
  23. #endif // __OBJC__
  24. #if TARGET_OS_IPHONE
  25. #include <Availability.h>
  26. #endif // TARGET_OS_IPHONE
  27. // ----------------------------------------------------------------------------
  28. // CPP symbols that can be overridden in a prefix to control how the toolbox
  29. // is compiled.
  30. // ----------------------------------------------------------------------------
  31. // By setting the GTM_CONTAINERS_VALIDATION_FAILED_LOG and
  32. // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT macros you can control what happens
  33. // when a validation fails. If you implement your own validators, you may want
  34. // to control their internals using the same macros for consistency.
  35. #ifndef GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
  36. #define GTM_CONTAINERS_VALIDATION_FAILED_ASSERT 0
  37. #endif
  38. // Ensure __has_feature and __has_extension are safe to use.
  39. // See http://clang-analyzer.llvm.org/annotations.html
  40. #ifndef __has_feature // Optional.
  41. #define __has_feature(x) 0 // Compatibility with non-clang compilers.
  42. #endif
  43. #ifndef __has_extension
  44. #define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
  45. #endif
  46. // Give ourselves a consistent way to do inlines. Apple's macros even use
  47. // a few different actual definitions, so we're based off of the foundation
  48. // one.
  49. #if !defined(GTM_INLINE)
  50. #if (defined (__GNUC__) && (__GNUC__ == 4)) || defined (__clang__)
  51. #define GTM_INLINE static __inline__ __attribute__((always_inline))
  52. #else
  53. #define GTM_INLINE static __inline__
  54. #endif
  55. #endif
  56. // Give ourselves a consistent way of doing externs that links up nicely
  57. // when mixing objc and objc++
  58. #if !defined (GTM_EXTERN)
  59. #if defined __cplusplus
  60. #define GTM_EXTERN extern "C"
  61. #define GTM_EXTERN_C_BEGIN extern "C" {
  62. #define GTM_EXTERN_C_END }
  63. #else
  64. #define GTM_EXTERN extern
  65. #define GTM_EXTERN_C_BEGIN
  66. #define GTM_EXTERN_C_END
  67. #endif
  68. #endif
  69. // Give ourselves a consistent way of exporting things if we have visibility
  70. // set to hidden.
  71. #if !defined (GTM_EXPORT)
  72. #define GTM_EXPORT __attribute__((visibility("default")))
  73. #endif
  74. // Give ourselves a consistent way of declaring something as unused. This
  75. // doesn't use __unused because that is only supported in gcc 4.2 and greater.
  76. #if !defined (GTM_UNUSED)
  77. #define GTM_UNUSED(x) ((void)(x))
  78. #endif
  79. // _GTMDevLog & _GTMDevAssert
  80. //
  81. // _GTMDevLog & _GTMDevAssert are meant to be a very lightweight shell for
  82. // developer level errors. This implementation simply macros to NSLog/NSAssert.
  83. // It is not intended to be a general logging/reporting system.
  84. //
  85. // Please see http://code.google.com/p/google-toolbox-for-mac/wiki/DevLogNAssert
  86. // for a little more background on the usage of these macros.
  87. //
  88. // _GTMDevLog log some error/problem in debug builds
  89. // _GTMDevAssert assert if condition isn't met w/in a method/function
  90. // in all builds.
  91. //
  92. // To replace this system, just provide different macro definitions in your
  93. // prefix header. Remember, any implementation you provide *must* be thread
  94. // safe since this could be called by anything in what ever situtation it has
  95. // been placed in.
  96. //
  97. // Ignore the "Macro name is a reserved identifier" warning in this section
  98. #pragma clang diagnostic push
  99. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  100. // We only define the simple macros if nothing else has defined this.
  101. #ifndef _GTMDevLog
  102. #ifdef DEBUG
  103. #define _GTMDevLog(...) NSLog(__VA_ARGS__)
  104. #else
  105. #define _GTMDevLog(...) do { } while (0)
  106. #endif
  107. #endif // _GTMDevLog
  108. #ifndef _GTMDevAssert
  109. // we directly invoke the NSAssert handler so we can pass on the varargs
  110. // (NSAssert doesn't have a macro we can use that takes varargs)
  111. #if !defined(NS_BLOCK_ASSERTIONS)
  112. #define _GTMDevAssert(condition, ...) \
  113. do { \
  114. if (!(condition)) { \
  115. [[NSAssertionHandler currentHandler] \
  116. handleFailureInFunction:(NSString *) \
  117. [NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
  118. file:(NSString *)[NSString stringWithUTF8String:__FILE__] \
  119. lineNumber:__LINE__ \
  120. description:__VA_ARGS__]; \
  121. } \
  122. } while(0)
  123. #else // !defined(NS_BLOCK_ASSERTIONS)
  124. #define _GTMDevAssert(condition, ...) do { } while (0)
  125. #endif // !defined(NS_BLOCK_ASSERTIONS)
  126. #endif // _GTMDevAssert
  127. // _GTMCompileAssert
  128. //
  129. // Note: Software for current compilers should just use _Static_assert directly
  130. // instead of this macro.
  131. //
  132. // _GTMCompileAssert is an assert that is meant to fire at compile time if you
  133. // want to check things at compile instead of runtime. For example if you
  134. // want to check that a wchar is 4 bytes instead of 2 you would use
  135. // _GTMCompileAssert(sizeof(wchar_t) == 4, wchar_t_is_4_bytes_on_OS_X)
  136. // Note that the second "arg" is not in quotes, and must be a valid processor
  137. // symbol in it's own right (no spaces, punctuation etc).
  138. // Wrapping this in an #ifndef allows external groups to define their own
  139. // compile time assert scheme.
  140. #ifndef _GTMCompileAssert
  141. #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
  142. #define _GTMCompileAssert(test, msg) _Static_assert((test), #msg)
  143. #else
  144. // Pre-Xcode 7 support.
  145. //
  146. // We got this technique from here:
  147. // http://unixjunkie.blogspot.com/2007/10/better-compile-time-asserts_29.html
  148. #define _GTMCompileAssertSymbolInner(line, msg) _GTMCOMPILEASSERT ## line ## __ ## msg
  149. #define _GTMCompileAssertSymbol(line, msg) _GTMCompileAssertSymbolInner(line, msg)
  150. #define _GTMCompileAssert(test, msg) \
  151. typedef char _GTMCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  152. #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
  153. #endif // _GTMCompileAssert
  154. #pragma clang diagnostic pop
  155. // ----------------------------------------------------------------------------
  156. // CPP symbols defined based on the project settings so the GTM code has
  157. // simple things to test against w/o scattering the knowledge of project
  158. // setting through all the code.
  159. // ----------------------------------------------------------------------------
  160. // Provide a single constant CPP symbol that all of GTM uses for ifdefing
  161. // iPhone code.
  162. #if TARGET_OS_IPHONE // iPhone SDK
  163. // For iPhone specific stuff
  164. #define GTM_IPHONE_SDK 1
  165. #if TARGET_IPHONE_SIMULATOR
  166. #define GTM_IPHONE_DEVICE 0
  167. #define GTM_IPHONE_SIMULATOR 1
  168. #else
  169. #define GTM_IPHONE_DEVICE 1
  170. #define GTM_IPHONE_SIMULATOR 0
  171. #endif // TARGET_IPHONE_SIMULATOR
  172. // By default, GTM has provided it's own unittesting support, define this
  173. // to use the support provided by Xcode, especially for the Xcode4 support
  174. // for unittesting.
  175. #ifndef GTM_USING_XCTEST
  176. #define GTM_USING_XCTEST 0
  177. #endif
  178. #define GTM_MACOS_SDK 0
  179. #else
  180. // For MacOS specific stuff
  181. #define GTM_MACOS_SDK 1
  182. #define GTM_IPHONE_SDK 0
  183. #define GTM_IPHONE_SIMULATOR 0
  184. #define GTM_IPHONE_DEVICE 0
  185. #ifndef GTM_USING_XCTEST
  186. #define GTM_USING_XCTEST 0
  187. #endif
  188. #endif
  189. // Some of our own availability macros
  190. #if GTM_MACOS_SDK
  191. #define GTM_AVAILABLE_ONLY_ON_IPHONE UNAVAILABLE_ATTRIBUTE
  192. #define GTM_AVAILABLE_ONLY_ON_MACOS
  193. #else
  194. #define GTM_AVAILABLE_ONLY_ON_IPHONE
  195. #define GTM_AVAILABLE_ONLY_ON_MACOS UNAVAILABLE_ATTRIBUTE
  196. #endif
  197. // GC was dropped by Apple, define the old constant incase anyone still keys
  198. // off of it.
  199. #ifndef GTM_SUPPORT_GC
  200. #define GTM_SUPPORT_GC 0
  201. #endif
  202. // Some support for advanced clang static analysis functionality
  203. #ifndef NS_RETURNS_RETAINED
  204. #if __has_feature(attribute_ns_returns_retained)
  205. #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
  206. #else
  207. #define NS_RETURNS_RETAINED
  208. #endif
  209. #endif
  210. #ifndef NS_RETURNS_NOT_RETAINED
  211. #if __has_feature(attribute_ns_returns_not_retained)
  212. #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
  213. #else
  214. #define NS_RETURNS_NOT_RETAINED
  215. #endif
  216. #endif
  217. #ifndef CF_RETURNS_RETAINED
  218. #if __has_feature(attribute_cf_returns_retained)
  219. #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
  220. #else
  221. #define CF_RETURNS_RETAINED
  222. #endif
  223. #endif
  224. #ifndef CF_RETURNS_NOT_RETAINED
  225. #if __has_feature(attribute_cf_returns_not_retained)
  226. #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
  227. #else
  228. #define CF_RETURNS_NOT_RETAINED
  229. #endif
  230. #endif
  231. #ifndef NS_CONSUMED
  232. #if __has_feature(attribute_ns_consumed)
  233. #define NS_CONSUMED __attribute__((ns_consumed))
  234. #else
  235. #define NS_CONSUMED
  236. #endif
  237. #endif
  238. #ifndef CF_CONSUMED
  239. #if __has_feature(attribute_cf_consumed)
  240. #define CF_CONSUMED __attribute__((cf_consumed))
  241. #else
  242. #define CF_CONSUMED
  243. #endif
  244. #endif
  245. #ifndef NS_CONSUMES_SELF
  246. #if __has_feature(attribute_ns_consumes_self)
  247. #define NS_CONSUMES_SELF __attribute__((ns_consumes_self))
  248. #else
  249. #define NS_CONSUMES_SELF
  250. #endif
  251. #endif
  252. #ifndef GTM_NONNULL
  253. #if defined(__has_attribute)
  254. #if __has_attribute(nonnull)
  255. #define GTM_NONNULL(x) __attribute__((nonnull x))
  256. #else
  257. #define GTM_NONNULL(x)
  258. #endif
  259. #else
  260. #define GTM_NONNULL(x)
  261. #endif
  262. #endif
  263. // Invalidates the initializer from which it's called.
  264. #ifndef GTMInvalidateInitializer
  265. #if __has_feature(objc_arc)
  266. #define GTMInvalidateInitializer() \
  267. do { \
  268. [self class]; /* Avoid warning of dead store to |self|. */ \
  269. _GTMDevAssert(NO, @"Invalid initializer."); \
  270. return nil; \
  271. } while (0)
  272. #else
  273. #define GTMInvalidateInitializer() \
  274. do { \
  275. [self release]; \
  276. _GTMDevAssert(NO, @"Invalid initializer."); \
  277. return nil; \
  278. } while (0)
  279. #endif
  280. #endif
  281. #ifndef GTMCFAutorelease
  282. // GTMCFAutorelease returns an id. In contrast, Apple's CFAutorelease returns
  283. // a CFTypeRef.
  284. #if __has_feature(objc_arc)
  285. #define GTMCFAutorelease(x) CFBridgingRelease(x)
  286. #else
  287. #define GTMCFAutorelease(x) ([(id)x autorelease])
  288. #endif
  289. #endif
  290. #ifdef __OBJC__
  291. // Macro to allow you to create NSStrings out of other macros.
  292. // #define FOO foo
  293. // NSString *fooString = GTM_NSSTRINGIFY(FOO);
  294. #if !defined (GTM_NSSTRINGIFY)
  295. #define GTM_NSSTRINGIFY_INNER(x) @#x
  296. #define GTM_NSSTRINGIFY(x) GTM_NSSTRINGIFY_INNER(x)
  297. #endif
  298. // Macro to allow fast enumeration when building for 10.5 or later, and
  299. // reliance on NSEnumerator for 10.4. Remember, NSDictionary w/ FastEnumeration
  300. // does keys, so pick the right thing, nothing is done on the FastEnumeration
  301. // side to be sure you're getting what you wanted.
  302. #ifndef GTM_FOREACH_OBJECT
  303. #if TARGET_OS_IPHONE || !(MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  304. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  305. for (element in enumeration)
  306. #define GTM_FOREACH_OBJECT(element, collection) \
  307. for (element in collection)
  308. #define GTM_FOREACH_KEY(element, collection) \
  309. for (element in collection)
  310. #else
  311. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  312. for (NSEnumerator *_ ## element ## _enum = enumeration; \
  313. (element = [_ ## element ## _enum nextObject]) != nil; )
  314. #define GTM_FOREACH_OBJECT(element, collection) \
  315. GTM_FOREACH_ENUMEREE(element, [collection objectEnumerator])
  316. #define GTM_FOREACH_KEY(element, collection) \
  317. GTM_FOREACH_ENUMEREE(element, [collection keyEnumerator])
  318. #endif
  319. #endif
  320. // ============================================================================
  321. // GTM_SEL_STRING is for specifying selector (usually property) names to KVC
  322. // or KVO methods.
  323. // In debug it will generate warnings for undeclared selectors if
  324. // -Wunknown-selector is turned on.
  325. // In release it will have no runtime overhead.
  326. #ifndef GTM_SEL_STRING
  327. #ifdef DEBUG
  328. #define GTM_SEL_STRING(selName) NSStringFromSelector(@selector(selName))
  329. #else
  330. #define GTM_SEL_STRING(selName) @#selName
  331. #endif // DEBUG
  332. #endif // GTM_SEL_STRING
  333. #ifndef GTM_WEAK
  334. #if __has_feature(objc_arc_weak)
  335. // With ARC enabled, __weak means a reference that isn't implicitly
  336. // retained. __weak objects are accessed through runtime functions, so
  337. // they are zeroed out, but this requires OS X 10.7+.
  338. // At clang r251041+, ARC-style zeroing weak references even work in
  339. // non-ARC mode.
  340. #define GTM_WEAK __weak
  341. #elif __has_feature(objc_arc)
  342. // ARC, but targeting 10.6 or older, where zeroing weak references don't
  343. // exist.
  344. #define GTM_WEAK __unsafe_unretained
  345. #else
  346. // With manual reference counting, __weak used to be silently ignored.
  347. // clang r251041 gives it the ARC semantics instead. This means they
  348. // now require a deployment target of 10.7, while some clients of GTM
  349. // still target 10.6. In these cases, expand to __unsafe_unretained instead
  350. #define GTM_WEAK
  351. #endif
  352. #endif
  353. #endif // __OBJC__