# stdafx.h

Go to the documentation of this file.

    00001 /* $Id: stdafx.h 26226 2014-01-05 10:15:57Z zuu $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef STDAFX_H
    00013 #define STDAFX_H
    00014
    00015 #if defined(__APPLE__)
    00016   #include "os/macosx/osx_stdafx.h"
    00017 #endif /* __APPLE__ */
    00018
    00019 #if defined(__BEOS__) || defined(__HAIKU__)
    00020   #include <SupportDefs.h>
    00021   #include <unistd.h>
    00022   #define _GNU_SOURCE
    00023   #define TROUBLED_INTS
    00024 #elif defined(__NDS__)
    00025   #include <nds/jtypes.h>
    00026   #define TROUBLED_INTS
    00027 #endif
    00028
    00029 /* It seems that we need to include stdint.h before anything else
    00030  * We need INT64_MAX, which for most systems comes from stdint.h. However, MSVC
    00031  * does not have stdint.h and apparently neither does MorphOS.
    00032  * For OSX the inclusion is already done in osx_stdafx.h. */
    00033 #if !defined(__APPLE__) && (!defined(_MSC_VER) || _MSC_VER >= 1600) && !defined(__MORPHOS__)
    00034   #if defined(SUNOS)
    00035     /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything
    00036      * stdint.h defines and we need. */
    00037     #include <inttypes.h>
    00038   #else
    00039     #define __STDC_LIMIT_MACROS
    00040     #include <stdint.h>
    00041   #endif
    00042 #endif
    00043
    00044 /* The conditions for these constants to be available are way too messy; so check them one by one */
    00045 #if !defined(UINT64_MAX)
    00046   #define UINT64_MAX (18446744073709551615ULL)
    00047 #endif
    00048 #if !defined(INT64_MAX)
    00049   #define INT64_MAX  (9223372036854775807LL)
    00050 #endif
    00051 #if !defined(INT64_MIN)
    00052   #define INT64_MIN  (-INT64_MAX - 1)
    00053 #endif
    00054 #if !defined(UINT32_MAX)
    00055   #define UINT32_MAX (4294967295U)
    00056 #endif
    00057 #if !defined(INT32_MAX)
    00058   #define INT32_MAX  (2147483647)
    00059 #endif
    00060 #if !defined(INT32_MIN)
    00061   #define INT32_MIN  (-INT32_MAX - 1)
    00062 #endif
    00063 #if !defined(UINT16_MAX)
    00064   #define UINT16_MAX (65535U)
    00065 #endif
    00066 #if !defined(INT16_MAX)
    00067   #define INT16_MAX  (32767)
    00068 #endif
    00069 #if !defined(INT16_MIN)
    00070   #define INT16_MIN  (-INT16_MAX - 1)
    00071 #endif
    00072 #if !defined(UINT8_MAX)
    00073   #define UINT8_MAX  (255)
    00074 #endif
    00075 #if !defined(INT8_MAX)
    00076   #define INT8_MAX   (127)
    00077 #endif
    00078 #if !defined(INT8_MIN)
    00079   #define INT8_MIN   (-INT8_MAX - 1)
    00080 #endif
    00081
    00082 #include <cstdio>
    00083 #include <cstddef>
    00084 #include <cstring>
    00085 #include <cstdlib>
    00086 #include <climits>
    00087 #include <cassert>
    00088
    00089 #ifndef SIZE_MAX
    00090   #define SIZE_MAX ((size_t)-1)
    00091 #endif
    00092
    00093 #if defined(UNIX) || defined(__MINGW32__)
    00094   #include <sys/types.h>
    00095 #endif
    00096
    00097 #if defined(__OS2__)
    00098   #include <types.h>
    00099   #define strcasecmp stricmp
    00100 #endif
    00101
    00102 #if defined(PSP)
    00103   #include <psptypes.h>
    00104   #include <pspdebug.h>
    00105   #include <pspthreadman.h>
    00106 #endif
    00107
    00108 #if defined(SUNOS) || defined(HPUX)
    00109   #include <alloca.h>
    00110 #endif
    00111
    00112 #if defined(__MORPHOS__)
    00113   /* MorphOS defines certain Amiga defines per default, we undefine them
    00114    * here to make the rest of source less messy and more clear what is
    00115    * required for morphos and what for AmigaOS */
    00116   #if defined(amigaos)
    00117     #undef amigaos
    00118   #endif
    00119   #if defined(__amigaos__)
    00120     #undef __amigaos__
    00121   # endif
    00122   #if defined(__AMIGA__)
    00123     #undef __AMIGA__
    00124   #endif
    00125   #if defined(AMIGA)
    00126     #undef AMIGA
    00127   #endif
    00128   #if defined(amiga)
    00129     #undef amiga
    00130   #endif
    00131   /* Act like we already included this file, as it somehow gives linkage problems
    00132    *  (mismatch linkage of C++ and C between this include and unistd.h). */
    00133   #define CLIB_USERGROUP_PROTOS_H
    00134 #endif /* __MORPHOS__ */
    00135
    00136 #if defined(PSP)
    00137   /* PSP can only have 10 file-descriptors open at any given time, but this
    00138    *  switch only limits reads via the Fio system. So keep 2 fds free for things
    00139    *  like saving a game. */
    00140   #define LIMITED_FDS 8
    00141   #define printf pspDebugScreenPrintf
    00142 #endif /* PSP */
    00143
    00144 /* Stuff for GCC */
    00145 #if defined(__GNUC__)
    00146   #define NORETURN __attribute__ ((noreturn))
    00147   #define CDECL
    00148   #define __int64 long long
    00149   #define GCC_PACK __attribute__((packed))
    00150   /* Warn about functions using 'printf' format syntax. First argument determines which parameter
    00151    * is the format string, second argument is start of values passed to printf. */
    00152   #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
    00153   #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
    00154     #define FINAL final
    00155   #else
    00156     #define FINAL
    00157   #endif
    00158 #endif /* __GNUC__ */
    00159
    00160 #if defined(__WATCOMC__)
    00161   #define NORETURN
    00162   #define CDECL
    00163   #define GCC_PACK
    00164   #define WARN_FORMAT(string, args)
    00165   #define FINAL
    00166   #include <malloc.h>
    00167 #endif /* __WATCOMC__ */
    00168
    00169 #if defined(__MINGW32__) || defined(__CYGWIN__)
    00170   #include <malloc.h> // alloca()
    00171 #endif
    00172
    00173 #if defined(WIN32)
    00174   #define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
    00175 #endif
    00176
    00177 /* Stuff for MSVC */
    00178 #if defined(_MSC_VER)
    00179   #pragma once
    00180   #ifdef _WIN64
    00181     /* No 64-bit Windows below XP, so we can safely assume it as the target platform. */
    00182     #define NTDDI_VERSION NTDDI_WINXP // Windows XP
    00183     #define _WIN32_WINNT 0x501        // Windows XP
    00184     #define _WIN32_WINDOWS 0x501      // Windows XP
    00185     #define WINVER 0x0501             // Windows XP
    00186     #define _WIN32_IE_ 0x0600         // 6.0 (XP+)
    00187   #else
    00188     /* Define a win32 target platform, to override defaults of the SDK
    00189      * We need to define NTDDI version for Vista SDK, but win2k is minimum */
    00190     #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
    00191     #define _WIN32_WINNT 0x0500       // Windows 2000
    00192     #define _WIN32_WINDOWS 0x400      // Windows 95
    00193     #if !defined(WINCE)
    00194       #define WINVER 0x0400     // Windows NT 4.0 / Windows 95
    00195     #endif
    00196     #define _WIN32_IE_ 0x0401         // 4.01 (win98 and NT4SP5+)
    00197   #endif
    00198   #define NOMINMAX                // Disable min/max macros in windows.h.
    00199
    00200   #pragma warning(disable: 4244)  // 'conversion' conversion from 'type1' to 'type2', possible loss of data
    00201   #pragma warning(disable: 4761)  // integral size mismatch in argument : conversion supplied
    00202   #pragma warning(disable: 4200)  // nonstandard extension used : zero-sized array in struct/union
    00203   #pragma warning(disable: 4355)  // 'this' : used in base member initializer list
    00204
    00205   #if (_MSC_VER < 1400)                   // MSVC 2005 safety checks
    00206     #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
    00207   #endif /* (_MSC_VER < 1400) */
    00208   #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)
    00209   #pragma warning(disable: 4996)   // 'function': was declared deprecated
    00210   #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
    00211   #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
    00212   #pragma warning(disable: 6011)   // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
    00213   #pragma warning(disable: 6326)   // code analyzer: potential comparison of a constant with another constant
    00214   #pragma warning(disable: 6031)   // code analyzer: Return value ignored: 'ReadFile'
    00215   #pragma warning(disable: 6255)   // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
    00216   #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 ...
    00217
    00218   #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
    00219     #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
    00220     #include <intrin.h>
    00221   #endif
    00222
    00223   #include <malloc.h> // alloca()
    00224   #define NORETURN __declspec(noreturn)
    00225   #define inline __forceinline
    00226
    00227   #if !defined(WINCE)
    00228     #define CDECL _cdecl
    00229   #endif
    00230
    00231   #define GCC_PACK
    00232   #define WARN_FORMAT(string, args)
    00233   #define FINAL sealed
    00234
    00235   int CDECL snprintf(char *str, size_t size, const char *format, ...) WARN_FORMAT(3, 4);
    00236   #if defined(WINCE)
    00237     int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
    00238   #endif
    00239
    00240   #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
    00241     #if !defined(_W64)
    00242       #define _W64
    00243     #endif
    00244
    00245     typedef _W64 int INT_PTR, *PINT_PTR;
    00246     typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
    00247   #endif /* WIN32 && !_WIN64 && !WIN64 */
    00248
    00249   #if defined(_WIN64) || defined(WIN64)
    00250     #define fseek _fseeki64
    00251   #endif /* _WIN64 || WIN64 */
    00252
    00253   /* This is needed to zlib uses the stdcall calling convention on visual studio */
    00254   #if defined(WITH_ZLIB) || defined(WITH_PNG)
    00255     #if !defined(ZLIB_WINAPI)
    00256       #define ZLIB_WINAPI
    00257     #endif
    00258   #endif
    00259
    00260   #if defined(WINCE)
    00261     #define strcasecmp _stricmp
    00262     #define strncasecmp _strnicmp
    00263     #undef DEBUG
    00264   #else
    00265     #define strcasecmp stricmp
    00266     #define strncasecmp strnicmp
    00267   #endif
    00268
    00269   /* MSVC doesn't have these :( */
    00270   #define S_ISDIR(mode) (mode & S_IFDIR)
    00271   #define S_ISREG(mode) (mode & S_IFREG)
    00272
    00273 #endif /* defined(_MSC_VER) */
    00274
    00275 #if defined(DOS)
    00276   /* The DOS port does not have all signals/signal functions. */
    00277   #define strsignal(sig) ""
    00278   /* Use 'no floating point' for bus errors; SIGBUS does not exist
    00279    * for DOS, SIGNOFP for other platforms. So it's fairly safe
    00280    * to interchange those. */
    00281   #define SIGBUS SIGNOFP
    00282 #endif
    00283
    00284 #if defined(WINCE)
    00285   #define strdup _strdup
    00286 #endif /* WINCE */
    00287
    00288 /* NOTE: the string returned by these functions is only valid until the next
    00289  * call to the same function and is not thread- or reentrancy-safe */
    00290 #if !defined(STRGEN) && !defined(SETTINGSGEN)
    00291   #if defined(WIN32) || defined(WIN64)
    00292     char *getcwd(char *buf, size_t size);
    00293     #include <tchar.h>
    00294     #include <io.h>
    00295
    00296     /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
    00297     #if !defined(WINCE)
    00298       namespace std { using ::_tfopen; }
    00299       #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
    00300       #define unlink(file) _tunlink(OTTD2FS(file))
    00301     #endif /* WINCE */
    00302
    00303     const char *FS2OTTD(const TCHAR *name);
    00304     const TCHAR *OTTD2FS(const char *name, bool console_cp = false);
    00305     #define SQ2OTTD(name) FS2OTTD(name)
    00306     #define OTTD2SQ(name) OTTD2FS(name)
    00307   #else
    00308     #define fopen(file, mode) fopen(OTTD2FS(file), mode)
    00309     const char *FS2OTTD(const char *name);
    00310     const char *OTTD2FS(const char *name);
    00311     #define SQ2OTTD(name) (name)
    00312     #define OTTD2SQ(name) (name)
    00313   #endif /* WIN32 */
    00314 #endif /* STRGEN || SETTINGSGEN */
    00315
    00316 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
    00317   #define PATHSEP "\\"
    00318   #define PATHSEPCHAR '\\'
    00319 #else
    00320   #define PATHSEP "/"
    00321   #define PATHSEPCHAR '/'
    00322 #endif
    00323
    00324 /* MSVCRT of course has to have a different syntax for long long *sigh* */
    00325 #if defined(_MSC_VER) || defined(__MINGW32__)
    00326   #define OTTD_PRINTF64 "%I64d"
    00327   #define OTTD_PRINTFHEX64 "%I64x"
    00328   #define PRINTF_SIZE "%Iu"
    00329 #else
    00330   #define OTTD_PRINTF64 "%lld"
    00331   #define OTTD_PRINTFHEX64 "%llx"
    00332   #define PRINTF_SIZE "%zu"
    00333 #endif
    00334
    00335 typedef unsigned char byte;
    00336
    00337 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
    00338 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
    00339   typedef unsigned int uint;
    00340 #endif
    00341
    00342 #if defined(TROUBLED_INTS)
    00343   /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
    00344    * trouble all over the place in OpenTTD. */
    00345   #define uint32 uint32_ugly_hack
    00346   #define int32 int32_ugly_hack
    00347   typedef unsigned int uint32_ugly_hack;
    00348   typedef signed int int32_ugly_hack;
    00349 #else
    00350   typedef unsigned char    uint8;
    00351   typedef   signed char     int8;
    00352   typedef unsigned short   uint16;
    00353   typedef   signed short    int16;
    00354   typedef unsigned int     uint32;
    00355   typedef   signed int      int32;
    00356   typedef unsigned __int64 uint64;
    00357   typedef   signed __int64  int64;
    00358 #endif /* !TROUBLED_INTS */
    00359
    00360 #if !defined(WITH_PERSONAL_DIR)
    00361   #define PERSONAL_DIR ""
    00362 #endif
    00363
    00364 /* Compile time assertions. Prefer c++0x static_assert().
    00365  * Older compilers cannot evaluate some expressions at compile time,
    00366  * typically when templates are involved, try assert_tcompile() in those cases. */
    00367 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert)
    00368   /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */
    00369   #define assert_compile(expr) static_assert(expr, #expr )
    00370   #define assert_tcompile(expr) assert_compile(expr)
    00371 #elif defined(__OS2__)
    00372   /* Disabled for OS/2 */
    00373   #define assert_compile(expr)
    00374   #define assert_tcompile(expr) assert_compile(expr)
    00375 #else
    00376   #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
    00377   #define assert_tcompile(expr) assert(expr)
    00378 #endif
    00379
    00380 /* Check if the types have the bitsizes like we are using them */
    00381 assert_compile(sizeof(uint64) == 8);
    00382 assert_compile(sizeof(uint32) == 4);
    00383 assert_compile(sizeof(uint16) == 2);
    00384 assert_compile(sizeof(uint8)  == 1);
    00385 assert_compile(SIZE_MAX >= UINT32_MAX);
    00386
    00387 #ifndef M_PI_2
    00388 #define M_PI_2 1.57079632679489661923
    00389 #define M_PI   3.14159265358979323846
    00390 #endif /* M_PI_2 */
    00391
    00400 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
    00401
    00408 #define endof(x) (&x[lengthof(x)])
    00409
    00416 #define lastof(x) (&x[lengthof(x) - 1])
    00417
    00418 #define cpp_offsetof(s, m)   (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
    00419 #if !defined(offsetof)
    00420   #define offsetof(s, m) cpp_offsetof(s, m)
    00421 #endif /* offsetof */
    00422
    00429 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
    00430
    00437 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
    00438
    00439
    00440 /* take care of some name clashes on MacOS */
    00441 #if defined(__APPLE__)
    00442   #define GetString OTTD_GetString
    00443   #define DrawString OTTD_DrawString
    00444   #define CloseConnection OTTD_CloseConnection
    00445 #endif /* __APPLE__ */
    00446
    00447 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
    00448 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
    00449 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
    00450
    00451 /* For non-debug builds with assertions enabled use the special assertion handler:
    00452  * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
    00453  * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
    00454  */
    00455 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
    00456   #undef assert
    00457   #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
    00458 #endif
    00459
    00460 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
    00461 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
    00462   #define OTTD_ASSERT
    00463 #endif
    00464
    00465 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
    00466   /* MorphOS and NDS don't have C++ conformant _stricmp... */
    00467   #define _stricmp stricmp
    00468 #elif defined(OPENBSD)
    00469   /* OpenBSD uses strcasecmp(3) */
    00470   #define _stricmp strcasecmp
    00471 #endif
    00472
    00473 #if defined(MAX_PATH)
    00474   /* It's already defined, no need to override */
    00475 #elif defined(PATH_MAX) && PATH_MAX > 0
    00476   /* Use the value from PATH_MAX, if it exists */
    00477   #define MAX_PATH PATH_MAX
    00478 #else
    00479   /* If all else fails, hardcode something :( */
    00480   #define MAX_PATH 260
    00481 #endif
    00482
    00487 static inline void free(const void *ptr)
    00488 {
    00489   free(const_cast<void *>(ptr));
    00490 }
    00491
    00496 #define MAX_UVALUE(type) ((type)~(type)0)
    00497
    00498 #if defined(_MSC_VER) && !defined(_DEBUG)
    00499   #define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
    00500   #define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
    00501 #elif defined(__GNUC__) && !defined(_DEBUG)
    00502   #define HELPER0(x) #x
    00503   #define HELPER1(x) HELPER0(GCC diagnostic ignored x)
    00504   #define HELPER2(y) HELPER1(#y)
    00505   #define IGNORE_UNINITIALIZED_WARNING_START \
    00506     _Pragma("GCC diagnostic push") \
    00507     _Pragma(HELPER2(-Wuninitialized)) \
    00508     _Pragma(HELPER2(-Wmaybe-uninitialized))
    00509   #define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
    00510 #else
    00511   #define IGNORE_UNINITIALIZED_WARNING_START
    00512   #define IGNORE_UNINITIALIZED_WARNING_STOP
    00513 #endif
    00514
    00515 #endif /* STDAFX_H */