GTMDefines.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. // Not all MAC_OS_X_VERSION_10_X macros defined in past SDKs
  28. #ifndef MAC_OS_X_VERSION_10_5
  29. #define MAC_OS_X_VERSION_10_5 1050
  30. #endif
  31. #ifndef MAC_OS_X_VERSION_10_6
  32. #define MAC_OS_X_VERSION_10_6 1060
  33. #endif
  34. #ifndef MAC_OS_X_VERSION_10_7
  35. #define MAC_OS_X_VERSION_10_7 1070
  36. #endif
  37. #ifndef MAC_OS_X_VERSION_10_8
  38. #define MAC_OS_X_VERSION_10_8 1080
  39. #endif
  40. #ifndef MAC_OS_X_VERSION_10_9
  41. #define MAC_OS_X_VERSION_10_9 1090
  42. #endif
  43. // Not all __IPHONE_X macros defined in past SDKs
  44. #ifndef __IPHONE_3_0
  45. #define __IPHONE_3_0 30000
  46. #endif
  47. #ifndef __IPHONE_3_1
  48. #define __IPHONE_3_1 30100
  49. #endif
  50. #ifndef __IPHONE_3_2
  51. #define __IPHONE_3_2 30200
  52. #endif
  53. #ifndef __IPHONE_4_0
  54. #define __IPHONE_4_0 40000
  55. #endif
  56. #ifndef __IPHONE_4_3
  57. #define __IPHONE_4_3 40300
  58. #endif
  59. #ifndef __IPHONE_5_0
  60. #define __IPHONE_5_0 50000
  61. #endif
  62. #ifndef __IPHONE_5_1
  63. #define __IPHONE_5_1 50100
  64. #endif
  65. #ifndef __IPHONE_6_0
  66. #define __IPHONE_6_0 60000
  67. #endif
  68. #ifndef __IPHONE_6_1
  69. #define __IPHONE_6_1 60100
  70. #endif
  71. #ifndef __IPHONE_7_0
  72. #define __IPHONE_7_0 70000
  73. #endif
  74. #ifndef __IPHONE_7_1
  75. #define __IPHONE_7_1 70100
  76. #endif
  77. // ----------------------------------------------------------------------------
  78. // CPP symbols that can be overridden in a prefix to control how the toolbox
  79. // is compiled.
  80. // ----------------------------------------------------------------------------
  81. // By setting the GTM_CONTAINERS_VALIDATION_FAILED_LOG and
  82. // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT macros you can control what happens
  83. // when a validation fails. If you implement your own validators, you may want
  84. // to control their internals using the same macros for consistency.
  85. #ifndef GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
  86. #define GTM_CONTAINERS_VALIDATION_FAILED_ASSERT 0
  87. #endif
  88. // Give ourselves a consistent way to do inlines. Apple's macros even use
  89. // a few different actual definitions, so we're based off of the foundation
  90. // one.
  91. #if !defined(GTM_INLINE)
  92. #if (defined (__GNUC__) && (__GNUC__ == 4)) || defined (__clang__)
  93. #define GTM_INLINE static __inline__ __attribute__((always_inline))
  94. #else
  95. #define GTM_INLINE static __inline__
  96. #endif
  97. #endif
  98. // Give ourselves a consistent way of doing externs that links up nicely
  99. // when mixing objc and objc++
  100. #if !defined (GTM_EXTERN)
  101. #if defined __cplusplus
  102. #define GTM_EXTERN extern "C"
  103. #define GTM_EXTERN_C_BEGIN extern "C" {
  104. #define GTM_EXTERN_C_END }
  105. #else
  106. #define GTM_EXTERN extern
  107. #define GTM_EXTERN_C_BEGIN
  108. #define GTM_EXTERN_C_END
  109. #endif
  110. #endif
  111. // Give ourselves a consistent way of exporting things if we have visibility
  112. // set to hidden.
  113. #if !defined (GTM_EXPORT)
  114. #define GTM_EXPORT __attribute__((visibility("default")))
  115. #endif
  116. // Give ourselves a consistent way of declaring something as unused. This
  117. // doesn't use __unused because that is only supported in gcc 4.2 and greater.
  118. #if !defined (GTM_UNUSED)
  119. #define GTM_UNUSED(x) ((void)(x))
  120. #endif
  121. // _GTMDevLog & _GTMDevAssert
  122. //
  123. // _GTMDevLog & _GTMDevAssert are meant to be a very lightweight shell for
  124. // developer level errors. This implementation simply macros to NSLog/NSAssert.
  125. // It is not intended to be a general logging/reporting system.
  126. //
  127. // Please see http://code.google.com/p/google-toolbox-for-mac/wiki/DevLogNAssert
  128. // for a little more background on the usage of these macros.
  129. //
  130. // _GTMDevLog log some error/problem in debug builds
  131. // _GTMDevAssert assert if conditon isn't met w/in a method/function
  132. // in all builds.
  133. //
  134. // To replace this system, just provide different macro definitions in your
  135. // prefix header. Remember, any implementation you provide *must* be thread
  136. // safe since this could be called by anything in what ever situtation it has
  137. // been placed in.
  138. //
  139. // We only define the simple macros if nothing else has defined this.
  140. #ifndef _GTMDevLog
  141. #ifdef DEBUG
  142. #define _GTMDevLog(...) NSLog(__VA_ARGS__)
  143. #else
  144. #define _GTMDevLog(...) do { } while (0)
  145. #endif
  146. #endif // _GTMDevLog
  147. #ifndef _GTMDevAssert
  148. // we directly invoke the NSAssert handler so we can pass on the varargs
  149. // (NSAssert doesn't have a macro we can use that takes varargs)
  150. #if !defined(NS_BLOCK_ASSERTIONS)
  151. #define _GTMDevAssert(condition, ...) \
  152. do { \
  153. if (!(condition)) { \
  154. [[NSAssertionHandler currentHandler] \
  155. handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
  156. file:[NSString stringWithUTF8String:__FILE__] \
  157. lineNumber:__LINE__ \
  158. description:__VA_ARGS__]; \
  159. } \
  160. } while(0)
  161. #else // !defined(NS_BLOCK_ASSERTIONS)
  162. #define _GTMDevAssert(condition, ...) do { } while (0)
  163. #endif // !defined(NS_BLOCK_ASSERTIONS)
  164. #endif // _GTMDevAssert
  165. // _GTMCompileAssert
  166. // _GTMCompileAssert is an assert that is meant to fire at compile time if you
  167. // want to check things at compile instead of runtime. For example if you
  168. // want to check that a wchar is 4 bytes instead of 2 you would use
  169. // _GTMCompileAssert(sizeof(wchar_t) == 4, wchar_t_is_4_bytes_on_OS_X)
  170. // Note that the second "arg" is not in quotes, and must be a valid processor
  171. // symbol in it's own right (no spaces, punctuation etc).
  172. // Wrapping this in an #ifndef allows external groups to define their own
  173. // compile time assert scheme.
  174. #ifndef _GTMCompileAssert
  175. // We got this technique from here:
  176. // http://unixjunkie.blogspot.com/2007/10/better-compile-time-asserts_29.html
  177. #define _GTMCompileAssertSymbolInner(line, msg) _GTMCOMPILEASSERT ## line ## __ ## msg
  178. #define _GTMCompileAssertSymbol(line, msg) _GTMCompileAssertSymbolInner(line, msg)
  179. #define _GTMCompileAssert(test, msg) \
  180. typedef char _GTMCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  181. #endif // _GTMCompileAssert
  182. // ----------------------------------------------------------------------------
  183. // CPP symbols defined based on the project settings so the GTM code has
  184. // simple things to test against w/o scattering the knowledge of project
  185. // setting through all the code.
  186. // ----------------------------------------------------------------------------
  187. // Provide a single constant CPP symbol that all of GTM uses for ifdefing
  188. // iPhone code.
  189. #if TARGET_OS_IPHONE // iPhone SDK
  190. // For iPhone specific stuff
  191. #define GTM_IPHONE_SDK 1
  192. #if TARGET_IPHONE_SIMULATOR
  193. #define GTM_IPHONE_DEVICE 0
  194. #define GTM_IPHONE_SIMULATOR 1
  195. #else
  196. #define GTM_IPHONE_DEVICE 1
  197. #define GTM_IPHONE_SIMULATOR 0
  198. #endif // TARGET_IPHONE_SIMULATOR
  199. // By default, GTM has provided it's own unittesting support, define this
  200. // to use the support provided by Xcode, especially for the Xcode4 support
  201. // for unittesting.
  202. // This is going to be deprecated as Apple is deprecating SenTest.
  203. #ifndef GTM_IPHONE_USE_SENTEST
  204. #define GTM_IPHONE_USE_SENTEST 0
  205. #endif
  206. // Define this to use XCTest instead of OCUnit/SenTest.
  207. #ifndef GTM_USING_XCTEST
  208. #define GTM_USING_XCTEST 0
  209. #endif
  210. #if GTM_IPHONE_USE_SENTEST && GTM_USING_XCTEST
  211. #error Can't define both GTM_IPHONE_USE_SENTEST and GTM_USING_XCTEST
  212. #endif
  213. #define GTM_MACOS_SDK 0
  214. #else
  215. // For MacOS specific stuff
  216. #define GTM_MACOS_SDK 1
  217. #define GTM_IPHONE_SDK 0
  218. #define GTM_IPHONE_SIMULATOR 0
  219. #define GTM_IPHONE_DEVICE 0
  220. #define GTM_IPHONE_USE_SENTEST 0
  221. #define GTM_USING_XCTEST 0
  222. #endif
  223. // Some of our own availability macros
  224. #if GTM_MACOS_SDK
  225. #define GTM_AVAILABLE_ONLY_ON_IPHONE UNAVAILABLE_ATTRIBUTE
  226. #define GTM_AVAILABLE_ONLY_ON_MACOS
  227. #else
  228. #define GTM_AVAILABLE_ONLY_ON_IPHONE
  229. #define GTM_AVAILABLE_ONLY_ON_MACOS UNAVAILABLE_ATTRIBUTE
  230. #endif
  231. // GC was dropped by Apple, define the old constant incase anyone still keys
  232. // off of it.
  233. #ifndef GTM_SUPPORT_GC
  234. #define GTM_SUPPORT_GC 0
  235. #endif
  236. // To simplify support for 64bit (and Leopard in general), we provide the type
  237. // defines for non Leopard SDKs
  238. #if !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
  239. // NSInteger/NSUInteger and Max/Mins
  240. #ifndef NSINTEGER_DEFINED
  241. #if (defined(__LP64__) && __LP64__) || NS_BUILD_32_LIKE_64
  242. typedef long NSInteger;
  243. typedef unsigned long NSUInteger;
  244. #else
  245. typedef int NSInteger;
  246. typedef unsigned int NSUInteger;
  247. #endif
  248. #define NSIntegerMax LONG_MAX
  249. #define NSIntegerMin LONG_MIN
  250. #define NSUIntegerMax ULONG_MAX
  251. #define NSINTEGER_DEFINED 1
  252. #endif // NSINTEGER_DEFINED
  253. // CGFloat
  254. #ifndef CGFLOAT_DEFINED
  255. #if defined(__LP64__) && __LP64__
  256. // This really is an untested path (64bit on Tiger?)
  257. typedef double CGFloat;
  258. #define CGFLOAT_MIN DBL_MIN
  259. #define CGFLOAT_MAX DBL_MAX
  260. #define CGFLOAT_IS_DOUBLE 1
  261. #else /* !defined(__LP64__) || !__LP64__ */
  262. typedef float CGFloat;
  263. #define CGFLOAT_MIN FLT_MIN
  264. #define CGFLOAT_MAX FLT_MAX
  265. #define CGFLOAT_IS_DOUBLE 0
  266. #endif /* !defined(__LP64__) || !__LP64__ */
  267. #define CGFLOAT_DEFINED 1
  268. #endif // CGFLOAT_DEFINED
  269. #endif // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  270. // Some support for advanced clang static analysis functionality
  271. // See http://clang-analyzer.llvm.org/annotations.html
  272. #ifndef __has_feature // Optional.
  273. #define __has_feature(x) 0 // Compatibility with non-clang compilers.
  274. #endif
  275. #ifndef NS_RETURNS_RETAINED
  276. #if __has_feature(attribute_ns_returns_retained)
  277. #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
  278. #else
  279. #define NS_RETURNS_RETAINED
  280. #endif
  281. #endif
  282. #ifndef NS_RETURNS_NOT_RETAINED
  283. #if __has_feature(attribute_ns_returns_not_retained)
  284. #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
  285. #else
  286. #define NS_RETURNS_NOT_RETAINED
  287. #endif
  288. #endif
  289. #ifndef CF_RETURNS_RETAINED
  290. #if __has_feature(attribute_cf_returns_retained)
  291. #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
  292. #else
  293. #define CF_RETURNS_RETAINED
  294. #endif
  295. #endif
  296. #ifndef CF_RETURNS_NOT_RETAINED
  297. #if __has_feature(attribute_cf_returns_not_retained)
  298. #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
  299. #else
  300. #define CF_RETURNS_NOT_RETAINED
  301. #endif
  302. #endif
  303. #ifndef NS_CONSUMED
  304. #if __has_feature(attribute_ns_consumed)
  305. #define NS_CONSUMED __attribute__((ns_consumed))
  306. #else
  307. #define NS_CONSUMED
  308. #endif
  309. #endif
  310. #ifndef CF_CONSUMED
  311. #if __has_feature(attribute_cf_consumed)
  312. #define CF_CONSUMED __attribute__((cf_consumed))
  313. #else
  314. #define CF_CONSUMED
  315. #endif
  316. #endif
  317. #ifndef NS_CONSUMES_SELF
  318. #if __has_feature(attribute_ns_consumes_self)
  319. #define NS_CONSUMES_SELF __attribute__((ns_consumes_self))
  320. #else
  321. #define NS_CONSUMES_SELF
  322. #endif
  323. #endif
  324. // Defined on 10.6 and above.
  325. #ifndef NS_FORMAT_ARGUMENT
  326. #define NS_FORMAT_ARGUMENT(A)
  327. #endif
  328. // Defined on 10.6 and above.
  329. #ifndef NS_FORMAT_FUNCTION
  330. #define NS_FORMAT_FUNCTION(F,A)
  331. #endif
  332. // Defined on 10.6 and above.
  333. #ifndef CF_FORMAT_ARGUMENT
  334. #define CF_FORMAT_ARGUMENT(A)
  335. #endif
  336. // Defined on 10.6 and above.
  337. #ifndef CF_FORMAT_FUNCTION
  338. #define CF_FORMAT_FUNCTION(F,A)
  339. #endif
  340. #ifndef GTM_NONNULL
  341. #if defined(__has_attribute)
  342. #if __has_attribute(nonnull)
  343. #define GTM_NONNULL(x) __attribute__((nonnull x))
  344. #else
  345. #define GTM_NONNULL(x)
  346. #endif
  347. #else
  348. #define GTM_NONNULL(x)
  349. #endif
  350. #endif
  351. // Invalidates the initializer from which it's called.
  352. #ifndef GTMInvalidateInitializer
  353. #if __has_feature(objc_arc)
  354. #define GTMInvalidateInitializer() \
  355. do { \
  356. [self class]; /* Avoid warning of dead store to |self|. */ \
  357. _GTMDevAssert(NO, @"Invalid initializer."); \
  358. return nil; \
  359. } while (0)
  360. #else
  361. #define GTMInvalidateInitializer() \
  362. do { \
  363. [self release]; \
  364. _GTMDevAssert(NO, @"Invalid initializer."); \
  365. return nil; \
  366. } while (0)
  367. #endif
  368. #endif
  369. #ifndef GTMCFAutorelease
  370. // GTMCFAutorelease returns an id. In contrast, Apple's CFAutorelease returns
  371. // a CFTypeRef.
  372. #if __has_feature(objc_arc)
  373. #define GTMCFAutorelease(x) CFBridgingRelease(x)
  374. #else
  375. #define GTMCFAutorelease(x) ([(id)x autorelease])
  376. #endif
  377. #endif
  378. #ifdef __OBJC__
  379. // Declared here so that it can easily be used for logging tracking if
  380. // necessary. See GTMUnitTestDevLog.h for details.
  381. @class NSString;
  382. GTM_EXTERN void _GTMUnitTestDevLog(NSString *format, ...) NS_FORMAT_FUNCTION(1, 2);
  383. // Macro to allow you to create NSStrings out of other macros.
  384. // #define FOO foo
  385. // NSString *fooString = GTM_NSSTRINGIFY(FOO);
  386. #if !defined (GTM_NSSTRINGIFY)
  387. #define GTM_NSSTRINGIFY_INNER(x) @#x
  388. #define GTM_NSSTRINGIFY(x) GTM_NSSTRINGIFY_INNER(x)
  389. #endif
  390. // Macro to allow fast enumeration when building for 10.5 or later, and
  391. // reliance on NSEnumerator for 10.4. Remember, NSDictionary w/ FastEnumeration
  392. // does keys, so pick the right thing, nothing is done on the FastEnumeration
  393. // side to be sure you're getting what you wanted.
  394. #ifndef GTM_FOREACH_OBJECT
  395. #if TARGET_OS_IPHONE || !(MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  396. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  397. for (element in enumeration)
  398. #define GTM_FOREACH_OBJECT(element, collection) \
  399. for (element in collection)
  400. #define GTM_FOREACH_KEY(element, collection) \
  401. for (element in collection)
  402. #else
  403. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  404. for (NSEnumerator *_ ## element ## _enum = enumeration; \
  405. (element = [_ ## element ## _enum nextObject]) != nil; )
  406. #define GTM_FOREACH_OBJECT(element, collection) \
  407. GTM_FOREACH_ENUMEREE(element, [collection objectEnumerator])
  408. #define GTM_FOREACH_KEY(element, collection) \
  409. GTM_FOREACH_ENUMEREE(element, [collection keyEnumerator])
  410. #endif
  411. #endif
  412. // ============================================================================
  413. // To simplify support for both Leopard and Snow Leopard we declare
  414. // the Snow Leopard protocols that we need here.
  415. #if !defined(GTM_10_6_PROTOCOLS_DEFINED) && !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
  416. #define GTM_10_6_PROTOCOLS_DEFINED 1
  417. @protocol NSConnectionDelegate
  418. @end
  419. @protocol NSAnimationDelegate
  420. @end
  421. @protocol NSImageDelegate
  422. @end
  423. @protocol NSTabViewDelegate
  424. @end
  425. #endif // !defined(GTM_10_6_PROTOCOLS_DEFINED) && !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
  426. // GTM_SEL_STRING is for specifying selector (usually property) names to KVC
  427. // or KVO methods.
  428. // In debug it will generate warnings for undeclared selectors if
  429. // -Wunknown-selector is turned on.
  430. // In release it will have no runtime overhead.
  431. #ifndef GTM_SEL_STRING
  432. #ifdef DEBUG
  433. #define GTM_SEL_STRING(selName) NSStringFromSelector(@selector(selName))
  434. #else
  435. #define GTM_SEL_STRING(selName) @#selName
  436. #endif // DEBUG
  437. #endif // GTM_SEL_STRING
  438. #endif // __OBJC__