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