Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / stdafx.h
blob83cfb98ae4d2a3add48a2bddbf681eea07afd177
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file stdafx.h Definition of base types and functions in a cross-platform compatible way. */
12 #ifndef STDAFX_H
13 #define STDAFX_H
15 #if defined(__APPLE__)
16 #include "os/macosx/osx_stdafx.h"
17 #endif /* __APPLE__ */
19 #if defined(__BEOS__) || defined(__HAIKU__)
20 #include <SupportDefs.h>
21 #include <unistd.h>
22 #define _GNU_SOURCE
23 #define TROUBLED_INTS
24 #elif defined(__NDS__)
25 #include <nds/jtypes.h>
26 #define TROUBLED_INTS
27 #endif
29 /* It seems that we need to include stdint.h before anything else
30 * We need INT64_MAX, which for most systems comes from stdint.h. However, MSVC
31 * does not have stdint.h and apparently neither does MorphOS.
32 * For OSX the inclusion is already done in osx_stdafx.h. */
33 #if !defined(__APPLE__) && (!defined(_MSC_VER) || _MSC_VER >= 1600) && !defined(__MORPHOS__)
34 #if defined(SUNOS)
35 /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything
36 * stdint.h defines and we need. */
37 #include <inttypes.h>
38 #else
39 #define __STDC_LIMIT_MACROS
40 #include <stdint.h>
41 #endif
42 #endif
44 /* The conditions for these constants to be available are way too messy; so check them one by one */
45 #if !defined(UINT64_MAX)
46 #define UINT64_MAX (18446744073709551615ULL)
47 #endif
48 #if !defined(INT64_MAX)
49 #define INT64_MAX (9223372036854775807LL)
50 #endif
51 #if !defined(INT64_MIN)
52 #define INT64_MIN (-INT64_MAX - 1)
53 #endif
54 #if !defined(UINT32_MAX)
55 #define UINT32_MAX (4294967295U)
56 #endif
57 #if !defined(INT32_MAX)
58 #define INT32_MAX (2147483647)
59 #endif
60 #if !defined(INT32_MIN)
61 #define INT32_MIN (-INT32_MAX - 1)
62 #endif
63 #if !defined(UINT16_MAX)
64 #define UINT16_MAX (65535U)
65 #endif
66 #if !defined(INT16_MAX)
67 #define INT16_MAX (32767)
68 #endif
69 #if !defined(INT16_MIN)
70 #define INT16_MIN (-INT16_MAX - 1)
71 #endif
72 #if !defined(UINT8_MAX)
73 #define UINT8_MAX (255)
74 #endif
75 #if !defined(INT8_MAX)
76 #define INT8_MAX (127)
77 #endif
78 #if !defined(INT8_MIN)
79 #define INT8_MIN (-INT8_MAX - 1)
80 #endif
82 #include <cstdio>
83 #include <cstddef>
84 #include <cstring>
85 #include <cstdlib>
86 #include <climits>
87 #include <cassert>
89 #ifndef SIZE_MAX
90 #define SIZE_MAX ((size_t)-1)
91 #endif
93 #if defined(UNIX) || defined(__MINGW32__)
94 #include <sys/types.h>
95 #endif
97 #if defined(__OS2__)
98 #include <types.h>
99 #define strcasecmp stricmp
100 #endif
102 #if defined(PSP)
103 #include <psptypes.h>
104 #include <pspdebug.h>
105 #include <pspthreadman.h>
106 #endif
108 #if defined(SUNOS) || defined(HPUX)
109 #include <alloca.h>
110 #endif
112 #if defined(__MORPHOS__)
113 /* MorphOS defines certain Amiga defines per default, we undefine them
114 * here to make the rest of source less messy and more clear what is
115 * required for morphos and what for AmigaOS */
116 #if defined(amigaos)
117 #undef amigaos
118 #endif
119 #if defined(__amigaos__)
120 #undef __amigaos__
121 # endif
122 #if defined(__AMIGA__)
123 #undef __AMIGA__
124 #endif
125 #if defined(AMIGA)
126 #undef AMIGA
127 #endif
128 #if defined(amiga)
129 #undef amiga
130 #endif
131 /* Act like we already included this file, as it somehow gives linkage problems
132 * (mismatch linkage of C++ and C between this include and unistd.h). */
133 #define CLIB_USERGROUP_PROTOS_H
134 #endif /* __MORPHOS__ */
136 #if defined(PSP)
137 /* PSP can only have 10 file-descriptors open at any given time, but this
138 * switch only limits reads via the Fio system. So keep 2 fds free for things
139 * like saving a game. */
140 #define LIMITED_FDS 8
141 #define printf pspDebugScreenPrintf
142 #endif /* PSP */
144 /* Stuff for GCC */
145 #if defined(__GNUC__)
146 #define NORETURN __attribute__ ((noreturn))
147 #define CDECL
148 #define __int64 long long
149 #define GCC_PACK __attribute__((packed))
150 /* Warn about functions using 'printf' format syntax. First argument determines which parameter
151 * is the format string, second argument is start of values passed to printf. */
152 #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
153 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
154 #define FINAL final
155 #define OVERRIDE override
156 #define FINAL_OVERRIDE final override
157 #else
158 #define FINAL
159 #define OVERRIDE
160 #define FINAL_OVERRIDE
161 #endif
162 #endif /* __GNUC__ */
164 #if defined(__WATCOMC__)
165 #define NORETURN
166 #define CDECL
167 #define GCC_PACK
168 #define WARN_FORMAT(string, args)
169 #define FINAL
170 #define OVERRIDE
171 #define FINAL_OVERRIDE
172 #include <malloc.h>
173 #endif /* __WATCOMC__ */
175 #if defined(__MINGW32__) || defined(__CYGWIN__)
176 #include <malloc.h> // alloca()
177 #endif
179 #if defined(WIN32)
180 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
181 #endif
183 /* Stuff for MSVC */
184 #if defined(_MSC_VER)
185 #pragma once
186 #ifdef _WIN64
187 /* No 64-bit Windows below XP, so we can safely assume it as the target platform. */
188 #define NTDDI_VERSION NTDDI_WINXP // Windows XP
189 #define _WIN32_WINNT 0x501 // Windows XP
190 #define _WIN32_WINDOWS 0x501 // Windows XP
191 #define WINVER 0x0501 // Windows XP
192 #define _WIN32_IE_ 0x0600 // 6.0 (XP+)
193 #else
194 /* Define a win32 target platform, to override defaults of the SDK
195 * We need to define NTDDI version for Vista SDK, but win2k is minimum */
196 #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
197 #define _WIN32_WINNT 0x0500 // Windows 2000
198 #define _WIN32_WINDOWS 0x400 // Windows 95
199 #if !defined(WINCE)
200 #define WINVER 0x0400 // Windows NT 4.0 / Windows 95
201 #endif
202 #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+)
203 #endif
204 #define NOMINMAX // Disable min/max macros in windows.h.
206 #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
207 #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
208 #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
209 #pragma warning(disable: 4355) // 'this' : used in base member initializer list
211 #if (_MSC_VER < 1400) // MSVC 2005 safety checks
212 #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
213 #endif /* (_MSC_VER < 1400) */
214 #pragma warning(disable: 4291) // no matching operator delete found; memory will not be freed if initialization throws an exception (reason: our overloaded functions never throw an exception)
215 #pragma warning(disable: 4996) // 'function': was declared deprecated
216 #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
217 #pragma warning(disable: 6308) // code analyzer: 'realloc' might return null pointer: assigning null pointer to 't_ptr', which is passed as an argument to 'realloc', will cause the original memory block to be leaked
218 #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
219 #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
220 #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
221 #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
222 #pragma warning(disable: 6246) // code analyzer: Local declaration of 'statspec' hides declaration of the same name in outer scope. For additional information, see previous declaration at ...
224 #include <malloc.h> // alloca()
225 #define NORETURN __declspec(noreturn)
226 #define inline __forceinline
228 #if !defined(WINCE)
229 #define CDECL _cdecl
230 #endif
232 #define GCC_PACK
233 #define WARN_FORMAT(string, args)
234 #define FINAL sealed
235 #define OVERRIDE
236 #define FINAL_OVERRIDE
238 int CDECL snprintf(char *str, size_t size, const char *format, ...) WARN_FORMAT(3, 4);
239 #if defined(WINCE)
240 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
241 #endif
243 #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
244 #if !defined(_W64)
245 #define _W64
246 #endif
248 typedef _W64 int INT_PTR, *PINT_PTR;
249 typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
250 #endif /* WIN32 && !_WIN64 && !WIN64 */
252 #if defined(_WIN64) || defined(WIN64)
253 #define fseek _fseeki64
254 #endif /* _WIN64 || WIN64 */
256 /* This is needed to zlib uses the stdcall calling convention on visual studio */
257 #if defined(WITH_ZLIB) || defined(WITH_PNG)
258 #if !defined(ZLIB_WINAPI)
259 #define ZLIB_WINAPI
260 #endif
261 #endif
263 #if defined(WINCE)
264 #define strcasecmp _stricmp
265 #define strncasecmp _strnicmp
266 #undef DEBUG
267 #else
268 #define strcasecmp stricmp
269 #define strncasecmp strnicmp
270 #endif
272 /* MSVC doesn't have these :( */
273 #define S_ISDIR(mode) (mode & S_IFDIR)
274 #define S_ISREG(mode) (mode & S_IFREG)
276 #endif /* defined(_MSC_VER) */
278 #if defined(DOS)
279 /* The DOS port does not have all signals/signal functions. */
280 #define strsignal(sig) ""
281 /* Use 'no floating point' for bus errors; SIGBUS does not exist
282 * for DOS, SIGNOFP for other platforms. So it's fairly safe
283 * to interchange those. */
284 #define SIGBUS SIGNOFP
285 #endif
287 #if defined(WINCE)
288 #define strdup _strdup
289 #endif /* WINCE */
291 /* NOTE: the string returned by these functions is only valid until the next
292 * call to the same function and is not thread- or reentrancy-safe */
293 #if !defined(STRGEN) && !defined(SETTINGSGEN)
294 #if defined(WIN32) || defined(WIN64)
295 char *getcwd(char *buf, size_t size);
296 #include <tchar.h>
297 #include <io.h>
299 /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
300 #if !defined(WINCE)
301 namespace std { using ::_tfopen; }
302 #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
303 #define unlink(file) _tunlink(OTTD2FS(file))
304 #endif /* WINCE */
306 const char *FS2OTTD(const TCHAR *name);
307 const TCHAR *OTTD2FS(const char *name, bool console_cp = false);
308 #define SQ2OTTD(name) FS2OTTD(name)
309 #define OTTD2SQ(name) OTTD2FS(name)
310 #else
311 #define fopen(file, mode) fopen(OTTD2FS(file), mode)
312 const char *FS2OTTD(const char *name);
313 const char *OTTD2FS(const char *name);
314 #define SQ2OTTD(name) (name)
315 #define OTTD2SQ(name) (name)
316 #endif /* WIN32 */
317 #endif /* STRGEN || SETTINGSGEN */
319 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
320 #define PATHSEP "\\"
321 #define PATHSEPCHAR '\\'
322 #else
323 #define PATHSEP "/"
324 #define PATHSEPCHAR '/'
325 #endif
327 /* MSVCRT of course has to have a different syntax for long long *sigh* */
328 #if defined(_MSC_VER) || defined(__MINGW32__)
329 #define OTTD_PRINTF64 "%I64d"
330 #define OTTD_PRINTFHEX64 "%I64x"
331 #define PRINTF_SIZE "%Iu"
332 #else
333 #define OTTD_PRINTF64 "%lld"
334 #define OTTD_PRINTFHEX64 "%llx"
335 #define PRINTF_SIZE "%zu"
336 #endif
338 typedef unsigned char byte;
340 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
341 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
342 typedef unsigned int uint;
343 #endif
345 #if defined(TROUBLED_INTS)
346 /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
347 * trouble all over the place in OpenTTD. */
348 #define uint32 uint32_ugly_hack
349 #define int32 int32_ugly_hack
350 typedef unsigned int uint32_ugly_hack;
351 typedef signed int int32_ugly_hack;
352 #else
353 typedef unsigned char uint8;
354 typedef signed char int8;
355 typedef unsigned short uint16;
356 typedef signed short int16;
357 typedef unsigned int uint32;
358 typedef signed int int32;
359 typedef unsigned __int64 uint64;
360 typedef signed __int64 int64;
361 #endif /* !TROUBLED_INTS */
363 #if !defined(WITH_PERSONAL_DIR)
364 #define PERSONAL_DIR ""
365 #endif
367 /* Compile time assertions. Prefer c++0x static_assert().
368 * Older compilers cannot evaluate some expressions at compile time,
369 * typically when templates are involved, try assert_tcompile() in those cases. */
370 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert)
371 /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */
372 #define assert_compile(expr) static_assert(expr, #expr )
373 #define assert_tcompile(expr) assert_compile(expr)
374 #elif defined(__OS2__)
375 /* Disabled for OS/2 */
376 #define assert_compile(expr)
377 #define assert_tcompile(expr) assert_compile(expr)
378 #else
379 #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
380 #define assert_tcompile(expr) assert(expr)
381 #endif
383 /* Check if the types have the bitsizes like we are using them */
384 assert_compile(sizeof(uint64) == 8);
385 assert_compile(sizeof(uint32) == 4);
386 assert_compile(sizeof(uint16) == 2);
387 assert_compile(sizeof(uint8) == 1);
388 assert_compile(SIZE_MAX >= UINT32_MAX);
390 #ifndef M_PI_2
391 #define M_PI_2 1.57079632679489661923
392 #define M_PI 3.14159265358979323846
393 #endif /* M_PI_2 */
396 * Return the length of an fixed size array.
397 * Unlike sizeof this function returns the number of elements
398 * of the given type.
400 * @param x The pointer to the first element of the array
401 * @return The number of elements
403 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
406 * Get the end element of an fixed size array.
408 * @param x The pointer to the first element of the array
409 * @return The pointer past to the last element of the array
411 #define endof(x) (&x[lengthof(x)])
414 * Get the last element of an fixed size array.
416 * @param x The pointer to the first element of the array
417 * @return The pointer to the last element of the array
419 #define lastof(x) (&x[lengthof(x) - 1])
421 #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
422 #if !defined(offsetof)
423 #define offsetof(s, m) cpp_offsetof(s, m)
424 #endif /* offsetof */
427 * Gets the size of a variable within a class.
428 * @param base The class the variable is in.
429 * @param variable The variable to get the size of.
430 * @return the size of the variable
432 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
435 * Gets the length of an array variable within a class.
436 * @param base The class the variable is in.
437 * @param variable The array variable to get the size of.
438 * @return the length of the array
440 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
443 /* take care of some name clashes on MacOS */
444 #if defined(__APPLE__)
445 #define GetString OTTD_GetString
446 #define DrawString OTTD_DrawString
447 #define CloseConnection OTTD_CloseConnection
448 #endif /* __APPLE__ */
450 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
451 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
452 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
454 /* For non-debug builds with assertions enabled use the special assertion handler:
455 * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
456 * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
458 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
459 #undef assert
460 #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
461 #endif
463 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
464 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
465 #define OTTD_ASSERT
466 #endif
468 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
469 /* MorphOS and NDS don't have C++ conformant _stricmp... */
470 #define _stricmp stricmp
471 #elif defined(OPENBSD)
472 /* OpenBSD uses strcasecmp(3) */
473 #define _stricmp strcasecmp
474 #endif
476 #if defined(MAX_PATH)
477 /* It's already defined, no need to override */
478 #elif defined(PATH_MAX) && PATH_MAX > 0
479 /* Use the value from PATH_MAX, if it exists */
480 #define MAX_PATH PATH_MAX
481 #else
482 /* If all else fails, hardcode something :( */
483 #define MAX_PATH 260
484 #endif
487 * Version of the standard free that accepts const pointers.
488 * @param ptr The data to free.
490 static inline void free(const void *ptr)
492 free(const_cast<void *>(ptr));
496 * The largest value that can be entered in a variable
497 * @param type the type of the variable
499 #define MAX_UVALUE(type) ((type)~(type)0)
501 #endif /* STDAFX_H */