OpenTTD
stdafx.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef STDAFX_H
11 #define STDAFX_H
12 
13 #if defined(__APPLE__)
14  #include "os/macosx/osx_stdafx.h"
15 #endif /* __APPLE__ */
16 
17 #if defined(__HAIKU__)
18  #include <SupportDefs.h>
19  #include <unistd.h>
20  #define _GNU_SOURCE
21  #define TROUBLED_INTS
22 #endif
23 
24 #if defined(__HAIKU__) || defined(__CYGWIN__)
25 # include <strings.h> /* strncasecmp */
26 #endif
27 
28 /* It seems that we need to include stdint.h before anything else
29  * We need INT64_MAX, which for most systems comes from stdint.h. However, MSVC
30  * does not have stdint.h.
31  * For OSX the inclusion is already done in osx_stdafx.h. */
32 #if !defined(__APPLE__) && (!defined(_MSC_VER) || _MSC_VER >= 1600)
33  #if defined(SUNOS)
34  /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything
35  * stdint.h defines and we need. */
36  #include <inttypes.h>
37  #else
38  #define __STDC_LIMIT_MACROS
39  #include <stdint.h>
40  #endif
41 #endif
42 
43 /* The conditions for these constants to be available are way too messy; so check them one by one */
44 #if !defined(UINT64_MAX)
45  #define UINT64_MAX (18446744073709551615ULL)
46 #endif
47 #if !defined(INT64_MAX)
48  #define INT64_MAX (9223372036854775807LL)
49 #endif
50 #if !defined(INT64_MIN)
51  #define INT64_MIN (-INT64_MAX - 1)
52 #endif
53 #if !defined(UINT32_MAX)
54  #define UINT32_MAX (4294967295U)
55 #endif
56 #if !defined(INT32_MAX)
57  #define INT32_MAX (2147483647)
58 #endif
59 #if !defined(INT32_MIN)
60  #define INT32_MIN (-INT32_MAX - 1)
61 #endif
62 #if !defined(UINT16_MAX)
63  #define UINT16_MAX (65535U)
64 #endif
65 #if !defined(INT16_MAX)
66  #define INT16_MAX (32767)
67 #endif
68 #if !defined(INT16_MIN)
69  #define INT16_MIN (-INT16_MAX - 1)
70 #endif
71 #if !defined(UINT8_MAX)
72  #define UINT8_MAX (255)
73 #endif
74 #if !defined(INT8_MAX)
75  #define INT8_MAX (127)
76 #endif
77 #if !defined(INT8_MIN)
78  #define INT8_MIN (-INT8_MAX - 1)
79 #endif
80 
81 #include <cstdio>
82 #include <cstddef>
83 #include <cstring>
84 #include <cstdlib>
85 #include <climits>
86 #include <cassert>
87 #include <memory>
88 
89 #ifndef SIZE_MAX
90  #define SIZE_MAX ((size_t)-1)
91 #endif
92 
93 #if defined(UNIX) || defined(__MINGW32__)
94  #include <sys/types.h>
95 #endif
96 
97 #if defined(__OS2__)
98  #include <types.h>
99  #define strcasecmp stricmp
100 #endif
101 
102 #if defined(SUNOS) || defined(HPUX) || defined(__CYGWIN__)
103  #include <alloca.h>
104 #endif
105 
106 /* Stuff for GCC */
107 #if defined(__GNUC__)
108  #define NORETURN __attribute__ ((noreturn))
109  #define CDECL
110  #define __int64 long long
111  /* Warn about functions using 'printf' format syntax. First argument determines which parameter
112  * is the format string, second argument is start of values passed to printf. */
113  #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
114  #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
115  #define FINAL final
116  #else
117  #define FINAL
118  #endif
119 
120  /* Use fallthrough attribute where supported */
121  #if __GNUC__ >= 7
122  #if __cplusplus > 201402L // C++17
123  #define FALLTHROUGH [[fallthrough]]
124  #else
125  #define FALLTHROUGH __attribute__((fallthrough))
126  #endif
127  #else
128  #define FALLTHROUGH
129  #endif
130 #endif /* __GNUC__ */
131 
132 #if defined(__WATCOMC__)
133  #define NORETURN
134  #define CDECL
135  #define WARN_FORMAT(string, args)
136  #define FINAL
137  #define FALLTHROUGH
138  #include <malloc.h>
139 #endif /* __WATCOMC__ */
140 
141 #if defined(__MINGW32__)
142  #include <malloc.h> // alloca()
143 #endif
144 
145 #if defined(_WIN32)
146  #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
147 #endif
148 
149 /* Stuff for MSVC */
150 #if defined(_MSC_VER)
151  #pragma once
152  #ifdef _WIN64
153  /* No 64-bit Windows below XP, so we can safely assume it as the target platform. */
154  #define NTDDI_VERSION NTDDI_WINXP // Windows XP
155  #define _WIN32_WINNT 0x501 // Windows XP
156  #define _WIN32_WINDOWS 0x501 // Windows XP
157  #define WINVER 0x0501 // Windows XP
158  #define _WIN32_IE_ 0x0600 // 6.0 (XP+)
159  #else
160  /* Define a win32 target platform, to override defaults of the SDK
161  * We need to define NTDDI version for Vista SDK, but win2k is minimum */
162  #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
163  #define _WIN32_WINNT 0x0500 // Windows 2000
164  #define _WIN32_WINDOWS 0x400 // Windows 95
165  #define WINVER 0x0400 // Windows NT 4.0 / Windows 95
166  #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+)
167  #endif
168  #define NOMINMAX // Disable min/max macros in windows.h.
169 
170  #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
171  #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
172  #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
173  #pragma warning(disable: 4355) // 'this' : used in base member initializer list
174 
175  #if (_MSC_VER < 1400) // MSVC 2005 safety checks
176  #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
177  #endif /* (_MSC_VER < 1400) */
178  #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)
179  #pragma warning(disable: 4996) // 'function': was declared deprecated
180  #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
181  #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
182  #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
183  #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
184  #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
185  #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
186  #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 ...
187 
188  #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
189  #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
190  #include <intrin.h>
191  #endif
192 
193  #include <malloc.h> // alloca()
194  #define NORETURN __declspec(noreturn)
195  #if (_MSC_VER < 1900)
196  #define inline __forceinline
197  #endif
198 
199  #define CDECL _cdecl
200  #define WARN_FORMAT(string, args)
201  #define FINAL sealed
202 
203  /* fallthrough attribute, VS 2017 */
204  #if (_MSC_VER >= 1910)
205  #define FALLTHROUGH [[fallthrough]]
206  #else
207  #define FALLTHROUGH
208  #endif
209 
210 # if defined(_WIN32) && !defined(_WIN64)
211  #if !defined(_W64)
212  #define _W64
213  #endif
214 
215  typedef _W64 int INT_PTR, *PINT_PTR;
216  typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
217 # endif /* _WIN32 && !_WIN64 */
218 
219 # if defined(_WIN64)
220  #define fseek _fseeki64
221 # endif /* _WIN64 */
222 
223  /* zlib from vcpkg use cdecl calling convention without enforcing it in the headers */
224 # if defined(WITH_ZLIB)
225 # if !defined(ZEXPORT)
226 # define ZEXPORT CDECL
227 # endif
228 # endif
229 
230  /* freetype from vcpkg use cdecl calling convention without enforcing it in the headers */
231 # if defined(WITH_FREETYPE)
232 # if !defined(FT_EXPORT)
233 # define FT_EXPORT( x ) extern "C" x CDECL
234 # endif
235 # endif
236 
237  /* liblzma from vcpkg (before 5.2.4-2) used to patch lzma.h to define LZMA_API_STATIC for static builds */
238 # if defined(WITH_LIBLZMA)
239 # if !defined(LZMA_API_STATIC)
240 # define LZMA_API_STATIC
241 # endif
242 # endif
243 
244  #define strcasecmp stricmp
245  #define strncasecmp strnicmp
246  #define strtoull _strtoui64
247 
248  /* MSVC doesn't have these :( */
249  #define S_ISDIR(mode) (mode & S_IFDIR)
250  #define S_ISREG(mode) (mode & S_IFREG)
251 
252 #endif /* defined(_MSC_VER) */
253 
254 /* NOTE: the string returned by these functions is only valid until the next
255  * call to the same function and is not thread- or reentrancy-safe */
256 #if !defined(STRGEN) && !defined(SETTINGSGEN)
257 # if defined(_WIN32)
258  char *getcwd(char *buf, size_t size);
259  #include <tchar.h>
260  #include <io.h>
261 
262  namespace std { using ::_tfopen; }
263  #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
264  #define unlink(file) _tunlink(OTTD2FS(file))
265 
266  const char *FS2OTTD(const TCHAR *name);
267  const TCHAR *OTTD2FS(const char *name, bool console_cp = false);
268 # else
269  #define fopen(file, mode) fopen(OTTD2FS(file), mode)
270  const char *FS2OTTD(const char *name);
271  const char *OTTD2FS(const char *name);
272 # endif /* _WIN32 */
273 #endif /* STRGEN || SETTINGSGEN */
274 
275 #if defined(_WIN32) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
276  #define PATHSEP "\\"
277  #define PATHSEPCHAR '\\'
278 #else
279  #define PATHSEP "/"
280  #define PATHSEPCHAR '/'
281 #endif
282 
283 #if defined(_MSC_VER) || defined(__WATCOMC__)
284 # define PACK_N(type_dec, n) __pragma(pack(push, n)) type_dec; __pragma(pack(pop))
285 #elif defined(__MINGW32__)
286 # define PRAGMA(x) _Pragma(#x)
287 # define PACK_N(type_dec, n) PRAGMA(pack(push, n)) type_dec; PRAGMA(pack(pop))
288 #else
289 # define PACK_N(type_dec, n) type_dec __attribute__((__packed__, aligned(n)))
290 #endif
291 #define PACK(type_dec) PACK_N(type_dec, 1)
292 
293 /* MSVCRT of course has to have a different syntax for long long *sigh* */
294 #if defined(_MSC_VER) || defined(__MINGW32__)
295 # define OTTD_PRINTF64 "%I64d"
296 # define OTTD_PRINTFHEX64 "%I64x"
297 # define PRINTF_SIZE "%Iu"
298 # define PRINTF_SIZEX "%IX"
299 #else
300 # define OTTD_PRINTF64 "%lld"
301 # define OTTD_PRINTFHEX64 "%llx"
302 # define PRINTF_SIZE "%zu"
303 # define PRINTF_SIZEX "%zX"
304 #endif
305 
306 typedef unsigned char byte;
307 
308 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
309 #if (!defined(UNIX) && !defined(__HAIKU__)) || defined(__QNXNTO__)
310  typedef unsigned int uint;
311 #endif
312 
313 #if defined(TROUBLED_INTS)
314  /* Haiku's types for uint32/int32/uint64/int64 are different than what
315  * they are on other platforms; not in length, but how to print them.
316  * So make them more like the other platforms, to make printf() etc a
317  * little bit easier. */
318 # define uint32 uint32_ugly_hack
319 # define int32 int32_ugly_hack
320 # define uint64 uint64_ugly_hack
321 # define int64 int64_ugly_hack
322  typedef unsigned int uint32_ugly_hack;
323  typedef signed int int32_ugly_hack;
324  typedef unsigned __int64 uint64_ugly_hack;
325  typedef signed __int64 int64_ugly_hack;
326 #else
327  typedef unsigned char uint8;
328  typedef signed char int8;
329  typedef unsigned short uint16;
330  typedef signed short int16;
331  typedef unsigned int uint32;
332  typedef signed int int32;
333  typedef unsigned __int64 uint64;
334  typedef signed __int64 int64;
335 #endif /* !TROUBLED_INTS */
336 
337 #if !defined(WITH_PERSONAL_DIR)
338  #define PERSONAL_DIR ""
339 #endif
340 
341 /* Compile time assertions. Prefer c++0x static_assert().
342  * Older compilers cannot evaluate some expressions at compile time,
343  * typically when templates are involved, try assert_tcompile() in those cases. */
344 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
345  #define assert_compile(expr) static_assert(expr, #expr )
346  #define assert_tcompile(expr) assert_compile(expr)
347 #elif defined(__OS2__)
348  /* Disabled for OS/2 */
349  #define assert_compile(expr)
350  #define assert_tcompile(expr) assert_compile(expr)
351 #else
352  #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
353  #define assert_tcompile(expr) assert(expr)
354 #endif
355 
356 /* Check if the types have the bitsizes like we are using them */
357 assert_compile(sizeof(uint64) == 8);
358 assert_compile(sizeof(uint32) == 4);
359 assert_compile(sizeof(uint16) == 2);
360 assert_compile(sizeof(uint8) == 1);
361 assert_compile(SIZE_MAX >= UINT32_MAX);
362 
363 #ifndef M_PI_2
364 #define M_PI_2 1.57079632679489661923
365 #define M_PI 3.14159265358979323846
366 #endif /* M_PI_2 */
367 
376 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
377 
384 #define endof(x) (&x[lengthof(x)])
385 
392 #define lastof(x) (&x[lengthof(x) - 1])
393 
394 #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
395 #if !defined(offsetof)
396  #define offsetof(s, m) cpp_offsetof(s, m)
397 #endif /* offsetof */
398 
405 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
406 
413 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
414 
415 
416 /* take care of some name clashes on MacOS */
417 #if defined(__APPLE__)
418  #define GetString OTTD_GetString
419  #define DrawString OTTD_DrawString
420  #define CloseConnection OTTD_CloseConnection
421 #endif /* __APPLE__ */
422 
423 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
424 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
425 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
426 
427 /* For non-debug builds with assertions enabled use the special assertion handler:
428  * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
429  * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
430  */
431 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
432  #undef assert
433  #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
434 #endif
435 
436 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
437 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
438  #define OTTD_ASSERT
439 #endif
440 
441 #if defined(OPENBSD)
442  /* OpenBSD uses strcasecmp(3) */
443  #define _stricmp strcasecmp
444 #endif
445 
446 #if defined(MAX_PATH)
447  /* It's already defined, no need to override */
448 #elif defined(PATH_MAX) && PATH_MAX > 0
449  /* Use the value from PATH_MAX, if it exists */
450  #define MAX_PATH PATH_MAX
451 #else
452  /* If all else fails, hardcode something :( */
453  #define MAX_PATH 260
454 #endif
455 
460 static inline void free(const void *ptr)
461 {
462  free(const_cast<void *>(ptr));
463 }
464 
469 #define MAX_UVALUE(type) ((type)~(type)0)
470 
471 #if defined(_MSC_VER) && !defined(_DEBUG)
472  #define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
473  #define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
474 #elif defined(__GNUC__) && !defined(_DEBUG)
475  #define HELPER0(x) #x
476  #define HELPER1(x) HELPER0(GCC diagnostic ignored x)
477  #define HELPER2(y) HELPER1(#y)
478 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
479  #define IGNORE_UNINITIALIZED_WARNING_START \
480  _Pragma("GCC diagnostic push") \
481  _Pragma(HELPER2(-Wuninitialized)) \
482  _Pragma(HELPER2(-Wmaybe-uninitialized))
483  #define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
484 #endif
485 #endif
486 
487 #ifndef IGNORE_UNINITIALIZED_WARNING_START
488  #define IGNORE_UNINITIALIZED_WARNING_START
489  #define IGNORE_UNINITIALIZED_WARNING_STOP
490 #endif
491 
492 #endif /* STDAFX_H */
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD&#39;s encoding from that of the local environment.
Definition: win32.cpp:558
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
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:576
OSX is different on some places.