Oolite
Loading...
Searching...
No Matches
OOCocoa.h
Go to the documentation of this file.
1/*
2
3OOCocoa.h
4
5Import OpenStep main headers and define some Macisms and other compatibility
6stuff.
7
8Oolite
9Copyright (C) 2004-2013 Giles C Williams and contributors
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24MA 02110-1301, USA.
25
26*/
27
28/*
29 Expressions like #define FOO (1 && !defined(NDEBUG)) are formally invalid,
30 causing a warning in Clang. This lets us write #define FOO (1 && OOLITE_DEBUG)
31 instead.
32 */
33#ifdef NDEBUG
34#define OOLITE_DEBUG 0
35#else
36#define OOLITE_DEBUG 1
37#endif
38
39#if !OOLITE_DEBUG
40#define NS_BLOCK_ASSERTIONS 1
41#endif
42
43
44#include <math.h>
45#include <stdbool.h>
46#import <Foundation/Foundation.h>
47
48#ifdef GNUSTEP_BASE_LIBRARY
49 #define OOLITE_GNUSTEP 1
50
51 #if (GNUSTEP_BASE_MAJOR_VERSION == 1 && GNUSTEP_BASE_MINOR_VERSION < 28)
52 #error Oolite cannot be built using GNUstep earlier than 1.28.
53 #endif
54
55 #ifndef NSIntegerMax
56 // Missing in GNUstep-base prior to 1.23.
57 #define NSIntegerMax INTPTR_MAX
58 #define NSIntegerMin INTPTR_MIN
59 #define NSUIntegerMax UINTPTR_MAX
60 #endif
61
62#else
63 #import <AppKit/AppKit.h>
64
65 #define OOLITE_MAC_OS_X 1
66 #define OOLITE_SPEECH_SYNTH 1
67
68 #if __LP64__
69 #define OOLITE_64_BIT 1
70 #endif
71
72 /* Useful macro copied from GNUstep.
73 */
74 #ifndef DESTROY
75 #define DESTROY(x) do { id x_ = x; x = nil; [x_ release]; } while (0)
76 #endif
77
78 #if defined MAC_OS_X_VERSION_10_7 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
79 #define OOLITE_MAC_OS_X_10_7 1
80 #endif
81
82 #if defined MAC_OS_X_VERSION_10_8 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_8
83 #define OOLITE_MAC_OS_X_10_8 1
84 #endif
85
86 #ifndef MAC_OS_X_VERSION_10_12
87 typedef NSUInteger NSWindowStyleMask;
88 #endif
89#endif
90
91
92#ifndef OOLITE_MAC_OS_X_10_7
93 #define OOLITE_MAC_OS_X_10_7 0
94#endif
95
96#ifndef OOLITE_MAC_OS_X_10_8
97 #define OOLITE_MAC_OS_X_10_8 0
98#endif
99
100
101#ifdef __clang__
102#define OOLITE_HAVE_CLANG 1
103#else
104#define OOLITE_HAVE_CLANG 0
105#endif
106
107
108#if defined(__GNUC__) && !OOLITE_HAVE_CLANG
109// GCC version; for instance, 40300 for 4.3.0. Deliberately undefined in Clang (which defines fake __GNUC__ macros for compatibility).
110#define OOLITE_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
111#endif
112
113
114#if OOLITE_GNUSTEP
115#include <stdint.h>
116#include <limits.h> // to get UINT_MAX
117
118
119#define OOLITE_SDL 1
120
121#ifdef WIN32
122 #define OOLITE_WINDOWS 1
123 #if defined(_WIN64)
124 #define OOLITE_64_BIT 1
125 #endif
126#endif
127
128#ifdef LINUX
129#define OOLITE_LINUX 1
130#endif
131
132
133#define true 1
134#define false 0
135
136#if !defined(MAX)
137 #define MAX(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a > __b ? __a : __b; })
138#endif
139
140#if !defined(MIN)
141 #define MIN(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
142#endif
143
144#ifdef HAVE_LIBESPEAK
145 #define OOLITE_SPEECH_SYNTH 1
146 #define OOLITE_ESPEAK 1
147#endif
148
149
150// Pseudo-keywords used for AppKit UI bindings.
151#ifndef IBOutlet
152#define IBOutlet
153#endif
154#ifndef IBAction
155#define IBAction void
156#endif
157
158
159#import "Comparison.h"
160
161/* Define AppKit constants for events */
162enum {
163 NSUpArrowFunctionKey = 0xF700,
164 NSDownArrowFunctionKey = 0xF701,
165 NSLeftArrowFunctionKey = 0xF702,
166 NSRightArrowFunctionKey = 0xF703,
167 NSF1FunctionKey = 0xF704,
168 NSF2FunctionKey = 0xF705,
169 NSF3FunctionKey = 0xF706,
170 NSF4FunctionKey = 0xF707,
171 NSF5FunctionKey = 0xF708,
172 NSF6FunctionKey = 0xF709,
173 NSF7FunctionKey = 0xF70A,
174 NSF8FunctionKey = 0xF70B,
175 NSF9FunctionKey = 0xF70C,
176 NSF10FunctionKey = 0xF70D,
177 NSF11FunctionKey = 0xF70E,
178 NSF12FunctionKey = 0xF70F,
179 NSF13FunctionKey = 0xF710,
180 NSF14FunctionKey = 0xF711,
181 NSF15FunctionKey = 0xF712,
182 NSF16FunctionKey = 0xF713,
183 NSF17FunctionKey = 0xF714,
184 NSF18FunctionKey = 0xF715,
185 NSF19FunctionKey = 0xF716,
186 NSF20FunctionKey = 0xF717,
187 NSF21FunctionKey = 0xF718,
188 NSF22FunctionKey = 0xF719,
189 NSF23FunctionKey = 0xF71A,
190 NSF24FunctionKey = 0xF71B,
191 NSF25FunctionKey = 0xF71C,
192 NSF26FunctionKey = 0xF71D,
193 NSF27FunctionKey = 0xF71E,
194 NSF28FunctionKey = 0xF71F,
195 NSF29FunctionKey = 0xF720,
196 NSF30FunctionKey = 0xF721,
197 NSF31FunctionKey = 0xF722,
198 NSF32FunctionKey = 0xF723,
199 NSF33FunctionKey = 0xF724,
200 NSF34FunctionKey = 0xF725,
201 NSF35FunctionKey = 0xF726,
202 NSInsertFunctionKey = 0xF727,
203 NSDeleteFunctionKey = 0xF728,
204 NSHomeFunctionKey = 0xF729,
205 NSBeginFunctionKey = 0xF72A,
206 NSEndFunctionKey = 0xF72B,
207 NSPageUpFunctionKey = 0xF72C,
208 NSPageDownFunctionKey = 0xF72D,
209 NSPrintScreenFunctionKey = 0xF72E,
210 NSScrollLockFunctionKey = 0xF72F,
211 NSPauseFunctionKey = 0xF730,
212 NSSysReqFunctionKey = 0xF731,
213 NSBreakFunctionKey = 0xF732,
214 NSResetFunctionKey = 0xF733,
215 NSStopFunctionKey = 0xF734,
216 NSMenuFunctionKey = 0xF735,
217 NSUserFunctionKey = 0xF736,
218 NSSystemFunctionKey = 0xF737,
219 NSPrintFunctionKey = 0xF738,
220 NSClearLineFunctionKey = 0xF739,
221 NSClearDisplayFunctionKey = 0xF73A,
222 NSInsertLineFunctionKey = 0xF73B,
223 NSDeleteLineFunctionKey = 0xF73C,
224 NSInsertCharFunctionKey = 0xF73D,
225 NSDeleteCharFunctionKey = 0xF73E,
226 NSPrevFunctionKey = 0xF73F,
227 NSNextFunctionKey = 0xF740,
228 NSSelectFunctionKey = 0xF741,
229 NSExecuteFunctionKey = 0xF742,
230 NSUndoFunctionKey = 0xF743,
231 NSRedoFunctionKey = 0xF744,
232 NSFindFunctionKey = 0xF745,
233 NSHelpFunctionKey = 0xF746,
234 NSModeSwitchFunctionKey = 0xF747
235};
236
237#endif
238
239
240#ifndef OOLITE_GNUSTEP
241#define OOLITE_GNUSTEP 0
242#endif
243
244#ifndef OOLITE_MAC_OS_X
245#define OOLITE_MAC_OS_X 0
246#endif
247
248#ifndef OOLITE_WINDOWS
249#define OOLITE_WINDOWS 0
250#endif
251
252#ifndef OOLITE_LINUX
253#define OOLITE_LINUX 0
254#endif
255
256#ifndef OOLITE_SDL
257#define OOLITE_SDL 0
258#endif
259
260#ifndef OOLITE_SPEECH_SYNTH
261#define OOLITE_SPEECH_SYNTH 0
262#endif
263
264#ifndef OOLITE_ESPEAK
265#define OOLITE_ESPEAK 0
266#endif
267
268#ifndef OOLITE_64_BIT
269 #define OOLITE_64_BIT 0
270#endif
271
272
273#define OOLITE_PROPERTY_SYNTAX (OOLITE_MAC_OS_X || OOLITE_HAVE_CLANG)
274
275
276#import "OOLogging.h"
277
278
279@interface NSObject (OODescriptionComponents)
280
281/* In order to allow implementations of -description to inherit description
282 components from superclasses, and to allow implementations of -description
283 and -oo_jsDescription to share code, both are implemented as wrappers
284 around -descriptionComponents. -descriptionComponents should provide
285 information about an object without a class name or surrounding
286 punctuation. -description will wrap the components like this:
287 <ClassName 0xnnnnnnnn>{descriptionComponents}
288 and -oo_jsDescription will wrap them like this:
289 [oo_jsClassName descriptionComponents]
290*/
291- (NSString *)descriptionComponents;
292
293
294/* A lot of Oolite's -description implementations are rather long, and many
295 embed other descriptions. -shortDescription provides a truncated
296 alternative, while -shortDescriptionComponents provides a
297 -descriptionComponents-like mechanism to simplify implementation.
298*/
299- (NSString *) shortDescription;
300- (NSString *) shortDescriptionComponents;
301
302@end
303
304
305#if OOLITE_MAC_OS_X
306 #define OOLITE_RELEASE_PLIST_ERROR_STRINGS 1
307#else
308 #define OOLITE_RELEASE_PLIST_ERROR_STRINGS 0
309#endif
310
311
312/* For some reason, return types for some comparison callbacks are typed
313 NSInteger/int under OS X but (more sensibly) NSComparisonResult under
314 GNUstep.
315*/
316#if OOLITE_MAC_OS_X
317 typedef NSInteger OOComparisonResult;
318#else
319 typedef NSComparisonResult OOComparisonResult;
320#endif
321
322
323/* Fast enumeration (for (x in y) syntax) is supported in all Mac compilers
324 when targeting 10.5 or later, and in gcc 4.6 with the GNU libobjc runtime.
325 At the time of writing, GNUstep stable does not support gcc 4.6, but it
326 already has support for the fast enumeration protocol in its collection
327 classes.
328
329 All release versions of clang support fast enumeration, assuming libobjc2
330 or ObjectiveC2.framework is being used. We shall make that assumption.
331
332 References:
333 http://lists.gnu.org/archive/html/discuss-gnustep/2011-02/msg00019.html
334 http://wiki.gnustep.org/index.php/ObjC2_FAQ
335 -- Ahruman 2011-02-04
336*/
337
338/* Enumeration macros:
339 foreach(VAR, COLLECTION) enumerates the members of an array or set, setting
340 the variable VAR to a member on each pass.
341 foreachkey(VAR, DICT) enumerates the keys of a dictionary the same way.
342
343 Example:
344 id element = nil;
345 foreach (element, array)
346 {
347 OOLog(@"element", @"%@", element);
348 }
349
350 These are based on macros by Jens Alfke.
351*/
352#define foreach(VAR, COLLECTION) for(VAR in COLLECTION)
353#define foreachkey(VAR, DICT) for(VAR in DICT)
354
355
356/* Support for foreach() with NSEnumerators in GCC.
357 It works without this with for (x in y) support, but we leave it defined
358 to reduce differences between different build environments.
359*/
360@interface NSEnumerator (OOForEachSupport)
361- (NSEnumerator *) objectEnumerator;
362@end
363
364
365/* @optional directive for protocols: added in Objective-C 2.0.
366
367 As a nasty, nasty hack, the OOLITE_OPTIONAL(foo) macro allows an optional
368 section with or without @optional. If @optional is not available, it
369 actually ends the protocol and starts an appropriately-named informal
370 protocol, i.e. a category on NSObject. Since it ends the protocol, there
371 can only be one and there's no way to switch back to @required.
372*/
373#ifndef OOLITE_HAVE_PROTOCOL_OPTIONAL
374#define OOLITE_HAVE_PROTOCOL_OPTIONAL (OOLITE_MAC_OS_X || OOLITE_HAVE_CLANG || OOLITE_GCC_VERSION >= 40700)
375#endif
376
377#if OOLITE_HAVE_PROTOCOL_OPTIONAL
378#define OOLITE_OPTIONAL(protocolName) @optional
379#else
380#define OOLITE_OPTIONAL(protocolName) @end @interface NSObject (protocolName ## Optional)
381#endif
382
383
384/* instancetype contextual keyword; added in Clang 3.0ish.
385
386 Pseudo-type indicating that the return value of an instance method is an
387 instance of the same class as the receiver, or for a class mothod, is an
388 instance of that class.
389
390 For example, given:
391 @interface Foo: NSObject
392 + (instancetype) fooWithProperty:(id)property;
393 @end
394
395 @interface Bar: Foo
396 @end
397
398 the type of [Bar fooWithProperty] is inferred to be Bar *.
399
400 Clang treats methods of type id as instancetype when their names begin with
401 +alloc, +new, -init, -autorelease, -retain, or -self.
402
403 For compilers without instancetype support, id is appropriate but less
404 type-safe.
405
406 NOTE: it is not appropriate to use instancetype for a factory method which
407 chooses which publicly-visible subclass to instantiate based on parameters.
408 For instance, calling one of the OOMaterial convenience factory methods on
409 OOShaderMaterial might return an OOSingleTextureMaterial, so the correct
410 return type is either OOMaterial or id.
411 On the other hand, it is appropriate on factory methods which just wrap
412 the corresponding -init and/or -init + configuration through properties.
413 (Such factory methods should be implemented in terms of [[self alloc]
414 init...].)
415*/
416#if __OBJC__ && !__has_feature(objc_instancetype)
417typedef id instancetype;
418#endif
419
420
421#ifndef OO_DEBUG
422// Defined by makefile/Xcode in debug builds.
423#define OO_DEBUG 0
424#endif
425
426#if OOLITE_WINDOWS
427#ifndef OO_GAME_DATA_TO_USER_FOLDER
428#define OO_GAME_DATA_TO_USER_FOLDER 0
429#endif
430#endif
NSUInteger NSWindowStyleMask
Definition OOCocoa.h:87
NSInteger OOComparisonResult
Definition OOCocoa.h:317
NSString * shortDescriptionComponents()
Definition OOCocoa.m:74