OpenTTD Source 15.0-beta2
stdafx.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10#ifndef STDAFX_H
11#define STDAFX_H
12
13#if defined(_WIN32)
14 /* Minimum supported version is Windows 7. */
15# define NTDDI_VERSION NTDDI_WIN7
16# define _WIN32_WINNT 0x0601 // _WIN32_WINNT_WIN7
17#endif
18
19#ifdef _MSC_VER
20 /* Stop Microsoft (and clang-cl) compilers from complaining about potentially-unsafe/potentially-non-standard functions */
21# define _CRT_SECURE_NO_DEPRECATE
22# define _CRT_SECURE_NO_WARNINGS
23# define _CRT_NONSTDC_NO_WARNINGS
24#endif
25
26#if defined(__APPLE__)
27# include "os/macosx/osx_stdafx.h"
28#else
29/* It seems that we need to include stdint.h before anything else
30 * We need INT64_MAX, which for most systems comes from stdint.h.
31 * For OSX the inclusion is already done in osx_stdafx.h. */
32# define __STDC_LIMIT_MACROS
33# include <stdint.h>
34#endif /* __APPLE__ */
35
36#if defined(__HAIKU__)
37# include <SupportDefs.h>
38# include <unistd.h>
39# define _DEFAULT_SOURCE
40# define _GNU_SOURCE
41#endif
42
43#include <algorithm>
44#include <array>
45#include <bit>
46#include <cassert>
47#include <cctype>
48#include <cerrno>
49#include <climits>
50#include <cmath>
51#include <cstddef>
52#include <cstdint>
53#include <cstdio>
54#include <cstring>
55#include <cstdlib>
56#include <cwchar>
57#include <deque>
58#include <exception>
59#include <functional>
60#include <iterator>
61#include <list>
62#include <limits>
63#include <map>
64#include <memory>
65#include <numeric>
66#include <optional>
67#include <set>
68#include <source_location>
69#include <span>
70#include <stdexcept>
71#include <string>
72#include <type_traits>
73#include <variant>
74#include <vector>
75
76#if defined(UNIX) || defined(__MINGW32__)
77# include <sys/types.h>
78#endif
79
80/* Stuff for GCC */
81#if defined(__GNUC__) || (defined(__clang__) && !defined(_MSC_VER))
82# define CDECL
83#endif /* __GNUC__ || __clang__ */
84
85#if defined(_WIN32)
86# define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
87#endif
88
89#if defined(_MSC_VER)
90 // See https://learn.microsoft.com/en-us/cpp/cpp/empty-bases?view=msvc-170
91# define EMPTY_BASES __declspec(empty_bases)
92#else
93# define EMPTY_BASES
94#endif
95
96/* Stuff for MSVC */
97#if defined(_MSC_VER)
98# pragma once
99# define NOMINMAX // Disable min/max macros in windows.h.
100
101# pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
102# pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
103# pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
104# pragma warning(disable: 4355) // 'this' : used in base member initializer list
105
106# 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)
107# pragma warning(disable: 4996) // 'function': was declared deprecated
108# 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
109# pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
110# pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
111# pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
112# 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 ...
113
114# define CDECL _cdecl
115
116# if defined(_WIN32) && !defined(_WIN64)
117# if !defined(_W64)
118# define _W64
119# endif
120
121 typedef _W64 int INT_PTR, *PINT_PTR;
122 typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
123# endif /* _WIN32 && !_WIN64 */
124
125# if defined(_WIN64)
126# define fseek _fseeki64
127# endif /* _WIN64 */
128
129 /* zlib from vcpkg use cdecl calling convention without enforcing it in the headers */
130# if defined(WITH_ZLIB)
131# if !defined(ZEXPORT)
132# define ZEXPORT CDECL
133# endif
134# endif
135
136 /* freetype from vcpkg use cdecl calling convention without enforcing it in the headers */
137# if defined(WITH_FREETYPE)
138# if !defined(FT_EXPORT)
139# define FT_EXPORT( x ) extern "C" x CDECL
140# endif
141# endif
142
143 /* liblzma from vcpkg (before 5.2.4-2) used to patch lzma.h to define LZMA_API_STATIC for static builds */
144# if defined(WITH_LIBLZMA)
145# if !defined(LZMA_API_STATIC)
146# define LZMA_API_STATIC
147# endif
148# endif
149
150 /* MSVC doesn't have these :( */
151# define S_ISDIR(mode) (mode & S_IFDIR)
152# define S_ISREG(mode) (mode & S_IFREG)
153
154#endif /* defined(_MSC_VER) */
155
156#if !defined(STRGEN) && !defined(SETTINGSGEN)
157# if defined(_WIN32)
158 char *getcwd(char *buf, size_t size);
159
160 std::string FS2OTTD(const std::wstring &name);
161 std::wstring OTTD2FS(const std::string &name);
162# elif defined(WITH_ICONV)
163 std::string FS2OTTD(const std::string &name);
164 std::string OTTD2FS(const std::string &name);
165# else
166 template <typename T> std::string FS2OTTD(T name) { return name; }
167 template <typename T> std::string OTTD2FS(T name) { return name; }
168# endif /* _WIN32 or WITH_ICONV */
169#endif /* STRGEN || SETTINGSGEN */
170
171#if defined(_WIN32)
172# define PATHSEP "\\"
173# define PATHSEPCHAR '\\'
174#else
175# define PATHSEP "/"
176# define PATHSEPCHAR '/'
177#endif
178
179#if defined(_MSC_VER)
180# define PACK_N(type_dec, n) __pragma(pack(push, n)) type_dec; __pragma(pack(pop))
181#elif defined(__MINGW32__)
182# define PRAGMA(x) _Pragma(#x)
183# define PACK_N(type_dec, n) PRAGMA(pack(push, n)) type_dec; PRAGMA(pack(pop))
184#else
185# define PACK_N(type_dec, n) type_dec __attribute__((__packed__, aligned(n)))
186#endif
187#define PACK(type_dec) PACK_N(type_dec, 1)
188
189/*
190 * When making a (pure) debug build, the compiler will by default disable
191 * inlining of functions. This has a detrimental effect on the performance of
192 * debug builds, especially when more and more trivial (wrapper) functions get
193 * added to the code base.
194 * Take for example the savegame called "Wentbourne", when running this game
195 * for 100 ticks with the null video driver a number of fairly trivial
196 * functions show up on top. The most common one is the implicit conversion
197 * operator of TileIndex to unsigned int, which takes up over 5% of the total
198 * run time and functionally does absolutely nothing. The remaining functions
199 * for the top 5 are GB, GetTileType, Map::Size and IsTileType to a total of
200 * about 12.5% of the game's total run time.
201 * It is possible to still force inlining in the most commonly used compilers,
202 * but that is at the cost of some problems with debugging due to the forced
203 * inlining. However, the performance benefit can be enormous; when forcing
204 * inlining for the previously mentioned top 5, the debug build ran about 15%
205 * quicker.
206 * The following debug_inline annotation may be added to functions comply
207 * with the following preconditions:
208 * 1: the function takes more than 0.5% of a profiled debug runtime
209 * 2: the function does not modify the game state
210 * 3: the function does not contain selection or iteration statements,
211 * i.e. no if, switch, for, do, while, etcetera.
212 * 4: the function is one line of code, excluding assertions.
213 * 5: the function is defined in a header file.
214 * The debug_inline annotation must be placed in front of the function, i.e.
215 * before the optional static or constexpr modifier.
216 */
217#if !defined(_DEBUG) || defined(NO_DEBUG_INLINE)
218/*
219 * Do not force inlining when not in debug. This way we do not work against
220 * any carefully designed compiler optimizations.
221 */
222#define debug_inline inline
223#elif defined(__clang__) || defined(__GNUC__)
224#define debug_inline [[gnu::always_inline]] inline
225#else
226/*
227 * MSVC explicitly disables inlining, even forced inlining, in debug builds
228 * so __forceinline makes no difference compared to inline. Other unknown
229 * compilers can also just fallback to a normal inline.
230 */
231#define debug_inline inline
232#endif
233
234/* This is already defined in unix, but not in QNX Neutrino (6.x) or Cygwin. */
235#if (!defined(UNIX) && !defined(__HAIKU__)) || defined(__QNXNTO__) || defined(__CYGWIN__)
236 typedef unsigned int uint;
237#endif
238
239#if !defined(WITH_PERSONAL_DIR)
240# define PERSONAL_DIR ""
241#endif
242
243/* Define the the platforms that use XDG */
244#if defined(WITH_PERSONAL_DIR) && defined(UNIX) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
245# define USE_XDG
246#endif
247
248/* Check if the types have the bitsizes like we are using them */
249static_assert(sizeof(uint64_t) == 8);
250static_assert(sizeof(uint32_t) == 4);
251static_assert(sizeof(uint16_t) == 2);
252static_assert(sizeof(uint8_t) == 1);
253static_assert(SIZE_MAX >= UINT32_MAX);
254
255#ifndef M_PI_2
256#define M_PI_2 1.57079632679489661923
257#define M_PI 3.14159265358979323846
258#endif /* M_PI_2 */
259
260template <typename T, size_t N>
261char (&ArraySizeHelper(T (&array)[N]))[N];
262
271#define lengthof(array) (sizeof(ArraySizeHelper(array)))
272
279#define cpp_sizeof(base, variable) (sizeof(std::declval<base>().variable))
280
281
282/* take care of some name clashes on MacOS */
283#if defined(__APPLE__)
284# define GetString OTTD_GetString
285# define DrawString OTTD_DrawString
286# define CloseConnection OTTD_CloseConnection
287#endif /* __APPLE__ */
288
289#if defined(__GNUC__) || defined(__clang__)
290# define GNU_TARGET(x) [[gnu::target(x)]]
291#else
292# define GNU_TARGET(x)
293#endif /* __GNUC__ || __clang__ */
294
295[[noreturn]] void NOT_REACHED(const std::source_location location = std::source_location::current());
296[[noreturn]] void AssertFailedError(const char *expression, const std::source_location location = std::source_location::current());
297
298/* For non-debug builds with assertions enabled use the special assertion handler. */
299#if defined(NDEBUG) && defined(WITH_ASSERT)
300# undef assert
301# define assert(expression) do { if (!(expression)) [[unlikely]] AssertFailedError(#expression); } while (false)
302#endif
303
304/* Define JSON_ASSERT, which is used by nlohmann-json. Otherwise the header-file
305 * will re-include assert.h, and reset the assert macro. */
306#define JSON_ASSERT(x) assert(x)
307
308#if defined(MAX_PATH)
309 /* It's already defined, no need to override */
310#elif defined(PATH_MAX) && PATH_MAX > 0
311 /* Use the value from PATH_MAX, if it exists */
312# define MAX_PATH PATH_MAX
313#else
314 /* If all else fails, hardcode something :( */
315# define MAX_PATH 260
316#endif
317
318#if defined(_MSC_VER) && !defined(_DEBUG)
319# define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
320# define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
321#elif defined(__GNUC__) && !defined(_DEBUG)
322# define HELPER0(x) #x
323# define HELPER1(x) HELPER0(GCC diagnostic ignored x)
324# define HELPER2(y) HELPER1(#y)
325#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
326# define IGNORE_UNINITIALIZED_WARNING_START \
327 _Pragma("GCC diagnostic push") \
328 _Pragma(HELPER2(-Wuninitialized)) \
329 _Pragma(HELPER2(-Wmaybe-uninitialized))
330# define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
331#endif
332#endif
333
334#ifndef IGNORE_UNINITIALIZED_WARNING_START
335# define IGNORE_UNINITIALIZED_WARNING_START
336# define IGNORE_UNINITIALIZED_WARNING_STOP
337#endif
338
339#endif /* STDAFX_H */
OSX is different on some places.
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:354
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:337