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/>.
10 /** @file stdafx.h Definition of base types and functions in a cross-platform compatible way. */
15 #if defined(__APPLE__)
16 #include "os/macosx/osx_stdafx.h"
17 #endif /* __APPLE__ */
19 #if defined(__BEOS__) || defined(__HAIKU__)
20 #include <SupportDefs.h>
25 #elif defined(__NDS__)
26 #include <nds/jtypes.h>
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__)
36 /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything
37 * stdint.h defines and we need. */
40 #define __STDC_LIMIT_MACROS
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)
49 #if !defined(INT64_MAX)
50 #define INT64_MAX (9223372036854775807LL)
52 #if !defined(INT64_MIN)
53 #define INT64_MIN (-INT64_MAX - 1)
55 #if !defined(UINT32_MAX)
56 #define UINT32_MAX (4294967295U)
58 #if !defined(INT32_MAX)
59 #define INT32_MAX (2147483647)
61 #if !defined(INT32_MIN)
62 #define INT32_MIN (-INT32_MAX - 1)
64 #if !defined(UINT16_MAX)
65 #define UINT16_MAX (65535U)
67 #if !defined(INT16_MAX)
68 #define INT16_MAX (32767)
70 #if !defined(INT16_MIN)
71 #define INT16_MIN (-INT16_MAX - 1)
73 #if !defined(UINT8_MAX)
74 #define UINT8_MAX (255)
76 #if !defined(INT8_MAX)
77 #define INT8_MAX (127)
79 #if !defined(INT8_MIN)
80 #define INT8_MIN (-INT8_MAX - 1)
91 #define SIZE_MAX ((size_t)-1)
94 #if defined(UNIX) || defined(__MINGW32__)
95 #include <sys/types.h>
100 #define strcasecmp stricmp
104 #include <psptypes.h>
105 #include <pspdebug.h>
106 #include <pspthreadman.h>
109 #if defined(SUNOS) || defined(HPUX)
113 #if defined(__MORPHOS__)
114 /* MorphOS defines certain Amiga defines per default, we undefine them
115 * here to make the rest of source less messy and more clear what is
116 * required for morphos and what for AmigaOS */
120 #if defined(__amigaos__)
123 #if defined(__AMIGA__)
132 /* Act like we already included this file, as it somehow gives linkage problems
133 * (mismatch linkage of C++ and C between this include and unistd.h). */
134 #define CLIB_USERGROUP_PROTOS_H
135 #endif /* __MORPHOS__ */
138 /* PSP can only have 10 file-descriptors open at any given time, but this
139 * switch only limits reads via the Fio system. So keep 2 fds free for things
140 * like saving a game. */
141 #define LIMITED_FDS 8
142 #define printf pspDebugScreenPrintf
146 #if defined(__GNUC__)
147 #define NORETURN __attribute__ ((noreturn))
149 #define __int64 long long
150 #define GCC_PACK __attribute__((packed))
151 /* Warn about functions using 'printf' format syntax. First argument determines which parameter
152 * is the format string, second argument is start of values passed to printf. */
153 #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
154 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
156 #define OVERRIDE override
157 #define FINAL_OVERRIDE final override
158 #define DELETED = delete
159 #define CONSTEXPR constexpr
163 #define FINAL_OVERRIDE
164 #define DELETED { NOT_REACHED(); }
168 /* Use fallthrough attribute where supported */
170 #if __cplusplus > 201402L // C++17
171 #define FALLTHROUGH [[fallthrough]]
173 #define FALLTHROUGH __attribute__((fallthrough))
178 #endif /* __GNUC__ */
180 #if defined(__WATCOMC__)
184 #define WARN_FORMAT(string, args)
187 #define FINAL_OVERRIDE
188 #define DELETED { NOT_REACHED(); }
192 #endif /* __WATCOMC__ */
194 #if defined(__MINGW32__) || defined(__CYGWIN__)
195 #include <malloc.h> // alloca()
199 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
203 #if defined(_MSC_VER)
206 /* No 64-bit Windows below XP, so we can safely assume it as the target platform. */
207 #define NTDDI_VERSION NTDDI_WINXP // Windows XP
208 #define _WIN32_WINNT 0x501 // Windows XP
209 #define _WIN32_WINDOWS 0x501 // Windows XP
210 #define WINVER 0x0501 // Windows XP
211 #define _WIN32_IE_ 0x0600 // 6.0 (XP+)
213 /* Define a win32 target platform, to override defaults of the SDK
214 * We need to define NTDDI version for Vista SDK, but win2k is minimum */
215 #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
216 #define _WIN32_WINNT 0x0500 // Windows 2000
217 #define _WIN32_WINDOWS 0x400 // Windows 95
219 #define WINVER 0x0400 // Windows NT 4.0 / Windows 95
221 #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+)
223 #define NOMINMAX // Disable min/max macros in windows.h.
225 #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
226 #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
227 #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
228 #pragma warning(disable: 4355) // 'this' : used in base member initializer list
230 #if (_MSC_VER < 1400) // MSVC 2005 safety checks
231 #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
232 #endif /* (_MSC_VER < 1400) */
233 #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)
234 #pragma warning(disable: 4996) // 'function': was declared deprecated
235 #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
236 #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
237 #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
238 #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
239 #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
240 #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
241 #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 ...
243 #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
244 #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
248 #include <malloc.h> // alloca()
249 #define NORETURN __declspec(noreturn)
250 #if (_MSC_VER < 1900)
251 #define inline __forceinline
259 #define WARN_FORMAT(string, args)
262 #define FINAL_OVERRIDE
263 #define DELETED { NOT_REACHED(); }
266 /* fallthrough attribute, VS 2017 */
267 #if (_MSC_VER >= 1910)
268 #define FALLTHROUGH [[fallthrough]]
273 int CDECL
snprintf(char *str
, size_t size
, const char *format
, ...) WARN_FORMAT(3, 4);
275 int CDECL
vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
278 #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
283 typedef _W64
int INT_PTR
, *PINT_PTR
;
284 typedef _W64
unsigned int UINT_PTR
, *PUINT_PTR
;
285 #endif /* WIN32 && !_WIN64 && !WIN64 */
287 #if defined(_WIN64) || defined(WIN64)
288 #define fseek _fseeki64
289 #endif /* _WIN64 || WIN64 */
291 /* This is needed to zlib uses the stdcall calling convention on visual studio */
292 #if defined(WITH_ZLIB) || defined(WITH_PNG)
293 #if !defined(ZLIB_WINAPI)
299 #define strcasecmp _stricmp
300 #define strncasecmp _strnicmp
303 #define strcasecmp stricmp
304 #define strncasecmp strnicmp
307 #define strtoull _strtoui64
309 /* MSVC doesn't have these :( */
310 #define S_ISDIR(mode) (mode & S_IFDIR)
311 #define S_ISREG(mode) (mode & S_IFREG)
313 #endif /* defined(_MSC_VER) */
316 /* The DOS port does not have all signals/signal functions. */
317 #define strsignal(sig) ""
318 /* Use 'no floating point' for bus errors; SIGBUS does not exist
319 * for DOS, SIGNOFP for other platforms. So it's fairly safe
320 * to interchange those. */
321 #define SIGBUS SIGNOFP
325 #define strdup _strdup
328 /* NOTE: the string returned by these functions is only valid until the next
329 * call to the same function and is not thread- or reentrancy-safe */
330 #if !defined(STRGEN) && !defined(SETTINGSGEN)
331 #if defined(WIN32) || defined(WIN64)
332 char *getcwd(char *buf
, size_t size
);
336 const char *FS2OTTD(const TCHAR
*name
);
337 const TCHAR
*OTTD2FS(const char *name
, bool console_cp
= false);
339 /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
341 namespace std
{ using ::_tfopen
; }
342 #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
343 static inline int ttd_unlink(const char *file
)
345 return _tunlink(OTTD2FS(file
));
347 #define unlink ttd_unlink
350 #define fopen(file, mode) fopen(OTTD2FS(file), mode)
351 const char *FS2OTTD(const char *name
);
352 const char *OTTD2FS(const char *name
);
354 #endif /* STRGEN || SETTINGSGEN */
356 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
358 #define PATHSEPCHAR '\\'
361 #define PATHSEPCHAR '/'
364 /* MSVCRT of course has to have a different syntax for long long *sigh* */
365 #if defined(_MSC_VER) || defined(__MINGW32__)
366 #define OTTD_PRINTF64 "%I64d"
367 #define OTTD_PRINTFHEX64 "%I64x"
368 #define PRINTF_SIZE "%Iu"
370 #define OTTD_PRINTF64 "%lld"
371 #define OTTD_PRINTFHEX64 "%llx"
372 #define PRINTF_SIZE "%zu"
375 typedef unsigned char byte
;
377 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
378 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
379 typedef unsigned int uint
;
382 #if defined(TROUBLED_INTS)
383 /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
384 * trouble all over the place in OpenTTD. */
385 #define uint32 uint32_ugly_hack
386 #define int32 int32_ugly_hack
387 typedef unsigned int uint32_ugly_hack
;
388 typedef signed int int32_ugly_hack
;
390 typedef unsigned char uint8
;
391 typedef signed char int8
;
392 typedef unsigned short uint16
;
393 typedef signed short int16
;
394 typedef unsigned int uint32
;
395 typedef signed int int32
;
396 typedef unsigned __int64 uint64
;
397 typedef signed __int64 int64
;
398 #endif /* !TROUBLED_INTS */
400 #if !defined(WITH_PERSONAL_DIR)
401 #define PERSONAL_DIR ""
404 /* Compile time assertions. Prefer c++0x static_assert().
405 * Older compilers cannot evaluate some expressions at compile time,
406 * typically when templates are involved, try assert_tcompile() in those cases. */
407 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert) || (defined(_MSC_VER) && _MSC_VER >= 1600)
408 /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */
409 #define assert_compile(expr) static_assert(expr, #expr )
410 #define assert_tcompile(expr) assert_compile(expr)
411 #elif defined(__OS2__)
412 /* Disabled for OS/2 */
413 #define assert_compile(expr)
414 #define assert_tcompile(expr) assert_compile(expr)
416 #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
417 #define assert_tcompile(expr) assert(expr)
420 /* Check if the types have the bitsizes like we are using them */
421 assert_compile(sizeof(uint64
) == 8);
422 assert_compile(sizeof(uint32
) == 4);
423 assert_compile(sizeof(uint16
) == 2);
424 assert_compile(sizeof(uint8
) == 1);
425 assert_compile(SIZE_MAX
>= UINT32_MAX
);
428 #define M_PI_2 1.57079632679489661923
429 #define M_PI 3.14159265358979323846
433 * Return the length of an fixed size array.
434 * Unlike sizeof this function returns the number of elements
437 * @param x The pointer to the first element of the array
438 * @return The number of elements
440 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
443 * Get the end element of an fixed size array.
445 * @param x The pointer to the first element of the array
446 * @return The pointer past to the last element of the array
448 #define endof(x) (&x[lengthof(x)])
451 * Get the last element of an fixed size array.
453 * @param x The pointer to the first element of the array
454 * @return The pointer to the last element of the array
456 #define lastof(x) (&x[lengthof(x) - 1])
458 #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
459 #if !defined(offsetof)
460 #define offsetof(s, m) cpp_offsetof(s, m)
461 #endif /* offsetof */
464 * Gets the size of a variable within a class.
465 * @param base The class the variable is in.
466 * @param variable The variable to get the size of.
467 * @return the size of the variable
469 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
472 * Gets the length of an array variable within a class.
473 * @param base The class the variable is in.
474 * @param variable The array variable to get the size of.
475 * @return the length of the array
477 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
480 * Construct a pointer to a field in a (nonexistent) object of a given type.
481 * Since the object does not exist, you should only use this pointer for its
482 * type information or, if it points to a class, to access its static members.
483 * @param base The struct that contains the field.
484 * @param variable The field in the struct.
485 * @return A pointer to the field in a nonexistent object.
487 #define cpp_pointer(base, variable) (&((base*)(char*)8)->variable)
490 /* take care of some name clashes on MacOS */
491 #if defined(__APPLE__)
492 #define GetString OTTD_GetString
493 #define DrawString OTTD_DrawString
494 #define CloseConnection OTTD_CloseConnection
495 #endif /* __APPLE__ */
497 void NORETURN CDECL
usererror(const char *str
, ...) WARN_FORMAT(1, 2);
498 void NORETURN CDECL
error(const char *str
, ...) WARN_FORMAT(1, 2);
499 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
501 /* For non-debug builds with assertions enabled use the special assertion handler:
502 * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
503 * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
505 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
507 #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
510 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
511 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
515 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
516 /* MorphOS and NDS don't have C++ conformant _stricmp... */
517 #define _stricmp stricmp
518 #elif defined(OPENBSD)
519 /* OpenBSD uses strcasecmp(3) */
520 #define _stricmp strcasecmp
523 #if defined(MAX_PATH)
524 /* It's already defined, no need to override */
525 #elif defined(PATH_MAX) && PATH_MAX > 0
526 /* Use the value from PATH_MAX, if it exists */
527 #define MAX_PATH PATH_MAX
529 /* If all else fails, hardcode something :( */
534 * Version of the standard free that accepts const pointers.
535 * @param ptr The data to free.
537 static inline void free(const void *ptr
)
539 free(const_cast<void *>(ptr
));
543 * The largest value that can be entered in a variable
544 * @param type the type of the variable
546 #define MAX_UVALUE(type) ((type)~(type)0)
548 #if defined(_MSC_VER) && !defined(_DEBUG)
549 #define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
550 #define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
551 #elif defined(__GNUC__) && !defined(_DEBUG)
552 #define HELPER0(x) #x
553 #define HELPER1(x) HELPER0(GCC diagnostic ignored x)
554 #define HELPER2(y) HELPER1(#y)
555 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
556 #define IGNORE_UNINITIALIZED_WARNING_START \
557 _Pragma("GCC diagnostic push") \
558 _Pragma(HELPER2(-Wuninitialized)) \
559 _Pragma(HELPER2(-Wmaybe-uninitialized))
560 #define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
564 #ifndef IGNORE_UNINITIALIZED_WARNING_START
565 #define IGNORE_UNINITIALIZED_WARNING_START
566 #define IGNORE_UNINITIALIZED_WARNING_STOP
569 #endif /* STDAFX_H */