stdafx.h

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