Oolite
Loading...
Searching...
No Matches
OOFoundation.h
Go to the documentation of this file.
1/*
2
3OOFoundation.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
34#pragma once
35
36#import <Foundation/Foundation.h>
37#include <math.h>
38#include <stdbool.h>
39
40#ifdef NDEBUG
41#define OOLITE_DEBUG 0
42#else
43#define OOLITE_DEBUG 1
44#endif
45
46#if !OOLITE_DEBUG
47#define NS_BLOCK_ASSERTIONS 1
48#endif
49
50#ifdef GNUSTEP_BASE_LIBRARY
51#define OOLITE_GNUSTEP 1
52
53#if (GNUSTEP_BASE_MAJOR_VERSION == 1 && GNUSTEP_BASE_MINOR_VERSION < 28)
54#error Oolite cannot be built using GNUstep earlier than 1.28.
55#endif
56#else
57#define OOLITE_MAC_OS_X 1
58
59/* Useful macro copied from GNUstep.
60 */
61#ifndef DESTROY
62#define DESTROY(x) \
63 do { \
64 id x_ = x; \
65 x = nil; \
66 [x_ release]; \
67 } while (0)
68#endif
69#endif
70
71#ifdef __clang__
72#define OOLITE_HAVE_CLANG 1
73#else
74#define OOLITE_HAVE_CLANG 0
75#endif
76
77#if defined(__GNUC__) && !OOLITE_HAVE_CLANG
78// GCC version; for instance, 40300 for 4.3.0. Deliberately undefined in Clang (which defines fake __GNUC__ macros for compatibility).
79#define OOLITE_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
80#endif
81
82#if OOLITE_MAC_OS_X && defined(OOLITE_SDL_MAC)
83/* macOS build using Apple Foundation with the SDL front end (the Cocoa
84 front end no longer exists). OOLITE_MAC_OS_X selects Apple Foundation and
85 OS-level behaviour; UI-level code takes the OOLITE_SDL path.
86 */
87#define OOLITE_SDL 1
88#endif
89
90#if OOLITE_GNUSTEP || OOLITE_SDL
91#include <limits.h> // to get UINT_MAX
92#include <stdint.h>
93
94#define OOLITE_SDL 1
95
96#ifdef WIN32
97#define OOLITE_WINDOWS 1
98#if defined(_WIN64)
99#define OOLITE_64_BIT 1
100#endif
101#endif
102
103#ifdef LINUX
104#define OOLITE_LINUX 1
105#endif
106
107#if OOLITE_GNUSTEP
108#define true 1
109#define false 0
110#endif
111
112#if !defined(MAX)
113#define MAX(A, B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a > __b ? __a : __b; })
114#endif
115
116#if !defined(MIN)
117#define MIN(A, B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
118#endif
119
120#ifdef HAVE_LIBESPEAK
121#define OOLITE_SPEECH_SYNTH 1
122#define OOLITE_ESPEAK 1
123#endif
124
125// Pseudo-keywords used for AppKit UI bindings.
126#ifndef IBOutlet
127#define IBOutlet /**/
128#endif
129#ifndef IBAction
130#define IBAction void
131#endif
132
133#import "Comparison.h"
134
135/* Define AppKit constants for events */
136enum {
137 NSUpArrowFunctionKey = 0xF700,
138 NSDownArrowFunctionKey = 0xF701,
139 NSLeftArrowFunctionKey = 0xF702,
140 NSRightArrowFunctionKey = 0xF703,
141 NSF1FunctionKey = 0xF704,
142 NSF2FunctionKey = 0xF705,
143 NSF3FunctionKey = 0xF706,
144 NSF4FunctionKey = 0xF707,
145 NSF5FunctionKey = 0xF708,
146 NSF6FunctionKey = 0xF709,
147 NSF7FunctionKey = 0xF70A,
148 NSF8FunctionKey = 0xF70B,
149 NSF9FunctionKey = 0xF70C,
150 NSF10FunctionKey = 0xF70D,
151 NSF11FunctionKey = 0xF70E,
152 NSF12FunctionKey = 0xF70F,
153 NSF13FunctionKey = 0xF710,
154 NSF14FunctionKey = 0xF711,
155 NSF15FunctionKey = 0xF712,
156 NSF16FunctionKey = 0xF713,
157 NSF17FunctionKey = 0xF714,
158 NSF18FunctionKey = 0xF715,
159 NSF19FunctionKey = 0xF716,
160 NSF20FunctionKey = 0xF717,
161 NSF21FunctionKey = 0xF718,
162 NSF22FunctionKey = 0xF719,
163 NSF23FunctionKey = 0xF71A,
164 NSF24FunctionKey = 0xF71B,
165 NSF25FunctionKey = 0xF71C,
166 NSF26FunctionKey = 0xF71D,
167 NSF27FunctionKey = 0xF71E,
168 NSF28FunctionKey = 0xF71F,
169 NSF29FunctionKey = 0xF720,
170 NSF30FunctionKey = 0xF721,
171 NSF31FunctionKey = 0xF722,
172 NSF32FunctionKey = 0xF723,
173 NSF33FunctionKey = 0xF724,
174 NSF34FunctionKey = 0xF725,
175 NSF35FunctionKey = 0xF726,
176 NSInsertFunctionKey = 0xF727,
177 NSDeleteFunctionKey = 0xF728,
178 NSHomeFunctionKey = 0xF729,
179 NSBeginFunctionKey = 0xF72A,
180 NSEndFunctionKey = 0xF72B,
181 NSPageUpFunctionKey = 0xF72C,
182 NSPageDownFunctionKey = 0xF72D,
183 NSPrintScreenFunctionKey = 0xF72E,
184 NSScrollLockFunctionKey = 0xF72F,
185 NSPauseFunctionKey = 0xF730,
186 NSSysReqFunctionKey = 0xF731,
187 NSBreakFunctionKey = 0xF732,
188 NSResetFunctionKey = 0xF733,
189 NSStopFunctionKey = 0xF734,
190 NSMenuFunctionKey = 0xF735,
191 NSUserFunctionKey = 0xF736,
192 NSSystemFunctionKey = 0xF737,
193 NSPrintFunctionKey = 0xF738,
194 NSClearLineFunctionKey = 0xF739,
195 NSClearDisplayFunctionKey = 0xF73A,
196 NSInsertLineFunctionKey = 0xF73B,
197 NSDeleteLineFunctionKey = 0xF73C,
198 NSInsertCharFunctionKey = 0xF73D,
199 NSDeleteCharFunctionKey = 0xF73E,
200 NSPrevFunctionKey = 0xF73F,
201 NSNextFunctionKey = 0xF740,
202 NSSelectFunctionKey = 0xF741,
203 NSExecuteFunctionKey = 0xF742,
204 NSUndoFunctionKey = 0xF743,
205 NSRedoFunctionKey = 0xF744,
206 NSFindFunctionKey = 0xF745,
207 NSHelpFunctionKey = 0xF746,
208 NSModeSwitchFunctionKey = 0xF747
209};
210
211#endif
212
213#ifndef OOLITE_GNUSTEP
214#define OOLITE_GNUSTEP 0
215#endif
216
217#ifndef OOLITE_MAC_OS_X
218#define OOLITE_MAC_OS_X 0
219#endif
220
221#ifndef OOLITE_WINDOWS
222#define OOLITE_WINDOWS 0
223#endif
224
225#ifndef OOLITE_LINUX
226#define OOLITE_LINUX 0
227#endif
228
229#ifndef OOLITE_SDL
230#define OOLITE_SDL 0
231#endif
232
233// The Cocoa (AppKit) front end, as opposed to Apple Foundation with SDL.
234#define OOLITE_MAC_COCOA_UI (OOLITE_MAC_OS_X && !OOLITE_SDL)
235
236#ifndef OOLITE_SPEECH_SYNTH
237#define OOLITE_SPEECH_SYNTH 0
238#endif
239
240#ifndef OOLITE_ESPEAK
241#define OOLITE_ESPEAK 0
242#endif
243
244#ifndef OOLITE_64_BIT
245#define OOLITE_64_BIT 0
246#endif
247
248#define OOLITE_PROPERTY_SYNTAX (OOLITE_MAC_OS_X || OOLITE_HAVE_CLANG)
249
250#import "OOLogging.h"
251
252@interface NSObject (OODescriptionComponents)
253
254/* In order to allow implementations of -description to inherit description
255 components from superclasses, and to allow implementations of -description
256 and -oo_jsDescription to share code, both are implemented as wrappers
257 around -descriptionComponents. -descriptionComponents should provide
258 information about an object without a class name or surrounding
259 punctuation. -description will wrap the components like this:
260 <ClassName 0xnnnnnnnn>{descriptionComponents}
261 and -oo_jsDescription will wrap them like this:
262 [oo_jsClassName descriptionComponents]
263*/
264- (NSString*)descriptionComponents;
265
266/* A lot of Oolite's -description implementations are rather long, and many
267 embed other descriptions. -shortDescription provides a truncated
268 alternative, while -shortDescriptionComponents provides a
269 -descriptionComponents-like mechanism to simplify implementation.
270*/
271- (NSString*)shortDescription;
272- (NSString*)shortDescriptionComponents;
273
274@end
275
276#if OOLITE_MAC_OS_X
277#define OOLITE_RELEASE_PLIST_ERROR_STRINGS 1
278#else
279#define OOLITE_RELEASE_PLIST_ERROR_STRINGS 0
280#endif
281
282/* For some reason, return types for some comparison callbacks are typed
283 NSInteger/int under OS X but (more sensibly) NSComparisonResult under
284 GNUstep.
285*/
286#if OOLITE_MAC_OS_X
287typedef NSInteger OOComparisonResult;
288#else
289typedef NSComparisonResult OOComparisonResult;
290#endif
291
292/* Fast enumeration (for (x in y) syntax) is supported in all Mac compilers
293 when targeting 10.5 or later, and in gcc 4.6 with the GNU libobjc runtime.
294 At the time of writing, GNUstep stable does not support gcc 4.6, but it
295 already has support for the fast enumeration protocol in its collection
296 classes.
297
298 All release versions of clang support fast enumeration, assuming libobjc2
299 or ObjectiveC2.framework is being used. We shall make that assumption.
300
301 References:
302 http://lists.gnu.org/archive/html/discuss-gnustep/2011-02/msg00019.html
303 http://wiki.gnustep.org/index.php/ObjC2_FAQ
304 -- Ahruman 2011-02-04
305*/
306
307/* Enumeration macros:
308 foreach(VAR, COLLECTION) enumerates the members of an array or set, setting
309 the variable VAR to a member on each pass.
310 foreachkey(VAR, DICT) enumerates the keys of a dictionary the same way.
311
312 Example:
313 id element = nil;
314 foreach (element, array)
315 {
316 OOLog(@"element", @"%@", element);
317 }
318
319 These are based on macros by Jens Alfke.
320*/
321#define foreach(VAR, COLLECTION) for (VAR in COLLECTION)
322#define foreachkey(VAR, DICT) for (VAR in DICT)
323
324/* Support for foreach() with NSEnumerators in GCC.
325 It works without this with for (x in y) support, but we leave it defined
326 to reduce differences between different build environments.
327*/
328@interface NSEnumerator (OOForEachSupport)
329- (NSEnumerator*)objectEnumerator;
330@end
331
332#define OOLITE_OPTIONAL(protocolName) @optional
333
334/* instancetype contextual keyword; added in Clang 3.0ish.
335
336 Pseudo-type indicating that the return value of an instance method is an
337 instance of the same class as the receiver, or for a class mothod, is an
338 instance of that class.
339
340 For example, given:
341 @interface Foo: NSObject
342 + (instancetype) fooWithProperty:(id)property;
343 @end
344
345 @interface Bar: Foo
346 @end
347
348 the type of [Bar fooWithProperty] is inferred to be Bar *.
349
350 Clang treats methods of type id as instancetype when their names begin with
351 +alloc, +new, -init, -autorelease, -retain, or -self.
352
353 For compilers without instancetype support, id is appropriate but less
354 type-safe.
355
356 NOTE: it is not appropriate to use instancetype for a factory method which
357 chooses which publicly-visible subclass to instantiate based on parameters.
358 For instance, calling one of the OOMaterial convenience factory methods on
359 OOShaderMaterial might return an OOSingleTextureMaterial, so the correct
360 return type is either OOMaterial or id.
361 On the other hand, it is appropriate on factory methods which just wrap
362 the corresponding -init and/or -init + configuration through properties.
363 (Such factory methods should be implemented in terms of [[self alloc]
364 init...].)
365*/
366#if __OBJC__ && !__has_feature(objc_instancetype)
367typedef id instancetype;
368#endif
369
370#ifndef OO_DEBUG
371// Defined by makefile/Xcode in debug builds.
372#define OO_DEBUG 0
373#endif
374
375#if OOLITE_WINDOWS
376#ifndef OO_GAME_DATA_TO_USER_FOLDER
377#define OO_GAME_DATA_TO_USER_FOLDER 0
378#endif
379#endif
NSInteger OOComparisonResult