/core/externals/google-toolbox-for-mac/GTMDefines.h

http://macfuse.googlecode.com/ · C++ Header · 456 lines · 293 code · 50 blank · 113 comment · 19 complexity · 0962b1a5b6843140d448bc99a1e7cf41 MD5 · raw file

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