(svn r27747) -Update from Eints:
[openttd.git] / src / stdafx.h
blob4616212f187cebb06b24a71a6442a513e240fd11
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 #include <strings.h>
25 #elif defined(__NDS__)
26 #include <nds/jtypes.h>
27 #define TROUBLED_INTS
28 #endif
30 /* It seems that we need to include stdint.h before anything else
31 * We need INT64_MAX, which for most systems comes from stdint.h. However, MSVC
32 * does not have stdint.h and apparently neither does MorphOS.
33 * For OSX the inclusion is already done in osx_stdafx.h. */
34 #if !defined(__APPLE__) && (!defined(_MSC_VER) || _MSC_VER >= 1600) && !defined(__MORPHOS__)
35 #if defined(SUNOS)
36 /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything
37 * stdint.h defines and we need. */
38 #include <inttypes.h>
39 #else
40 #define __STDC_LIMIT_MACROS
41 #include <stdint.h>
42 #endif
43 #endif
45 /* The conditions for these constants to be available are way too messy; so check them one by one */
46 #if !defined(UINT64_MAX)
47 #define UINT64_MAX (18446744073709551615ULL)
48 #endif
49 #if !defined(INT64_MAX)
50 #define INT64_MAX (9223372036854775807LL)
51 #endif
52 #if !defined(INT64_MIN)
53 #define INT64_MIN (-INT64_MAX - 1)
54 #endif
55 #if !defined(UINT32_MAX)
56 #define UINT32_MAX (4294967295U)
57 #endif
58 #if !defined(INT32_MAX)
59 #define INT32_MAX (2147483647)
60 #endif
61 #if !defined(INT32_MIN)
62 #define INT32_MIN (-INT32_MAX - 1)
63 #endif
64 #if !defined(UINT16_MAX)
65 #define UINT16_MAX (65535U)
66 #endif
67 #if !defined(INT16_MAX)
68 #define INT16_MAX (32767)
69 #endif
70 #if !defined(INT16_MIN)
71 #define INT16_MIN (-INT16_MAX - 1)
72 #endif
73 #if !defined(UINT8_MAX)
74 #define UINT8_MAX (255)
75 #endif
76 #if !defined(INT8_MAX)
77 #define INT8_MAX (127)
78 #endif
79 #if !defined(INT8_MIN)
80 #define INT8_MIN (-INT8_MAX - 1)
81 #endif
83 #include <cstdio>
84 #include <cstddef>
85 #include <cstring>
86 #include <cstdlib>
87 #include <climits>
88 #include <cassert>
90 #ifndef SIZE_MAX
91 #define SIZE_MAX ((size_t)-1)
92 #endif
94 #if defined(UNIX) || defined(__MINGW32__)
95 #include <sys/types.h>
96 #endif
98 #if defined(__OS2__)
99 #include <types.h>
100 #define strcasecmp stricmp
101 #endif
103 #if defined(PSP)
104 #include <psptypes.h>
105 #include <pspdebug.h>
106 #include <pspthreadman.h>
107 #endif
109 #if defined(SUNOS) || defined(HPUX)
110 #include <alloca.h>
111 #endif
113 #if defined(__MORPHOS__)
114 /* MorphOS defines certain Amiga defines per default, we undefine them
115 * here to make the rest of source less messy and more clear what is
116 * required for morphos and what for AmigaOS */
117 #if defined(amigaos)
118 #undef amigaos
119 #endif
120 #if defined(__amigaos__)
121 #undef __amigaos__
122 # endif
123 #if defined(__AMIGA__)
124 #undef __AMIGA__
125 #endif
126 #if defined(AMIGA)
127 #undef AMIGA
128 #endif
129 #if defined(amiga)
130 #undef amiga
131 #endif
132 /* Act like we already included this file, as it somehow gives linkage problems
133 * (mismatch linkage of C++ and C between this include and unistd.h). */
134 #define CLIB_USERGROUP_PROTOS_H
135 #endif /* __MORPHOS__ */
137 #if defined(PSP)
138 /* PSP can only have 10 file-descriptors open at any given time, but this
139 * switch only limits reads via the Fio system. So keep 2 fds free for things
140 * like saving a game. */
141 #define LIMITED_FDS 8
142 #define printf pspDebugScreenPrintf
143 #endif /* PSP */
145 /* Stuff for GCC */
146 #if defined(__GNUC__)
147 #define NORETURN __attribute__ ((noreturn))
148 #define CDECL
149 #define __int64 long long
150 #define GCC_PACK __attribute__((packed))
151 /* Warn about functions using 'printf' format syntax. First argument determines which parameter
152 * is the format string, second argument is start of values passed to printf. */
153 #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
154 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
155 #define FINAL final
156 #else
157 #define FINAL
158 #endif
159 #endif /* __GNUC__ */
161 #if defined(__WATCOMC__)
162 #define NORETURN
163 #define CDECL
164 #define GCC_PACK
165 #define WARN_FORMAT(string, args)
166 #define FINAL
167 #include <malloc.h>
168 #endif /* __WATCOMC__ */
170 #if defined(__MINGW32__) || defined(__CYGWIN__)
171 #include <malloc.h> // alloca()
172 #endif
174 #if defined(WIN32)
175 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
176 #endif
178 /* Stuff for MSVC */
179 #if defined(_MSC_VER)
180 #pragma once
181 #ifdef _WIN64
182 /* No 64-bit Windows below XP, so we can safely assume it as the target platform. */
183 #define NTDDI_VERSION NTDDI_WINXP // Windows XP
184 #define _WIN32_WINNT 0x501 // Windows XP
185 #define _WIN32_WINDOWS 0x501 // Windows XP
186 #define WINVER 0x0501 // Windows XP
187 #define _WIN32_IE_ 0x0600 // 6.0 (XP+)
188 #else
189 /* Define a win32 target platform, to override defaults of the SDK
190 * We need to define NTDDI version for Vista SDK, but win2k is minimum */
191 #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
192 #define _WIN32_WINNT 0x0500 // Windows 2000
193 #define _WIN32_WINDOWS 0x400 // Windows 95
194 #if !defined(WINCE)
195 #define WINVER 0x0400 // Windows NT 4.0 / Windows 95
196 #endif
197 #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+)
198 #endif
199 #define NOMINMAX // Disable min/max macros in windows.h.
201 #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
202 #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
203 #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
204 #pragma warning(disable: 4355) // 'this' : used in base member initializer list
206 #if (_MSC_VER < 1400) // MSVC 2005 safety checks
207 #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
208 #endif /* (_MSC_VER < 1400) */
209 #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)
210 #pragma warning(disable: 4996) // 'function': was declared deprecated
211 #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
212 #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
213 #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
214 #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
215 #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
216 #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
217 #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 ...
219 #if (_MSC_VER == 1500) // Addresses item #13 on http://blogs.msdn.com/b/vcblog/archive/2008/08/11/tr1-fixes-in-vc9-sp1.aspx, for Visual Studio 2008
220 #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
221 #include <intrin.h>
222 #endif
224 #include <malloc.h> // alloca()
225 #define NORETURN __declspec(noreturn)
226 #if (_MSC_VER < 1900)
227 #define inline __forceinline
228 #endif
230 #if !defined(WINCE)
231 #define CDECL _cdecl
232 #endif
234 #define GCC_PACK
235 #define WARN_FORMAT(string, args)
236 #define FINAL sealed
238 #if defined(WINCE)
239 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
240 #endif
242 #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
243 #if !defined(_W64)
244 #define _W64
245 #endif
247 typedef _W64 int INT_PTR, *PINT_PTR;
248 typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
249 #endif /* WIN32 && !_WIN64 && !WIN64 */
251 #if defined(_WIN64) || defined(WIN64)
252 #define fseek _fseeki64
253 #endif /* _WIN64 || WIN64 */
255 /* This is needed to zlib uses the stdcall calling convention on visual studio */
256 #if defined(WITH_ZLIB) || defined(WITH_PNG)
257 #if !defined(ZLIB_WINAPI)
258 #define ZLIB_WINAPI
259 #endif
260 #endif
262 #if defined(WINCE)
263 #define strcasecmp _stricmp
264 #define strncasecmp _strnicmp
265 #undef DEBUG
266 #else
267 #define strcasecmp stricmp
268 #define strncasecmp strnicmp
269 #endif
271 #define strtoull _strtoui64
273 /* MSVC doesn't have these :( */
274 #define S_ISDIR(mode) (mode & S_IFDIR)
275 #define S_ISREG(mode) (mode & S_IFREG)
277 #endif /* defined(_MSC_VER) */
279 #if defined(DOS)
280 /* The DOS port does not have all signals/signal functions. */
281 #define strsignal(sig) ""
282 /* Use 'no floating point' for bus errors; SIGBUS does not exist
283 * for DOS, SIGNOFP for other platforms. So it's fairly safe
284 * to interchange those. */
285 #define SIGBUS SIGNOFP
286 #endif
288 #if defined(WINCE)
289 #define stredup _stredup
290 #endif /* WINCE */
292 /* NOTE: the string returned by these functions is only valid until the next
293 * call to the same function and is not thread- or reentrancy-safe */
294 #if !defined(STRGEN) && !defined(SETTINGSGEN)
295 #if defined(WIN32) || defined(WIN64)
296 char *getcwd(char *buf, size_t size);
297 #include <tchar.h>
298 #include <io.h>
300 /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
301 #if !defined(WINCE)
302 namespace std { using ::_tfopen; }
303 #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
304 #define unlink(file) _tunlink(OTTD2FS(file))
305 #endif /* WINCE */
307 const char *FS2OTTD(const TCHAR *name);
308 const TCHAR *OTTD2FS(const char *name, bool console_cp = false);
309 #else
310 #define fopen(file, mode) fopen(OTTD2FS(file), mode)
311 const char *FS2OTTD(const char *name);
312 const char *OTTD2FS(const char *name);
313 #endif /* WIN32 */
314 #endif /* STRGEN || SETTINGSGEN */
316 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
317 #define PATHSEP "\\"
318 #define PATHSEPCHAR '\\'
319 #else
320 #define PATHSEP "/"
321 #define PATHSEPCHAR '/'
322 #endif
324 /* MSVCRT of course has to have a different syntax for long long *sigh* */
325 #if defined(_MSC_VER) || defined(__MINGW32__)
326 #define OTTD_PRINTF64 "%I64d"
327 #define OTTD_PRINTFHEX64 "%I64x"
328 #define PRINTF_SIZE "%Iu"
329 #else
330 #define OTTD_PRINTF64 "%lld"
331 #define OTTD_PRINTFHEX64 "%llx"
332 #define PRINTF_SIZE "%zu"
333 #endif
335 typedef unsigned char byte;
337 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
338 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
339 typedef unsigned int uint;
340 #endif
342 #if defined(TROUBLED_INTS)
343 /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
344 * trouble all over the place in OpenTTD. */
345 #define uint32 uint32_ugly_hack
346 #define int32 int32_ugly_hack
347 typedef unsigned int uint32_ugly_hack;
348 typedef signed int int32_ugly_hack;
349 #else
350 typedef unsigned char uint8;
351 typedef signed char int8;
352 typedef unsigned short uint16;
353 typedef signed short int16;
354 typedef unsigned int uint32;
355 typedef signed int int32;
356 typedef unsigned __int64 uint64;
357 typedef signed __int64 int64;
358 #endif /* !TROUBLED_INTS */
360 #if !defined(WITH_PERSONAL_DIR)
361 #define PERSONAL_DIR ""
362 #endif
364 /* Compile time assertions. Prefer c++0x static_assert().
365 * Older compilers cannot evaluate some expressions at compile time,
366 * typically when templates are involved, try assert_tcompile() in those cases. */
367 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert)
368 /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */
369 #define assert_compile(expr) static_assert(expr, #expr )
370 #define assert_tcompile(expr) assert_compile(expr)
371 #elif defined(__OS2__)
372 /* Disabled for OS/2 */
373 #define assert_compile(expr)
374 #define assert_tcompile(expr) assert_compile(expr)
375 #else
376 #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
377 #define assert_tcompile(expr) assert(expr)
378 #endif
380 /* Check if the types have the bitsizes like we are using them */
381 assert_compile(sizeof(uint64) == 8);
382 assert_compile(sizeof(uint32) == 4);
383 assert_compile(sizeof(uint16) == 2);
384 assert_compile(sizeof(uint8) == 1);
385 assert_compile(SIZE_MAX >= UINT32_MAX);
387 #ifndef M_PI_2
388 #define M_PI_2 1.57079632679489661923
389 #define M_PI 3.14159265358979323846
390 #endif /* M_PI_2 */
393 * Return the length of an fixed size array.
394 * Unlike sizeof this function returns the number of elements
395 * of the given type.
397 * @param x The pointer to the first element of the array
398 * @return The number of elements
400 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
403 * Get the end element of an fixed size array.
405 * @param x The pointer to the first element of the array
406 * @return The pointer past to the last element of the array
408 #define endof(x) (&x[lengthof(x)])
411 * Get the last element of an fixed size array.
413 * @param x The pointer to the first element of the array
414 * @return The pointer to the last element of the array
416 #define lastof(x) (&x[lengthof(x) - 1])
418 #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
419 #if !defined(offsetof)
420 #define offsetof(s, m) cpp_offsetof(s, m)
421 #endif /* offsetof */
424 * Gets the size of a variable within a class.
425 * @param base The class the variable is in.
426 * @param variable The variable to get the size of.
427 * @return the size of the variable
429 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
432 * Gets the length of an array variable within a class.
433 * @param base The class the variable is in.
434 * @param variable The array variable to get the size of.
435 * @return the length of the array
437 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
440 /* take care of some name clashes on MacOS */
441 #if defined(__APPLE__)
442 #define GetString OTTD_GetString
443 #define DrawString OTTD_DrawString
444 #define CloseConnection OTTD_CloseConnection
445 #endif /* __APPLE__ */
447 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
448 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
449 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
451 /* For non-debug builds with assertions enabled use the special assertion handler:
452 * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
453 * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
455 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
456 #undef assert
457 #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
458 #endif
460 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
461 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
462 #define OTTD_ASSERT
463 #endif
465 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
466 /* MorphOS and NDS don't have C++ conformant _stricmp... */
467 #define _stricmp stricmp
468 #elif defined(OPENBSD)
469 /* OpenBSD uses strcasecmp(3) */
470 #define _stricmp strcasecmp
471 #endif
473 #if defined(MAX_PATH)
474 /* It's already defined, no need to override */
475 #elif defined(PATH_MAX) && PATH_MAX > 0
476 /* Use the value from PATH_MAX, if it exists */
477 #define MAX_PATH PATH_MAX
478 #else
479 /* If all else fails, hardcode something :( */
480 #define MAX_PATH 260
481 #endif
484 * Version of the standard free that accepts const pointers.
485 * @param ptr The data to free.
487 static inline void free(const void *ptr)
489 free(const_cast<void *>(ptr));
493 * The largest value that can be entered in a variable
494 * @param type the type of the variable
496 #define MAX_UVALUE(type) ((type)~(type)0)
498 #if defined(_MSC_VER) && !defined(_DEBUG)
499 #define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
500 #define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
501 #elif defined(__GNUC__) && !defined(_DEBUG)
502 #define HELPER0(x) #x
503 #define HELPER1(x) HELPER0(GCC diagnostic ignored x)
504 #define HELPER2(y) HELPER1(#y)
505 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
506 #define IGNORE_UNINITIALIZED_WARNING_START \
507 _Pragma("GCC diagnostic push") \
508 _Pragma(HELPER2(-Wuninitialized)) \
509 _Pragma(HELPER2(-Wmaybe-uninitialized))
510 #define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
511 #endif
512 #endif
514 #ifndef IGNORE_UNINITIALIZED_WARNING_START
515 #define IGNORE_UNINITIALIZED_WARNING_START
516 #define IGNORE_UNINITIALIZED_WARNING_STOP
517 #endif
519 #endif /* STDAFX_H */