OpenTTD Source: src/stdafx.h Source File

OpenTTD Source 13.2.1

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