stdafx.h

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