# stdafx.h

Go to the documentation of this file.

    00001 /* $Id: stdafx.h 25168 2013-04-08 20:52:32Z rubidium $ */
    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   /* Define a win32 target platform, to override defaults of the SDK
    00181    * We need to define NTDDI version for Vista SDK, but win2k is minimum */
    00182   #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
    00183   #define _WIN32_WINNT 0x0500       // Windows 2000
    00184   #define _WIN32_WINDOWS 0x400      // Windows 95
    00185   #if !defined(WINCE)
    00186     #define WINVER 0x0400     // Windows NT 4.0 / Windows 95
    00187   #endif
    00188   #define _WIN32_IE_ 0x0401         // 4.01 (win98 and NT4SP5+)
    00189   #define NOMINMAX                // Disable min/max macros in windows.h.
    00190
    00191   #pragma warning(disable: 4244)  // 'conversion' conversion from 'type1' to 'type2', possible loss of data
    00192   #pragma warning(disable: 4761)  // integral size mismatch in argument : conversion supplied
    00193   #pragma warning(disable: 4200)  // nonstandard extension used : zero-sized array in struct/union
    00194   #pragma warning(disable: 4355)  // 'this' : used in base member initializer list
    00195
    00196   #if (_MSC_VER < 1400)                   // MSVC 2005 safety checks
    00197     #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
    00198   #endif /* (_MSC_VER < 1400) */
    00199   #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)
    00200   #pragma warning(disable: 4996)   // 'function': was declared deprecated
    00201   #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
    00202   #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
    00203   #pragma warning(disable: 6011)   // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
    00204   #pragma warning(disable: 6326)   // code analyzer: potential comparison of a constant with another constant
    00205   #pragma warning(disable: 6031)   // code analyzer: Return value ignored: 'ReadFile'
    00206   #pragma warning(disable: 6255)   // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
    00207   #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 ...
    00208
    00209   #include <malloc.h> // alloca()
    00210   #define NORETURN __declspec(noreturn)
    00211   #define inline __forceinline
    00212
    00213   #if !defined(WINCE)
    00214     #define CDECL _cdecl
    00215   #endif
    00216
    00217   #define GCC_PACK
    00218   #define WARN_FORMAT(string, args)
    00219   #define FINAL sealed
    00220
    00221   int CDECL snprintf(char *str, size_t size, const char *format, ...) WARN_FORMAT(3, 4);
    00222   #if defined(WINCE)
    00223     int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
    00224   #endif
    00225
    00226   #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
    00227     #if !defined(_W64)
    00228       #define _W64
    00229     #endif
    00230
    00231     typedef _W64 int INT_PTR, *PINT_PTR;
    00232     typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
    00233   #endif /* WIN32 && !_WIN64 && !WIN64 */
    00234
    00235   #if defined(_WIN64) || defined(WIN64)
    00236     #define fseek _fseeki64
    00237   #endif /* _WIN64 || WIN64 */
    00238
    00239   /* This is needed to zlib uses the stdcall calling convention on visual studio */
    00240   #if defined(WITH_ZLIB) || defined(WITH_PNG)
    00241     #if !defined(ZLIB_WINAPI)
    00242       #define ZLIB_WINAPI
    00243     #endif
    00244   #endif
    00245
    00246   #if defined(WINCE)
    00247     #define strcasecmp _stricmp
    00248     #define strncasecmp _strnicmp
    00249     #undef DEBUG
    00250   #else
    00251     #define strcasecmp stricmp
    00252     #define strncasecmp strnicmp
    00253   #endif
    00254
    00255   /* MSVC doesn't have these :( */
    00256   #define S_ISDIR(mode) (mode & S_IFDIR)
    00257   #define S_ISREG(mode) (mode & S_IFREG)
    00258
    00259 #endif /* defined(_MSC_VER) */
    00260
    00261 #if defined(DOS)
    00262   /* The DOS port does not have all signals/signal functions. */
    00263   #define strsignal(sig) ""
    00264   /* Use 'no floating point' for bus errors; SIGBUS does not exist
    00265    * for DOS, SIGNOFP for other platforms. So it's fairly safe
    00266    * to interchange those. */
    00267   #define SIGBUS SIGNOFP
    00268 #endif
    00269
    00270 #if defined(WINCE)
    00271   #define strdup _strdup
    00272 #endif /* WINCE */
    00273
    00274 /* NOTE: the string returned by these functions is only valid until the next
    00275  * call to the same function and is not thread- or reentrancy-safe */
    00276 #if !defined(STRGEN) && !defined(SETTINGSGEN)
    00277   #if defined(WIN32) || defined(WIN64)
    00278     char *getcwd(char *buf, size_t size);
    00279     #include <tchar.h>
    00280     #include <io.h>
    00281
    00282     /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
    00283     #if !defined(WINCE)
    00284       namespace std { using ::_tfopen; }
    00285       #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
    00286       #define unlink(file) _tunlink(OTTD2FS(file))
    00287     #endif /* WINCE */
    00288
    00289     const char *FS2OTTD(const TCHAR *name);
    00290     const TCHAR *OTTD2FS(const char *name);
    00291     #define SQ2OTTD(name) FS2OTTD(name)
    00292     #define OTTD2SQ(name) OTTD2FS(name)
    00293   #else
    00294     #define fopen(file, mode) fopen(OTTD2FS(file), mode)
    00295     const char *FS2OTTD(const char *name);
    00296     const char *OTTD2FS(const char *name);
    00297     #define SQ2OTTD(name) (name)
    00298     #define OTTD2SQ(name) (name)
    00299   #endif /* WIN32 */
    00300 #endif /* STRGEN || SETTINGSGEN */
    00301
    00302 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
    00303   #define PATHSEP "\\"
    00304   #define PATHSEPCHAR '\\'
    00305 #else
    00306   #define PATHSEP "/"
    00307   #define PATHSEPCHAR '/'
    00308 #endif
    00309
    00310 /* MSVCRT of course has to have a different syntax for long long *sigh* */
    00311 #if defined(_MSC_VER) || defined(__MINGW32__)
    00312   #define OTTD_PRINTF64 "%I64d"
    00313   #define OTTD_PRINTFHEX64 "%I64x"
    00314   #define PRINTF_SIZE "%Iu"
    00315 #else
    00316   #define OTTD_PRINTF64 "%lld"
    00317   #define OTTD_PRINTFHEX64 "%llx"
    00318   #define PRINTF_SIZE "%zu"
    00319 #endif
    00320
    00321 typedef unsigned char byte;
    00322
    00323 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
    00324 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
    00325   typedef unsigned int uint;
    00326 #endif
    00327
    00328 #if defined(TROUBLED_INTS)
    00329   /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
    00330    * trouble all over the place in OpenTTD. */
    00331   #define uint32 uint32_ugly_hack
    00332   #define int32 int32_ugly_hack
    00333   typedef unsigned int uint32_ugly_hack;
    00334   typedef signed int int32_ugly_hack;
    00335 #else
    00336   typedef unsigned char    uint8;
    00337   typedef   signed char     int8;
    00338   typedef unsigned short   uint16;
    00339   typedef   signed short    int16;
    00340   typedef unsigned int     uint32;
    00341   typedef   signed int      int32;
    00342   typedef unsigned __int64 uint64;
    00343   typedef   signed __int64  int64;
    00344 #endif /* !TROUBLED_INTS */
    00345
    00346 #if !defined(WITH_PERSONAL_DIR)
    00347   #define PERSONAL_DIR ""
    00348 #endif
    00349
    00350 /* Compile time assertions. Prefer c++0x static_assert().
    00351  * Older compilers cannot evaluate some expressions at compile time,
    00352  * typically when templates are involved, try assert_tcompile() in those cases. */
    00353 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert)
    00354   /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */
    00355   #define assert_compile(expr) static_assert(expr, #expr )
    00356   #define assert_tcompile(expr) assert_compile(expr)
    00357 #elif defined(__OS2__)
    00358   /* Disabled for OS/2 */
    00359   #define assert_compile(expr)
    00360   #define assert_tcompile(expr) assert_compile(expr)
    00361 #else
    00362   #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
    00363   #define assert_tcompile(expr) assert(expr)
    00364 #endif
    00365
    00366 /* Check if the types have the bitsizes like we are using them */
    00367 assert_compile(sizeof(uint64) == 8);
    00368 assert_compile(sizeof(uint32) == 4);
    00369 assert_compile(sizeof(uint16) == 2);
    00370 assert_compile(sizeof(uint8)  == 1);
    00371 assert_compile(SIZE_MAX >= UINT32_MAX);
    00372
    00373 #ifndef M_PI_2
    00374 #define M_PI_2 1.57079632679489661923
    00375 #define M_PI   3.14159265358979323846
    00376 #endif /* M_PI_2 */
    00377
    00386 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
    00387
    00394 #define endof(x) (&x[lengthof(x)])
    00395
    00402 #define lastof(x) (&x[lengthof(x) - 1])
    00403
    00404 #define cpp_offsetof(s, m)   (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
    00405 #if !defined(offsetof)
    00406   #define offsetof(s, m) cpp_offsetof(s, m)
    00407 #endif /* offsetof */
    00408
    00415 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
    00416
    00423 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
    00424
    00425
    00426 /* take care of some name clashes on MacOS */
    00427 #if defined(__APPLE__)
    00428   #define GetString OTTD_GetString
    00429   #define DrawString OTTD_DrawString
    00430   #define CloseConnection OTTD_CloseConnection
    00431 #endif /* __APPLE__ */
    00432
    00433 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
    00434 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
    00435 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
    00436
    00437 /* For non-debug builds with assertions enabled use the special assertion handler:
    00438  * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
    00439  * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
    00440  */
    00441 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
    00442   #undef assert
    00443   #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
    00444 #endif
    00445
    00446 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
    00447 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
    00448   #define OTTD_ASSERT
    00449 #endif
    00450
    00451 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
    00452   /* MorphOS and NDS don't have C++ conformant _stricmp... */
    00453   #define _stricmp stricmp
    00454 #elif defined(OPENBSD)
    00455   /* OpenBSD uses strcasecmp(3) */
    00456   #define _stricmp strcasecmp
    00457 #endif
    00458
    00459 #if defined(MAX_PATH)
    00460   /* It's already defined, no need to override */
    00461 #elif defined(PATH_MAX) && PATH_MAX > 0
    00462   /* Use the value from PATH_MAX, if it exists */
    00463   #define MAX_PATH PATH_MAX
    00464 #else
    00465   /* If all else fails, hardcode something :( */
    00466   #define MAX_PATH 260
    00467 #endif
    00468
    00473 static inline void free(const void *ptr)
    00474 {
    00475   free(const_cast<void *>(ptr));
    00476 }
    00477
    00482 #define MAX_UVALUE(type) ((type)~(type)0)
    00483
    00484 #endif /* STDAFX_H */