OpenTTD
stdafx.h
Go to the documentation of this file.
1 /* $Id$ */
2 
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  */
9 
12 #ifndef STDAFX_H
13 #define STDAFX_H
14 
15 #if defined(__APPLE__)
16  #include "os/macosx/osx_stdafx.h"
17 #endif /* __APPLE__ */
18 
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
29 
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
44 
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
82 
83 #include <cstdio>
84 #include <cstddef>
85 #include <cstring>
86 #include <cstdlib>
87 #include <climits>
88 #include <cassert>
89 
90 #ifndef SIZE_MAX
91  #define SIZE_MAX ((size_t)-1)
92 #endif
93 
94 #if defined(UNIX) || defined(__MINGW32__)
95  #include <sys/types.h>
96 #endif
97 
98 #if defined(__OS2__)
99  #include <types.h>
100  #define strcasecmp stricmp
101 #endif
102 
103 #if defined(SUNOS) || defined(HPUX)
104  #include <alloca.h>
105 #endif
106 
107 #if defined(__MORPHOS__)
108  /* MorphOS defines certain Amiga defines per default, we undefine them
109  * here to make the rest of source less messy and more clear what is
110  * required for morphos and what for AmigaOS */
111  #if defined(amigaos)
112  #undef amigaos
113  #endif
114  #if defined(__amigaos__)
115  #undef __amigaos__
116  # endif
117  #if defined(__AMIGA__)
118  #undef __AMIGA__
119  #endif
120  #if defined(AMIGA)
121  #undef AMIGA
122  #endif
123  #if defined(amiga)
124  #undef amiga
125  #endif
126  /* Act like we already included this file, as it somehow gives linkage problems
127  * (mismatch linkage of C++ and C between this include and unistd.h). */
128  #define CLIB_USERGROUP_PROTOS_H
129 #endif /* __MORPHOS__ */
130 
131 /* Stuff for GCC */
132 #if defined(__GNUC__)
133  #define NORETURN __attribute__ ((noreturn))
134  #define CDECL
135  #define __int64 long long
136  /* Warn about functions using 'printf' format syntax. First argument determines which parameter
137  * is the format string, second argument is start of values passed to printf. */
138  #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
139  #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
140  #define FINAL final
141  #else
142  #define FINAL
143  #endif
144 
145  /* Use fallthrough attribute where supported */
146  #if __GNUC__ >= 7
147  #if __cplusplus > 201402L // C++17
148  #define FALLTHROUGH [[fallthrough]]
149  #else
150  #define FALLTHROUGH __attribute__((fallthrough))
151  #endif
152  #else
153  #define FALLTHROUGH
154  #endif
155 #endif /* __GNUC__ */
156 
157 #if defined(__WATCOMC__)
158  #define NORETURN
159  #define CDECL
160  #define WARN_FORMAT(string, args)
161  #define FINAL
162  #define FALLTHROUGH
163  #include <malloc.h>
164 #endif /* __WATCOMC__ */
165 
166 #if defined(__MINGW32__) || defined(__CYGWIN__)
167  #include <malloc.h> // alloca()
168 #endif
169 
170 #if defined(_WIN32)
171  #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
172 #endif
173 
174 /* Stuff for MSVC */
175 #if defined(_MSC_VER)
176  #pragma once
177  #ifdef _WIN64
178  /* No 64-bit Windows below XP, so we can safely assume it as the target platform. */
179  #define NTDDI_VERSION NTDDI_WINXP // Windows XP
180  #define _WIN32_WINNT 0x501 // Windows XP
181  #define _WIN32_WINDOWS 0x501 // Windows XP
182  #define WINVER 0x0501 // Windows XP
183  #define _WIN32_IE_ 0x0600 // 6.0 (XP+)
184  #else
185  /* Define a win32 target platform, to override defaults of the SDK
186  * We need to define NTDDI version for Vista SDK, but win2k is minimum */
187  #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
188  #define _WIN32_WINNT 0x0500 // Windows 2000
189  #define _WIN32_WINDOWS 0x400 // Windows 95
190  #define WINVER 0x0400 // Windows NT 4.0 / Windows 95
191  #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+)
192  #endif
193  #define NOMINMAX // Disable min/max macros in windows.h.
194 
195  #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
196  #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
197  #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
198  #pragma warning(disable: 4355) // 'this' : used in base member initializer list
199 
200  #if (_MSC_VER < 1400) // MSVC 2005 safety checks
201  #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
202  #endif /* (_MSC_VER < 1400) */
203  #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)
204  #pragma warning(disable: 4996) // 'function': was declared deprecated
205  #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
206  #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
207  #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
208  #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
209  #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
210  #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
211  #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 ...
212 
213  #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
214  #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
215  #include <intrin.h>
216  #endif
217 
218  #include <malloc.h> // alloca()
219  #define NORETURN __declspec(noreturn)
220  #if (_MSC_VER < 1900)
221  #define inline __forceinline
222  #endif
223 
224  #define CDECL _cdecl
225  #define WARN_FORMAT(string, args)
226  #define FINAL sealed
227 
228  /* fallthrough attribute, VS 2017 */
229  #if (_MSC_VER >= 1910)
230  #define FALLTHROUGH [[fallthrough]]
231  #else
232  #define FALLTHROUGH
233  #endif
234 
235 # if defined(_WIN32) && !defined(_WIN64)
236  #if !defined(_W64)
237  #define _W64
238  #endif
239 
240  typedef _W64 int INT_PTR, *PINT_PTR;
241  typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
242 # endif /* _WIN32 && !_WIN64 */
243 
244 # if defined(_WIN64)
245  #define fseek _fseeki64
246 # endif /* _WIN64 */
247 
248  /* zlib from vcpkg use cdecl calling convention without enforcing it in the headers */
249 # if defined(WITH_ZLIB)
250 # if !defined(ZEXPORT)
251 # define ZEXPORT CDECL
252 # endif
253 # endif
254 
255  /* freetype from vcpkg use cdecl calling convention without enforcing it in the headers */
256 # if defined(WITH_FREETYPE)
257 # if !defined(FT_EXPORT)
258 # define FT_EXPORT( x ) extern "C" x CDECL
259 # endif
260 # endif
261 
262  /* liblzma from vcpkg (before 5.2.4-2) used to patch lzma.h to define LZMA_API_STATIC for static builds */
263 # if defined(WITH_LZMA)
264 # if !defined(LZMA_API_STATIC)
265 # define LZMA_API_STATIC
266 # endif
267 # endif
268 
269  #define strcasecmp stricmp
270  #define strncasecmp strnicmp
271  #define strtoull _strtoui64
272 
273  /* MSVC doesn't have these :( */
274  #define S_ISDIR(mode) (mode & S_IFDIR)
275  #define S_ISREG(mode) (mode & S_IFREG)
276 
277 #endif /* defined(_MSC_VER) */
278 
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
287 
288 /* NOTE: the string returned by these functions is only valid until the next
289  * call to the same function and is not thread- or reentrancy-safe */
290 #if !defined(STRGEN) && !defined(SETTINGSGEN)
291 # if defined(_WIN32)
292  char *getcwd(char *buf, size_t size);
293  #include <tchar.h>
294  #include <io.h>
295 
296  namespace std { using ::_tfopen; }
297  #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
298  #define unlink(file) _tunlink(OTTD2FS(file))
299 
300  const char *FS2OTTD(const TCHAR *name);
301  const TCHAR *OTTD2FS(const char *name, bool console_cp = false);
302 # else
303  #define fopen(file, mode) fopen(OTTD2FS(file), mode)
304  const char *FS2OTTD(const char *name);
305  const char *OTTD2FS(const char *name);
306 # endif /* _WIN32 */
307 #endif /* STRGEN || SETTINGSGEN */
308 
309 #if defined(_WIN32) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
310  #define PATHSEP "\\"
311  #define PATHSEPCHAR '\\'
312 #else
313  #define PATHSEP "/"
314  #define PATHSEPCHAR '/'
315 #endif
316 
317 #if defined(_MSC_VER) || defined(__WATCOMC__)
318 # define PACK_N(type_dec, n) __pragma(pack(push, n)) type_dec; __pragma(pack(pop))
319 #elif defined(__MINGW32__)
320 # define PRAGMA(x) _Pragma(#x)
321 # define PACK_N(type_dec, n) PRAGMA(pack(push, n)) type_dec; PRAGMA(pack(pop))
322 #else
323 # define PACK_N(type_dec, n) type_dec __attribute__((__packed__, aligned(n)))
324 #endif
325 #define PACK(type_dec) PACK_N(type_dec, 1)
326 
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 # define PRINTF_SIZEX "%IX"
333 #else
334 # define OTTD_PRINTF64 "%lld"
335 # define OTTD_PRINTFHEX64 "%llx"
336 # define PRINTF_SIZE "%zu"
337 # define PRINTF_SIZEX "%zX"
338 #endif
339 
340 typedef unsigned char byte;
341 
342 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
343 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
344  typedef unsigned int uint;
345 #endif
346 
347 #if defined(TROUBLED_INTS)
348  /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
349  * trouble all over the place in OpenTTD. */
350  #define uint32 uint32_ugly_hack
351  #define int32 int32_ugly_hack
352  typedef unsigned int uint32_ugly_hack;
353  typedef signed int int32_ugly_hack;
354 #else
355  typedef unsigned char uint8;
356  typedef signed char int8;
357  typedef unsigned short uint16;
358  typedef signed short int16;
359  typedef unsigned int uint32;
360  typedef signed int int32;
361  typedef unsigned __int64 uint64;
362  typedef signed __int64 int64;
363 #endif /* !TROUBLED_INTS */
364 
365 #if !defined(WITH_PERSONAL_DIR)
366  #define PERSONAL_DIR ""
367 #endif
368 
369 /* Compile time assertions. Prefer c++0x static_assert().
370  * Older compilers cannot evaluate some expressions at compile time,
371  * typically when templates are involved, try assert_tcompile() in those cases. */
372 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
373  #define assert_compile(expr) static_assert(expr, #expr )
374  #define assert_tcompile(expr) assert_compile(expr)
375 #elif defined(__OS2__)
376  /* Disabled for OS/2 */
377  #define assert_compile(expr)
378  #define assert_tcompile(expr) assert_compile(expr)
379 #else
380  #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
381  #define assert_tcompile(expr) assert(expr)
382 #endif
383 
384 /* Check if the types have the bitsizes like we are using them */
385 assert_compile(sizeof(uint64) == 8);
386 assert_compile(sizeof(uint32) == 4);
387 assert_compile(sizeof(uint16) == 2);
388 assert_compile(sizeof(uint8) == 1);
389 assert_compile(SIZE_MAX >= UINT32_MAX);
390 
391 #ifndef M_PI_2
392 #define M_PI_2 1.57079632679489661923
393 #define M_PI 3.14159265358979323846
394 #endif /* M_PI_2 */
395 
404 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
405 
412 #define endof(x) (&x[lengthof(x)])
413 
420 #define lastof(x) (&x[lengthof(x) - 1])
421 
422 #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
423 #if !defined(offsetof)
424  #define offsetof(s, m) cpp_offsetof(s, m)
425 #endif /* offsetof */
426 
433 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
434 
441 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
442 
443 
444 /* take care of some name clashes on MacOS */
445 #if defined(__APPLE__)
446  #define GetString OTTD_GetString
447  #define DrawString OTTD_DrawString
448  #define CloseConnection OTTD_CloseConnection
449 #endif /* __APPLE__ */
450 
451 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
452 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
453 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
454 
455 /* For non-debug builds with assertions enabled use the special assertion handler:
456  * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
457  * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
458  */
459 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
460  #undef assert
461  #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
462 #endif
463 
464 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
465 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
466  #define OTTD_ASSERT
467 #endif
468 
469 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
470  /* MorphOS and NDS don't have C++ conformant _stricmp... */
471  #define _stricmp stricmp
472 #elif defined(OPENBSD)
473  /* OpenBSD uses strcasecmp(3) */
474  #define _stricmp strcasecmp
475 #endif
476 
477 #if defined(MAX_PATH)
478  /* It's already defined, no need to override */
479 #elif defined(PATH_MAX) && PATH_MAX > 0
480  /* Use the value from PATH_MAX, if it exists */
481  #define MAX_PATH PATH_MAX
482 #else
483  /* If all else fails, hardcode something :( */
484  #define MAX_PATH 260
485 #endif
486 
491 static inline void free(const void *ptr)
492 {
493  free(const_cast<void *>(ptr));
494 }
495 
500 #define MAX_UVALUE(type) ((type)~(type)0)
501 
502 #if defined(_MSC_VER) && !defined(_DEBUG)
503  #define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
504  #define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
505 #elif defined(__GNUC__) && !defined(_DEBUG)
506  #define HELPER0(x) #x
507  #define HELPER1(x) HELPER0(GCC diagnostic ignored x)
508  #define HELPER2(y) HELPER1(#y)
509 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
510  #define IGNORE_UNINITIALIZED_WARNING_START \
511  _Pragma("GCC diagnostic push") \
512  _Pragma(HELPER2(-Wuninitialized)) \
513  _Pragma(HELPER2(-Wmaybe-uninitialized))
514  #define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
515 #endif
516 #endif
517 
518 #ifndef IGNORE_UNINITIALIZED_WARNING_START
519  #define IGNORE_UNINITIALIZED_WARNING_START
520  #define IGNORE_UNINITIALIZED_WARNING_STOP
521 #endif
522 
523 #endif /* STDAFX_H */
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD&#39;s encoding from that of the local environment.
Definition: win32.cpp:565
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:491
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:92
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:583
OSX is different on some places.