GTMDefines.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #if TARGET_OS_IPHONE
  22. #include <Availability.h>
  23. #endif // TARGET_OS_IPHONE
  24. // Not all MAC_OS_X_VERSION_10_X macros defined in past SDKs
  25. #ifndef MAC_OS_X_VERSION_10_5
  26. #define MAC_OS_X_VERSION_10_5 1050
  27. #endif
  28. #ifndef MAC_OS_X_VERSION_10_6
  29. #define MAC_OS_X_VERSION_10_6 1060
  30. #endif
  31. // Not all __IPHONE_X macros defined in past SDKs
  32. #ifndef __IPHONE_2_1
  33. #define __IPHONE_2_1 20100
  34. #endif
  35. #ifndef __IPHONE_2_2
  36. #define __IPHONE_2_2 20200
  37. #endif
  38. #ifndef __IPHONE_3_0
  39. #define __IPHONE_3_0 30000
  40. #endif
  41. #ifndef __IPHONE_3_1
  42. #define __IPHONE_3_1 30100
  43. #endif
  44. #ifndef __IPHONE_3_2
  45. #define __IPHONE_3_2 30200
  46. #endif
  47. #ifndef __IPHONE_4_0
  48. #define __IPHONE_4_0 40000
  49. #endif
  50. // ----------------------------------------------------------------------------
  51. // CPP symbols that can be overridden in a prefix to control how the toolbox
  52. // is compiled.
  53. // ----------------------------------------------------------------------------
  54. // By setting the GTM_CONTAINERS_VALIDATION_FAILED_LOG and
  55. // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT macros you can control what happens
  56. // when a validation fails. If you implement your own validators, you may want
  57. // to control their internals using the same macros for consistency.
  58. #ifndef GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
  59. #define GTM_CONTAINERS_VALIDATION_FAILED_ASSERT 0
  60. #endif
  61. // Give ourselves a consistent way to do inlines. Apple's macros even use
  62. // a few different actual definitions, so we're based off of the foundation
  63. // one.
  64. #if !defined(GTM_INLINE)
  65. #if defined (__GNUC__) && (__GNUC__ == 4)
  66. #define GTM_INLINE static __inline__ __attribute__((always_inline))
  67. #else
  68. #define GTM_INLINE static __inline__
  69. #endif
  70. #endif
  71. // Give ourselves a consistent way of doing externs that links up nicely
  72. // when mixing objc and objc++
  73. #if !defined (GTM_EXTERN)
  74. #if defined __cplusplus
  75. #define GTM_EXTERN extern "C"
  76. #define GTM_EXTERN_C_BEGIN extern "C" {
  77. #define GTM_EXTERN_C_END }
  78. #else
  79. #define GTM_EXTERN extern
  80. #define GTM_EXTERN_C_BEGIN
  81. #define GTM_EXTERN_C_END
  82. #endif
  83. #endif
  84. // Give ourselves a consistent way of exporting things if we have visibility
  85. // set to hidden.
  86. #if !defined (GTM_EXPORT)
  87. #define GTM_EXPORT __attribute__((visibility("default")))
  88. #endif
  89. // Give ourselves a consistent way of declaring something as unused. This
  90. // doesn't use __unused because that is only supported in gcc 4.2 and greater.
  91. #if !defined (GTM_UNUSED)
  92. #define GTM_UNUSED(x) ((void)(x))
  93. #endif
  94. // _GTMDevLog & _GTMDevAssert
  95. //
  96. // _GTMDevLog & _GTMDevAssert are meant to be a very lightweight shell for
  97. // developer level errors. This implementation simply macros to NSLog/NSAssert.
  98. // It is not intended to be a general logging/reporting system.
  99. //
  100. // Please see http://code.google.com/p/google-toolbox-for-mac/wiki/DevLogNAssert
  101. // for a little more background on the usage of these macros.
  102. //
  103. // _GTMDevLog log some error/problem in debug builds
  104. // _GTMDevAssert assert if conditon isn't met w/in a method/function
  105. // in all builds.
  106. //
  107. // To replace this system, just provide different macro definitions in your
  108. // prefix header. Remember, any implementation you provide *must* be thread
  109. // safe since this could be called by anything in what ever situtation it has
  110. // been placed in.
  111. //
  112. // We only define the simple macros if nothing else has defined this.
  113. #ifndef _GTMDevLog
  114. #ifdef DEBUG
  115. #define _GTMDevLog(...) NSLog(__VA_ARGS__)
  116. #else
  117. #define _GTMDevLog(...) do { } while (0)
  118. #endif
  119. #endif // _GTMDevLog
  120. #ifndef _GTMDevAssert
  121. // we directly invoke the NSAssert handler so we can pass on the varargs
  122. // (NSAssert doesn't have a macro we can use that takes varargs)
  123. #if !defined(NS_BLOCK_ASSERTIONS)
  124. #define _GTMDevAssert(condition, ...) \
  125. do { \
  126. if (!(condition)) { \
  127. [[NSAssertionHandler currentHandler] \
  128. handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
  129. file:[NSString stringWithUTF8String:__FILE__] \
  130. lineNumber:__LINE__ \
  131. description:__VA_ARGS__]; \
  132. } \
  133. } while(0)
  134. #else // !defined(NS_BLOCK_ASSERTIONS)
  135. #define _GTMDevAssert(condition, ...) do { } while (0)
  136. #endif // !defined(NS_BLOCK_ASSERTIONS)
  137. #endif // _GTMDevAssert
  138. // _GTMCompileAssert
  139. // _GTMCompileAssert is an assert that is meant to fire at compile time if you
  140. // want to check things at compile instead of runtime. For example if you
  141. // want to check that a wchar is 4 bytes instead of 2 you would use
  142. // _GTMCompileAssert(sizeof(wchar_t) == 4, wchar_t_is_4_bytes_on_OS_X)
  143. // Note that the second "arg" is not in quotes, and must be a valid processor
  144. // symbol in it's own right (no spaces, punctuation etc).
  145. // Wrapping this in an #ifndef allows external groups to define their own
  146. // compile time assert scheme.
  147. #ifndef _GTMCompileAssert
  148. // We got this technique from here:
  149. // http://unixjunkie.blogspot.com/2007/10/better-compile-time-asserts_29.html
  150. #define _GTMCompileAssertSymbolInner(line, msg) _GTMCOMPILEASSERT ## line ## __ ## msg
  151. #define _GTMCompileAssertSymbol(line, msg) _GTMCompileAssertSymbolInner(line, msg)
  152. #define _GTMCompileAssert(test, msg) \
  153. typedef char _GTMCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  154. #endif // _GTMCompileAssert
  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_SIMULATOR 1
  167. #else
  168. #define GTM_IPHONE_DEVICE 1
  169. #endif // TARGET_IPHONE_SIMULATOR
  170. #else
  171. // For MacOS specific stuff
  172. #define GTM_MACOS_SDK 1
  173. #endif
  174. // Some of our own availability macros
  175. #if GTM_MACOS_SDK
  176. #define GTM_AVAILABLE_ONLY_ON_IPHONE UNAVAILABLE_ATTRIBUTE
  177. #define GTM_AVAILABLE_ONLY_ON_MACOS
  178. #else
  179. #define GTM_AVAILABLE_ONLY_ON_IPHONE
  180. #define GTM_AVAILABLE_ONLY_ON_MACOS UNAVAILABLE_ATTRIBUTE
  181. #endif
  182. // Provide a symbol to include/exclude extra code for GC support. (This mainly
  183. // just controls the inclusion of finalize methods).
  184. #ifndef GTM_SUPPORT_GC
  185. #if GTM_IPHONE_SDK
  186. // iPhone never needs GC
  187. #define GTM_SUPPORT_GC 0
  188. #else
  189. // We can't find a symbol to tell if GC is supported/required, so best we
  190. // do on Mac targets is include it if we're on 10.5 or later.
  191. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  192. #define GTM_SUPPORT_GC 0
  193. #else
  194. #define GTM_SUPPORT_GC 1
  195. #endif
  196. #endif
  197. #endif
  198. // To simplify support for 64bit (and Leopard in general), we provide the type
  199. // defines for non Leopard SDKs
  200. #if !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
  201. // NSInteger/NSUInteger and Max/Mins
  202. #ifndef NSINTEGER_DEFINED
  203. #if __LP64__ || NS_BUILD_32_LIKE_64
  204. typedef long NSInteger;
  205. typedef unsigned long NSUInteger;
  206. #else
  207. typedef int NSInteger;
  208. typedef unsigned int NSUInteger;
  209. #endif
  210. #define NSIntegerMax LONG_MAX
  211. #define NSIntegerMin LONG_MIN
  212. #define NSUIntegerMax ULONG_MAX
  213. #define NSINTEGER_DEFINED 1
  214. #endif // NSINTEGER_DEFINED
  215. // CGFloat
  216. #ifndef CGFLOAT_DEFINED
  217. #if defined(__LP64__) && __LP64__
  218. // This really is an untested path (64bit on Tiger?)
  219. typedef double CGFloat;
  220. #define CGFLOAT_MIN DBL_MIN
  221. #define CGFLOAT_MAX DBL_MAX
  222. #define CGFLOAT_IS_DOUBLE 1
  223. #else /* !defined(__LP64__) || !__LP64__ */
  224. typedef float CGFloat;
  225. #define CGFLOAT_MIN FLT_MIN
  226. #define CGFLOAT_MAX FLT_MAX
  227. #define CGFLOAT_IS_DOUBLE 0
  228. #endif /* !defined(__LP64__) || !__LP64__ */
  229. #define CGFLOAT_DEFINED 1
  230. #endif // CGFLOAT_DEFINED
  231. #endif // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  232. // Some support for advanced clang static analysis functionality
  233. // See http://clang-analyzer.llvm.org/annotations.html
  234. #ifndef __has_feature // Optional.
  235. #define __has_feature(x) 0 // Compatibility with non-clang compilers.
  236. #endif
  237. #ifndef NS_RETURNS_RETAINED
  238. #if __has_feature(attribute_ns_returns_retained)
  239. #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
  240. #else
  241. #define NS_RETURNS_RETAINED
  242. #endif
  243. #endif
  244. #ifndef NS_RETURNS_NOT_RETAINED
  245. #if __has_feature(attribute_ns_returns_not_retained)
  246. #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
  247. #else
  248. #define NS_RETURNS_NOT_RETAINED
  249. #endif
  250. #endif
  251. #ifndef CF_RETURNS_RETAINED
  252. #if __has_feature(attribute_cf_returns_retained)
  253. #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
  254. #else
  255. #define CF_RETURNS_RETAINED
  256. #endif
  257. #endif
  258. #ifndef CF_RETURNS_NOT_RETAINED
  259. #if __has_feature(attribute_cf_returns_not_retained)
  260. #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
  261. #else
  262. #define CF_RETURNS_NOT_RETAINED
  263. #endif
  264. #endif
  265. // Defined on 10.6 and above.
  266. #ifndef NS_FORMAT_ARGUMENT
  267. #define NS_FORMAT_ARGUMENT(A)
  268. #endif
  269. // Defined on 10.6 and above.
  270. #ifndef NS_FORMAT_FUNCTION
  271. #define NS_FORMAT_FUNCTION(F,A)
  272. #endif
  273. // Defined on 10.6 and above.
  274. #ifndef CF_FORMAT_ARGUMENT
  275. #define CF_FORMAT_ARGUMENT(A)
  276. #endif
  277. // Defined on 10.6 and above.
  278. #ifndef CF_FORMAT_FUNCTION
  279. #define CF_FORMAT_FUNCTION(F,A)
  280. #endif
  281. #ifndef GTM_NONNULL
  282. #define GTM_NONNULL(x) __attribute__((nonnull(x)))
  283. #endif
  284. #ifdef __OBJC__
  285. // Declared here so that it can easily be used for logging tracking if
  286. // necessary. See GTMUnitTestDevLog.h for details.
  287. @class NSString;
  288. GTM_EXTERN void _GTMUnitTestDevLog(NSString *format, ...);
  289. // Macro to allow you to create NSStrings out of other macros.
  290. // #define FOO foo
  291. // NSString *fooString = GTM_NSSTRINGIFY(FOO);
  292. #if !defined (GTM_NSSTRINGIFY)
  293. #define GTM_NSSTRINGIFY_INNER(x) @#x
  294. #define GTM_NSSTRINGIFY(x) GTM_NSSTRINGIFY_INNER(x)
  295. #endif
  296. // Macro to allow fast enumeration when building for 10.5 or later, and
  297. // reliance on NSEnumerator for 10.4. Remember, NSDictionary w/ FastEnumeration
  298. // does keys, so pick the right thing, nothing is done on the FastEnumeration
  299. // side to be sure you're getting what you wanted.
  300. #ifndef GTM_FOREACH_OBJECT
  301. #if TARGET_OS_IPHONE || !(MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  302. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  303. for (element in enumeration)
  304. #define GTM_FOREACH_OBJECT(element, collection) \
  305. for (element in collection)
  306. #define GTM_FOREACH_KEY(element, collection) \
  307. for (element in collection)
  308. #else
  309. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  310. for (NSEnumerator *_ ## element ## _enum = enumeration; \
  311. (element = [_ ## element ## _enum nextObject]) != nil; )
  312. #define GTM_FOREACH_OBJECT(element, collection) \
  313. GTM_FOREACH_ENUMEREE(element, [collection objectEnumerator])
  314. #define GTM_FOREACH_KEY(element, collection) \
  315. GTM_FOREACH_ENUMEREE(element, [collection keyEnumerator])
  316. #endif
  317. #endif
  318. // ============================================================================
  319. // To simplify support for both Leopard and Snow Leopard we declare
  320. // the Snow Leopard protocols that we need here.
  321. #if !defined(GTM_10_6_PROTOCOLS_DEFINED) && !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
  322. #define GTM_10_6_PROTOCOLS_DEFINED 1
  323. @protocol NSConnectionDelegate
  324. @end
  325. @protocol NSAnimationDelegate
  326. @end
  327. @protocol NSImageDelegate
  328. @end
  329. @protocol NSTabViewDelegate
  330. @end
  331. #endif // !defined(GTM_10_6_PROTOCOLS_DEFINED) && !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
  332. // GTM_SEL_STRING is for specifying selector (usually property) names to KVC
  333. // or KVO methods.
  334. // In debug it will generate warnings for undeclared selectors if
  335. // -Wunknown-selector is turned on.
  336. // In release it will have no runtime overhead.
  337. #ifndef GTM_SEL_STRING
  338. #ifdef DEBUG
  339. #define GTM_SEL_STRING(selName) NSStringFromSelector(@selector(selName))
  340. #else
  341. #define GTM_SEL_STRING(selName) @#selName
  342. #endif // DEBUG
  343. #endif // GTM_SEL_STRING
  344. #endif // __OBJC__