GTMDefines.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // Not all MAC_OS_X_VERSION_10_X macros defined in past SDKs
  21. #ifndef MAC_OS_X_VERSION_10_5
  22. #define MAC_OS_X_VERSION_10_5 1050
  23. #endif
  24. #ifndef MAC_OS_X_VERSION_10_6
  25. #define MAC_OS_X_VERSION_10_6 1060
  26. #endif
  27. // ----------------------------------------------------------------------------
  28. // CPP symbols that can be overridden in a prefix to control how the toolbox
  29. // is compiled.
  30. // ----------------------------------------------------------------------------
  31. // GTMHTTPFetcher will support logging by default but only hook its input
  32. // stream support for logging when requested. You can control the inclusion of
  33. // the code by providing your own definitions for these w/in a prefix header.
  34. //
  35. #ifndef GTM_HTTPFETCHER_ENABLE_LOGGING
  36. #define GTM_HTTPFETCHER_ENABLE_LOGGING 1
  37. #endif // GTM_HTTPFETCHER_ENABLE_LOGGING
  38. #ifndef GTM_HTTPFETCHER_ENABLE_INPUTSTREAM_LOGGING
  39. #define GTM_HTTPFETCHER_ENABLE_INPUTSTREAM_LOGGING 0
  40. #endif // GTM_HTTPFETCHER_ENABLE_INPUTSTREAM_LOGGING
  41. // By setting the GTM_CONTAINERS_VALIDATION_FAILED_LOG and
  42. // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT macros you can control what happens
  43. // when a validation fails. If you implement your own validators, you may want
  44. // to control their internals using the same macros for consistency.
  45. #ifndef GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
  46. #define GTM_CONTAINERS_VALIDATION_FAILED_ASSERT 0
  47. #endif
  48. // Give ourselves a consistent way to do inlines. Apple's macros even use
  49. // a few different actual definitions, so we're based off of the foundation
  50. // one.
  51. #if !defined(GTM_INLINE)
  52. #if defined (__GNUC__) && (__GNUC__ == 4)
  53. #define GTM_INLINE static __inline__ __attribute__((always_inline))
  54. #else
  55. #define GTM_INLINE static __inline__
  56. #endif
  57. #endif
  58. // Give ourselves a consistent way of doing externs that links up nicely
  59. // when mixing objc and objc++
  60. #if !defined (GTM_EXTERN)
  61. #if defined __cplusplus
  62. #define GTM_EXTERN extern "C"
  63. #else
  64. #define GTM_EXTERN extern
  65. #endif
  66. #endif
  67. // Give ourselves a consistent way of exporting things if we have visibility
  68. // set to hidden.
  69. #if !defined (GTM_EXPORT)
  70. #define GTM_EXPORT __attribute__((visibility("default")))
  71. #endif
  72. // _GTMDevLog & _GTMDevAssert
  73. //
  74. // _GTMDevLog & _GTMDevAssert are meant to be a very lightweight shell for
  75. // developer level errors. This implementation simply macros to NSLog/NSAssert.
  76. // It is not intended to be a general logging/reporting system.
  77. //
  78. // Please see http://code.google.com/p/google-toolbox-for-mac/wiki/DevLogNAssert
  79. // for a little more background on the usage of these macros.
  80. //
  81. // _GTMDevLog log some error/problem in debug builds
  82. // _GTMDevAssert assert if conditon isn't met w/in a method/function
  83. // in all builds.
  84. //
  85. // To replace this system, just provide different macro definitions in your
  86. // prefix header. Remember, any implementation you provide *must* be thread
  87. // safe since this could be called by anything in what ever situtation it has
  88. // been placed in.
  89. //
  90. // We only define the simple macros if nothing else has defined this.
  91. #ifndef _GTMDevLog
  92. #ifdef DEBUG
  93. #define _GTMDevLog(...) NSLog(__VA_ARGS__)
  94. #else
  95. #define _GTMDevLog(...) do { } while (0)
  96. #endif
  97. #endif // _GTMDevLog
  98. // Declared here so that it can easily be used for logging tracking if
  99. // necessary. See GTMUnitTestDevLog.h for details.
  100. @class NSString;
  101. GTM_EXTERN void _GTMUnitTestDevLog(NSString *format, ...);
  102. #ifndef _GTMDevAssert
  103. // we directly invoke the NSAssert handler so we can pass on the varargs
  104. // (NSAssert doesn't have a macro we can use that takes varargs)
  105. #if !defined(NS_BLOCK_ASSERTIONS)
  106. #define _GTMDevAssert(condition, ...) \
  107. do { \
  108. if (!(condition)) { \
  109. [[NSAssertionHandler currentHandler] \
  110. handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
  111. file:[NSString stringWithUTF8String:__FILE__] \
  112. lineNumber:__LINE__ \
  113. description:__VA_ARGS__]; \
  114. } \
  115. } while(0)
  116. #else // !defined(NS_BLOCK_ASSERTIONS)
  117. #define _GTMDevAssert(condition, ...) do { } while (0)
  118. #endif // !defined(NS_BLOCK_ASSERTIONS)
  119. #endif // _GTMDevAssert
  120. // _GTMCompileAssert
  121. // _GTMCompileAssert is an assert that is meant to fire at compile time if you
  122. // want to check things at compile instead of runtime. For example if you
  123. // want to check that a wchar is 4 bytes instead of 2 you would use
  124. // _GTMCompileAssert(sizeof(wchar_t) == 4, wchar_t_is_4_bytes_on_OS_X)
  125. // Note that the second "arg" is not in quotes, and must be a valid processor
  126. // symbol in it's own right (no spaces, punctuation etc).
  127. // Wrapping this in an #ifndef allows external groups to define their own
  128. // compile time assert scheme.
  129. #ifndef _GTMCompileAssert
  130. // We got this technique from here:
  131. // http://unixjunkie.blogspot.com/2007/10/better-compile-time-asserts_29.html
  132. #define _GTMCompileAssertSymbolInner(line, msg) _GTMCOMPILEASSERT ## line ## __ ## msg
  133. #define _GTMCompileAssertSymbol(line, msg) _GTMCompileAssertSymbolInner(line, msg)
  134. #define _GTMCompileAssert(test, msg) \
  135. typedef char _GTMCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  136. #endif // _GTMCompileAssert
  137. // Macro to allow fast enumeration when building for 10.5 or later, and
  138. // reliance on NSEnumerator for 10.4. Remember, NSDictionary w/ FastEnumeration
  139. // does keys, so pick the right thing, nothing is done on the FastEnumeration
  140. // side to be sure you're getting what you wanted.
  141. #ifndef GTM_FOREACH_OBJECT
  142. #if defined(TARGET_OS_IPHONE) || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
  143. #define GTM_FOREACH_OBJECT(element, collection) \
  144. for (element in collection)
  145. #define GTM_FOREACH_KEY(element, collection) \
  146. for (element in collection)
  147. #else
  148. #define GTM_FOREACH_OBJECT(element, collection) \
  149. for (NSEnumerator * _ ## element ## _enum = [collection objectEnumerator]; \
  150. (element = [_ ## element ## _enum nextObject]) != nil; )
  151. #define GTM_FOREACH_KEY(element, collection) \
  152. for (NSEnumerator * _ ## element ## _enum = [collection keyEnumerator]; \
  153. (element = [_ ## element ## _enum nextObject]) != nil; )
  154. #endif
  155. #endif
  156. // ============================================================================
  157. // ----------------------------------------------------------------------------
  158. // CPP symbols defined based on the project settings so the GTM code has
  159. // simple things to test against w/o scattering the knowledge of project
  160. // setting through all the code.
  161. // ----------------------------------------------------------------------------
  162. // Provide a single constant CPP symbol that all of GTM uses for ifdefing
  163. // iPhone code.
  164. #include <TargetConditionals.h>
  165. #if TARGET_OS_IPHONE // iPhone SDK
  166. // For iPhone specific stuff
  167. #define GTM_IPHONE_SDK 1
  168. #if TARGET_IPHONE_SIMULATOR
  169. #define GTM_IPHONE_SIMULATOR 1
  170. #else
  171. #define GTM_IPHONE_DEVICE 1
  172. #endif // TARGET_IPHONE_SIMULATOR
  173. #else
  174. // For MacOS specific stuff
  175. #define GTM_MACOS_SDK 1
  176. #endif
  177. // Provide a symbol to include/exclude extra code for GC support. (This mainly
  178. // just controls the inclusion of finalize methods).
  179. #ifndef GTM_SUPPORT_GC
  180. #if GTM_IPHONE_SDK
  181. // iPhone never needs GC
  182. #define GTM_SUPPORT_GC 0
  183. #else
  184. // We can't find a symbol to tell if GC is supported/required, so best we
  185. // do on Mac targets is include it if we're on 10.5 or later.
  186. #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
  187. #define GTM_SUPPORT_GC 0
  188. #else
  189. #define GTM_SUPPORT_GC 1
  190. #endif
  191. #endif
  192. #endif
  193. // To simplify support for 64bit (and Leopard in general), we provide the type
  194. // defines for non Leopard SDKs
  195. #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
  196. // NSInteger/NSUInteger and Max/Mins
  197. #ifndef NSINTEGER_DEFINED
  198. #if __LP64__ || NS_BUILD_32_LIKE_64
  199. typedef long NSInteger;
  200. typedef unsigned long NSUInteger;
  201. #else
  202. typedef int NSInteger;
  203. typedef unsigned int NSUInteger;
  204. #endif
  205. #define NSIntegerMax LONG_MAX
  206. #define NSIntegerMin LONG_MIN
  207. #define NSUIntegerMax ULONG_MAX
  208. #define NSINTEGER_DEFINED 1
  209. #endif // NSINTEGER_DEFINED
  210. // CGFloat
  211. #ifndef CGFLOAT_DEFINED
  212. #if defined(__LP64__) && __LP64__
  213. // This really is an untested path (64bit on Tiger?)
  214. typedef double CGFloat;
  215. #define CGFLOAT_MIN DBL_MIN
  216. #define CGFLOAT_MAX DBL_MAX
  217. #define CGFLOAT_IS_DOUBLE 1
  218. #else /* !defined(__LP64__) || !__LP64__ */
  219. typedef float CGFloat;
  220. #define CGFLOAT_MIN FLT_MIN
  221. #define CGFLOAT_MAX FLT_MAX
  222. #define CGFLOAT_IS_DOUBLE 0
  223. #endif /* !defined(__LP64__) || !__LP64__ */
  224. #define CGFLOAT_DEFINED 1
  225. #endif // CGFLOAT_DEFINED
  226. #endif // MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4