OpenTTD Source 20250205-master-gfd85ab1e2c
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 __GNUC__ > 11 || (__GNUC__ == 11 && __GNUC_MINOR__ >= 1)
86# define NOACCESS(args) __attribute__ ((access (none, args)))
87#else
88# define NOACCESS(args)
89#endif
90
91#if defined(_WIN32)
92# define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
93#endif
94
95#if defined(_MSC_VER)
96 // See https://learn.microsoft.com/en-us/cpp/cpp/empty-bases?view=msvc-170
97# define EMPTY_BASES __declspec(empty_bases)
98#else
99# define EMPTY_BASES
100#endif
101
102/* Stuff for MSVC */
103#if defined(_MSC_VER)
104# pragma once
105# define NOMINMAX // Disable min/max macros in windows.h.
106
107# pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
108# pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
109# pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
110# pragma warning(disable: 4355) // 'this' : used in base member initializer list
111
112# 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)
113# pragma warning(disable: 4996) // 'function': was declared deprecated
114# 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
115# pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
116# pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
117# pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
118# 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 ...
119
120# define CDECL _cdecl
121
122# if defined(_WIN32) && !defined(_WIN64)
123# if !defined(_W64)
124# define _W64
125# endif
126
127 typedef _W64 int INT_PTR, *PINT_PTR;
128 typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
129# endif /* _WIN32 && !_WIN64 */
130
131# if defined(_WIN64)
132# define fseek _fseeki64
133# endif /* _WIN64 */
134
135 /* zlib from vcpkg use cdecl calling convention without enforcing it in the headers */
136# if defined(WITH_ZLIB)
137# if !defined(ZEXPORT)
138# define ZEXPORT CDECL
139# endif
140# endif
141
142 /* freetype from vcpkg use cdecl calling convention without enforcing it in the headers */
143# if defined(WITH_FREETYPE)
144# if !defined(FT_EXPORT)
145# define FT_EXPORT( x ) extern "C" x CDECL
146# endif
147# endif
148
149 /* liblzma from vcpkg (before 5.2.4-2) used to patch lzma.h to define LZMA_API_STATIC for static builds */
150# if defined(WITH_LIBLZMA)
151# if !defined(LZMA_API_STATIC)
152# define LZMA_API_STATIC
153# endif
154# endif
155
156 /* MSVC doesn't have these :( */
157# define S_ISDIR(mode) (mode & S_IFDIR)
158# define S_ISREG(mode) (mode & S_IFREG)
159
160#endif /* defined(_MSC_VER) */
161
162#if !defined(STRGEN) && !defined(SETTINGSGEN)
163# if defined(_WIN32)
164 char *getcwd(char *buf, size_t size);
165
166 std::string FS2OTTD(const std::wstring &name);
167 std::wstring OTTD2FS(const std::string &name);
168# elif defined(WITH_ICONV)
169 std::string FS2OTTD(const std::string &name);
170 std::string OTTD2FS(const std::string &name);
171# else
172 template <typename T> std::string FS2OTTD(T name) { return name; }
173 template <typename T> std::string OTTD2FS(T name) { return name; }
174# endif /* _WIN32 or WITH_ICONV */
175#endif /* STRGEN || SETTINGSGEN */
176
177#if defined(_WIN32)
178# define PATHSEP "\\"
179# define PATHSEPCHAR '\\'
180#else
181# define PATHSEP "/"
182# define PATHSEPCHAR '/'
183#endif
184
185#if defined(_MSC_VER)
186# define PACK_N(type_dec, n) __pragma(pack(push, n)) type_dec; __pragma(pack(pop))
187#elif defined(__MINGW32__)
188# define PRAGMA(x) _Pragma(#x)
189# define PACK_N(type_dec, n) PRAGMA(pack(push, n)) type_dec; PRAGMA(pack(pop))
190#else
191# define PACK_N(type_dec, n) type_dec __attribute__((__packed__, aligned(n)))
192#endif
193#define PACK(type_dec) PACK_N(type_dec, 1)
194
195/*
196 * When making a (pure) debug build, the compiler will by default disable
197 * inlining of functions. This has a detremental effect on the performance of
198 * debug builds, especially when more and more trivial (wrapper) functions get
199 * added to the code base.
200 * Take for example the savegame called "Wentbourne", when running this game
201 * for 100 ticks with the null video driver a number of fairly trivial
202 * functions show up on top. The most common one is the implicit conversion
203 * operator of TileIndex to unsigned int, which takes up over 5% of the total
204 * run time and functionally does absolutely nothing. The remaining functions
205 * for the top 5 are GB, GetTileType, Map::Size and IsTileType to a total of
206 * about 12.5% of the game's total run time.
207 * It is possible to still force inlining in the most commonly used compilers,
208 * but that is at the cost of some problems with debugging due to the forced
209 * inlining. However, the performance benefit can be enormous; when forcing
210 * inlining for the previously mentioned top 5, the debug build ran about 15%
211 * quicker.
212 * The following debug_inline annotation may be added to functions comply
213 * with the following preconditions:
214 * 1: the function takes more than 0.5% of a profiled debug runtime
215 * 2: the function does not modify the game state
216 * 3: the function does not contain selection or iteration statements,
217 * i.e. no if, switch, for, do, while, etcetera.
218 * 4: the function is one line of code, excluding assertions.
219 * 5: the function is defined in a header file.
220 * The debug_inline annotation must be placed in front of the function, i.e.
221 * before the optional static or constexpr modifier.
222 */
223#if !defined(_DEBUG) || defined(NO_DEBUG_INLINE)
224/*
225 * Do not force inlining when not in debug. This way we do not work against
226 * any carefully designed compiler optimizations.
227 */
228#define debug_inline inline
229#elif defined(__clang__) || defined(__GNUC__)
230#define debug_inline [[gnu::always_inline]] inline
231#else
232/*
233 * MSVC explicitly disables inlining, even forced inlining, in debug builds
234 * so __forceinline makes no difference compared to inline. Other unknown
235 * compilers can also just fallback to a normal inline.
236 */
237#define debug_inline inline
238#endif
239
240/* This is already defined in unix, but not in QNX Neutrino (6.x) or Cygwin. */
241#if (!defined(UNIX) && !defined(__HAIKU__)) || defined(__QNXNTO__) || defined(__CYGWIN__)
242 typedef unsigned int uint;
243#endif
244
245#if !defined(WITH_PERSONAL_DIR)
246# define PERSONAL_DIR ""
247#endif
248
249/* Define the the platforms that use XDG */
250#if defined(WITH_PERSONAL_DIR) && defined(UNIX) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
251# define USE_XDG
252#endif
253
254/* Check if the types have the bitsizes like we are using them */
255static_assert(sizeof(uint64_t) == 8);
256static_assert(sizeof(uint32_t) == 4);
257static_assert(sizeof(uint16_t) == 2);
258static_assert(sizeof(uint8_t) == 1);
259static_assert(SIZE_MAX >= UINT32_MAX);
260
261#ifndef M_PI_2
262#define M_PI_2 1.57079632679489661923
263#define M_PI 3.14159265358979323846
264#endif /* M_PI_2 */
265
266template <typename T, size_t N>
267char (&ArraySizeHelper(T (&array)[N]))[N];
268
277#define lengthof(array) (sizeof(ArraySizeHelper(array)))
278
285#define cpp_sizeof(base, variable) (sizeof(std::declval<base>().variable))
286
287
288/* take care of some name clashes on MacOS */
289#if defined(__APPLE__)
290# define GetString OTTD_GetString
291# define DrawString OTTD_DrawString
292# define CloseConnection OTTD_CloseConnection
293#endif /* __APPLE__ */
294
295#if defined(__GNUC__) || defined(__clang__)
296# define GNU_TARGET(x) [[gnu::target(x)]]
297#else
298# define GNU_TARGET(x)
299#endif /* __GNUC__ || __clang__ */
300
301/* For the FMT library we only want to use the headers, not link to some library. */
302#define FMT_HEADER_ONLY
303
304[[noreturn]] void NOT_REACHED(const std::source_location location = std::source_location::current());
305[[noreturn]] void AssertFailedError(const char *expression, const std::source_location location = std::source_location::current());
306
307/* For non-debug builds with assertions enabled use the special assertion handler. */
308#if defined(NDEBUG) && defined(WITH_ASSERT)
309# undef assert
310# define assert(expression) do { if (!(expression)) [[unlikely]] AssertFailedError(#expression); } while (false)
311#endif
312
313/* Define JSON_ASSERT, which is used by nlohmann-json. Otherwise the header-file
314 * will re-include assert.h, and reset the assert macro. */
315#define JSON_ASSERT(x) assert(x)
316
317#if defined(MAX_PATH)
318 /* It's already defined, no need to override */
319#elif defined(PATH_MAX) && PATH_MAX > 0
320 /* Use the value from PATH_MAX, if it exists */
321# define MAX_PATH PATH_MAX
322#else
323 /* If all else fails, hardcode something :( */
324# define MAX_PATH 260
325#endif
326
331inline void free(const void *ptr)
332{
333 free(const_cast<void *>(ptr));
334}
335
336#if defined(_MSC_VER) && !defined(_DEBUG)
337# define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
338# define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
339#elif defined(__GNUC__) && !defined(_DEBUG)
340# define HELPER0(x) #x
341# define HELPER1(x) HELPER0(GCC diagnostic ignored x)
342# define HELPER2(y) HELPER1(#y)
343#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
344# define IGNORE_UNINITIALIZED_WARNING_START \
345 _Pragma("GCC diagnostic push") \
346 _Pragma(HELPER2(-Wuninitialized)) \
347 _Pragma(HELPER2(-Wmaybe-uninitialized))
348# define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
349#endif
350#endif
351
352#ifndef IGNORE_UNINITIALIZED_WARNING_START
353# define IGNORE_UNINITIALIZED_WARNING_START
354# define IGNORE_UNINITIALIZED_WARNING_STOP
355#endif
356
357#endif /* STDAFX_H */
OSX is different on some places.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition stdafx.h:331
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