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