GTMDefines.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #else
  77. #define GTM_EXTERN extern
  78. #endif
  79. #endif
  80. // Give ourselves a consistent way of exporting things if we have visibility
  81. // set to hidden.
  82. #if !defined (GTM_EXPORT)
  83. #define GTM_EXPORT __attribute__((visibility("default")))
  84. #endif
  85. // _GTMDevLog & _GTMDevAssert
  86. //
  87. // _GTMDevLog & _GTMDevAssert are meant to be a very lightweight shell for
  88. // developer level errors. This implementation simply macros to NSLog/NSAssert.
  89. // It is not intended to be a general logging/reporting system.
  90. //
  91. // Please see http://code.google.com/p/google-toolbox-for-mac/wiki/DevLogNAssert
  92. // for a little more background on the usage of these macros.
  93. //
  94. // _GTMDevLog log some error/problem in debug builds
  95. // _GTMDevAssert assert if conditon isn't met w/in a method/function
  96. // in all builds.
  97. //
  98. // To replace this system, just provide different macro definitions in your
  99. // prefix header. Remember, any implementation you provide *must* be thread
  100. // safe since this could be called by anything in what ever situtation it has
  101. // been placed in.
  102. //
  103. // We only define the simple macros if nothing else has defined this.
  104. #ifndef _GTMDevLog
  105. #ifdef DEBUG
  106. #define _GTMDevLog(...) NSLog(__VA_ARGS__)
  107. #else
  108. #define _GTMDevLog(...) do { } while (0)
  109. #endif
  110. #endif // _GTMDevLog
  111. // Declared here so that it can easily be used for logging tracking if
  112. // necessary. See GTMUnitTestDevLog.h for details.
  113. @class NSString;
  114. GTM_EXTERN void _GTMUnitTestDevLog(NSString *format, ...);
  115. #ifndef _GTMDevAssert
  116. // we directly invoke the NSAssert handler so we can pass on the varargs
  117. // (NSAssert doesn't have a macro we can use that takes varargs)
  118. #if !defined(NS_BLOCK_ASSERTIONS)
  119. #define _GTMDevAssert(condition, ...) \
  120. do { \
  121. if (!(condition)) { \
  122. [[NSAssertionHandler currentHandler] \
  123. handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
  124. file:[NSString stringWithUTF8String:__FILE__] \
  125. lineNumber:__LINE__ \
  126. description:__VA_ARGS__]; \
  127. } \
  128. } while(0)
  129. #else // !defined(NS_BLOCK_ASSERTIONS)
  130. #define _GTMDevAssert(condition, ...) do { } while (0)
  131. #endif // !defined(NS_BLOCK_ASSERTIONS)
  132. #endif // _GTMDevAssert
  133. // _GTMCompileAssert
  134. // _GTMCompileAssert is an assert that is meant to fire at compile time if you
  135. // want to check things at compile instead of runtime. For example if you
  136. // want to check that a wchar is 4 bytes instead of 2 you would use
  137. // _GTMCompileAssert(sizeof(wchar_t) == 4, wchar_t_is_4_bytes_on_OS_X)
  138. // Note that the second "arg" is not in quotes, and must be a valid processor
  139. // symbol in it's own right (no spaces, punctuation etc).
  140. // Wrapping this in an #ifndef allows external groups to define their own
  141. // compile time assert scheme.
  142. #ifndef _GTMCompileAssert
  143. // We got this technique from here:
  144. // http://unixjunkie.blogspot.com/2007/10/better-compile-time-asserts_29.html
  145. #define _GTMCompileAssertSymbolInner(line, msg) _GTMCOMPILEASSERT ## line ## __ ## msg
  146. #define _GTMCompileAssertSymbol(line, msg) _GTMCompileAssertSymbolInner(line, msg)
  147. #define _GTMCompileAssert(test, msg) \
  148. typedef char _GTMCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  149. #endif // _GTMCompileAssert
  150. // Macro to allow you to create NSStrings out of other macros.
  151. // #define FOO foo
  152. // NSString *fooString = GTM_NSSTRINGIFY(FOO);
  153. #if !defined (GTM_NSSTRINGIFY)
  154. #define GTM_NSSTRINGIFY_INNER(x) @#x
  155. #define GTM_NSSTRINGIFY(x) GTM_NSSTRINGIFY_INNER(x)
  156. #endif
  157. // Macro to allow fast enumeration when building for 10.5 or later, and
  158. // reliance on NSEnumerator for 10.4. Remember, NSDictionary w/ FastEnumeration
  159. // does keys, so pick the right thing, nothing is done on the FastEnumeration
  160. // side to be sure you're getting what you wanted.
  161. #ifndef GTM_FOREACH_OBJECT
  162. #if TARGET_OS_IPHONE || !(MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  163. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  164. for (element in enumeration)
  165. #define GTM_FOREACH_OBJECT(element, collection) \
  166. for (element in collection)
  167. #define GTM_FOREACH_KEY(element, collection) \
  168. for (element in collection)
  169. #else
  170. #define GTM_FOREACH_ENUMEREE(element, enumeration) \
  171. for (NSEnumerator *_ ## element ## _enum = enumeration; \
  172. (element = [_ ## element ## _enum nextObject]) != nil; )
  173. #define GTM_FOREACH_OBJECT(element, collection) \
  174. GTM_FOREACH_ENUMEREE(element, [collection objectEnumerator])
  175. #define GTM_FOREACH_KEY(element, collection) \
  176. GTM_FOREACH_ENUMEREE(element, [collection keyEnumerator])
  177. #endif
  178. #endif
  179. // ============================================================================
  180. // ----------------------------------------------------------------------------
  181. // CPP symbols defined based on the project settings so the GTM code has
  182. // simple things to test against w/o scattering the knowledge of project
  183. // setting through all the code.
  184. // ----------------------------------------------------------------------------
  185. // Provide a single constant CPP symbol that all of GTM uses for ifdefing
  186. // iPhone code.
  187. #if TARGET_OS_IPHONE // iPhone SDK
  188. // For iPhone specific stuff
  189. #define GTM_IPHONE_SDK 1
  190. #if TARGET_IPHONE_SIMULATOR
  191. #define GTM_IPHONE_SIMULATOR 1
  192. #else
  193. #define GTM_IPHONE_DEVICE 1
  194. #endif // TARGET_IPHONE_SIMULATOR
  195. #else
  196. // For MacOS specific stuff
  197. #define GTM_MACOS_SDK 1
  198. #endif
  199. // Some of our own availability macros
  200. #if GTM_MACOS_SDK
  201. #define GTM_AVAILABLE_ONLY_ON_IPHONE UNAVAILABLE_ATTRIBUTE
  202. #define GTM_AVAILABLE_ONLY_ON_MACOS
  203. #else
  204. #define GTM_AVAILABLE_ONLY_ON_IPHONE
  205. #define GTM_AVAILABLE_ONLY_ON_MACOS UNAVAILABLE_ATTRIBUTE
  206. #endif
  207. // Provide a symbol to include/exclude extra code for GC support. (This mainly
  208. // just controls the inclusion of finalize methods).
  209. #ifndef GTM_SUPPORT_GC
  210. #if GTM_IPHONE_SDK
  211. // iPhone never needs GC
  212. #define GTM_SUPPORT_GC 0
  213. #else
  214. // We can't find a symbol to tell if GC is supported/required, so best we
  215. // do on Mac targets is include it if we're on 10.5 or later.
  216. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  217. #define GTM_SUPPORT_GC 0
  218. #else
  219. #define GTM_SUPPORT_GC 1
  220. #endif
  221. #endif
  222. #endif
  223. // To simplify support for 64bit (and Leopard in general), we provide the type
  224. // defines for non Leopard SDKs
  225. #if !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
  226. // NSInteger/NSUInteger and Max/Mins
  227. #ifndef NSINTEGER_DEFINED
  228. #if __LP64__ || NS_BUILD_32_LIKE_64
  229. typedef long NSInteger;
  230. typedef unsigned long NSUInteger;
  231. #else
  232. typedef int NSInteger;
  233. typedef unsigned int NSUInteger;
  234. #endif
  235. #define NSIntegerMax LONG_MAX
  236. #define NSIntegerMin LONG_MIN
  237. #define NSUIntegerMax ULONG_MAX
  238. #define NSINTEGER_DEFINED 1
  239. #endif // NSINTEGER_DEFINED
  240. // CGFloat
  241. #ifndef CGFLOAT_DEFINED
  242. #if defined(__LP64__) && __LP64__
  243. // This really is an untested path (64bit on Tiger?)
  244. typedef double CGFloat;
  245. #define CGFLOAT_MIN DBL_MIN
  246. #define CGFLOAT_MAX DBL_MAX
  247. #define CGFLOAT_IS_DOUBLE 1
  248. #else /* !defined(__LP64__) || !__LP64__ */
  249. typedef float CGFloat;
  250. #define CGFLOAT_MIN FLT_MIN
  251. #define CGFLOAT_MAX FLT_MAX
  252. #define CGFLOAT_IS_DOUBLE 0
  253. #endif /* !defined(__LP64__) || !__LP64__ */
  254. #define CGFLOAT_DEFINED 1
  255. #endif // CGFLOAT_DEFINED
  256. #endif // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  257. // Some support for advanced clang static analysis functionality
  258. // See http://clang-analyzer.llvm.org/annotations.html
  259. #ifndef __has_feature // Optional.
  260. #define __has_feature(x) 0 // Compatibility with non-clang compilers.
  261. #endif
  262. #ifndef NS_RETURNS_RETAINED
  263. #if __has_feature(attribute_ns_returns_retained)
  264. #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
  265. #else
  266. #define NS_RETURNS_RETAINED
  267. #endif
  268. #endif
  269. #ifndef NS_RETURNS_NOT_RETAINED
  270. #if __has_feature(attribute_ns_returns_not_retained)
  271. #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
  272. #else
  273. #define NS_RETURNS_NOT_RETAINED
  274. #endif
  275. #endif
  276. #ifndef CF_RETURNS_RETAINED
  277. #if __has_feature(attribute_cf_returns_retained)
  278. #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
  279. #else
  280. #define CF_RETURNS_RETAINED
  281. #endif
  282. #endif
  283. #ifndef CF_RETURNS_NOT_RETAINED
  284. #if __has_feature(attribute_cf_returns_not_retained)
  285. #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
  286. #else
  287. #define CF_RETURNS_NOT_RETAINED
  288. #endif
  289. #endif
  290. // Defined on 10.6 and above.
  291. #ifndef NS_FORMAT_ARGUMENT
  292. #define NS_FORMAT_ARGUMENT(A)
  293. #endif
  294. // Defined on 10.6 and above.
  295. #ifndef NS_FORMAT_FUNCTION
  296. #define NS_FORMAT_FUNCTION(F,A)
  297. #endif
  298. #ifndef GTM_NONNULL
  299. #define GTM_NONNULL(x) __attribute__((nonnull(x)))
  300. #endif
  301. // To simplify support for both Leopard and Snow Leopard we declare
  302. // the Snow Leopard protocols that we need here.
  303. #if !defined(GTM_10_6_PROTOCOLS_DEFINED) && !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
  304. #define GTM_10_6_PROTOCOLS_DEFINED 1
  305. @protocol NSConnectionDelegate
  306. @end
  307. @protocol NSAnimationDelegate
  308. @end
  309. @protocol NSImageDelegate
  310. @end
  311. @protocol NSTabViewDelegate
  312. @end
  313. #endif // !defined(GTM_10_6_PROTOCOLS_DEFINED) && !(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)