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