OpenTTD Source: src/stdafx.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
76using namespace std::literals::string_view_literals;
77
78#if defined(UNIX) || defined(__MINGW32__)
79# include <sys/types.h>
80#endif
81
82/* Stuff for GCC */
83#if defined(__GNUC__) || (defined(__clang__) && !defined(_MSC_VER))
84# define CDECL
85#endif /* __GNUC__ || __clang__ */
86
87#if defined(_WIN32)
88# define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
89#endif
90
91#if defined(_MSC_VER)
92 /* See https://learn.microsoft.com/en-us/cpp/cpp/empty-bases?view=msvc-170 */
93# define EMPTY_BASES __declspec(empty_bases)
94#else
95# define EMPTY_BASES
96#endif
97
98/* Stuff for MSVC */
99#if defined(_MSC_VER)
100# pragma once
101# define NOMINMAX // Disable min/max macros in windows.h.
102
103# pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
104# pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
105# pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
106# pragma warning(disable: 4355) // 'this' : used in base member initializer list
107
108# 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)
109# pragma warning(disable: 4996) // 'function': was declared deprecated
110# 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
111# pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
112# pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
113# pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
114# 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 ...
115
116# define CDECL _cdecl
117
118# if defined(_WIN32) && !defined(_WIN64)
119# if !defined(_W64)
120# define _W64
121# endif
122
123 typedef _W64 int INT_PTR, *PINT_PTR;
124 typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
125# endif /* _WIN32 && !_WIN64 */
126
127# if defined(_WIN64)
128# define fseek _fseeki64
129# endif /* _WIN64 */
130
131 /* zlib from vcpkg use cdecl calling convention without enforcing it in the headers */
132# if defined(WITH_ZLIB)
133# if !defined(ZEXPORT)
134# define ZEXPORT CDECL
135# endif
136# endif
137
138 /* freetype from vcpkg use cdecl calling convention without enforcing it in the headers */
139# if defined(WITH_FREETYPE)
140# if !defined(FT_EXPORT)
141# define FT_EXPORT( x ) extern "C" x CDECL
142# endif
143# endif
144
145 /* liblzma from vcpkg (before 5.2.4-2) used to patch lzma.h to define LZMA_API_STATIC for static builds */
146# if defined(WITH_LIBLZMA)
147# if !defined(LZMA_API_STATIC)
148# define LZMA_API_STATIC
149# endif
150# endif
151
152 /* MSVC doesn't have these :( */
153# define S_ISDIR(mode) (mode & S_IFDIR)
154# define S_ISREG(mode) (mode & S_IFREG)
155
156#endif /* defined(_MSC_VER) */
157
158#if !defined(STRGEN) && !defined(SETTINGSGEN)
159# if defined(_WIN32)
160 std::string FS2OTTD(std::wstring_view name);
161 std::wstring OTTD2FS(std::string_view name);
162# elif defined(WITH_ICONV)
163 std::string FS2OTTD(std::string_view name);
164 std::string OTTD2FS(std::string_view name);
165# else
166 static inline std::string FS2OTTD(std::string_view name) { return std::string{name}; }
167 static inline std::string OTTD2FS(std::string_view name) { return std::string{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 attribute 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 attribute 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
223#elif defined(__clang__) || defined(__GNUC__)
224#define debug_inline gnu::always_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
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
273
274/* take care of some name clashes on MacOS */
275#if defined(__APPLE__)
276# define GetString OTTD_GetString
277# define DrawString OTTD_DrawString
278# define CloseConnection OTTD_CloseConnection
279#endif /* __APPLE__ */
280
281#if defined(__GNUC__) || defined(__clang__)
282# define GNU_TARGET(x) [[gnu::target(x)]]
283#else
284# define GNU_TARGET(x)
285#endif /* __GNUC__ || __clang__ */
286
287[[noreturn]] void NOT_REACHED(const std::source_location location = std::source_location::current());
288[[noreturn]] void AssertFailedError(std::string_view expression, const std::source_location location = std::source_location::current());
289
290/* For non-debug builds with assertions enabled use the special assertion handler. */
291#if defined(NDEBUG) && defined(WITH_ASSERT)
292# undef assert
293# define assert(expression) do { if (!(expression)) [[unlikely]] AssertFailedError(#expression); } while (false)
294#endif
295
296/* Define JSON_ASSERT, which is used by nlohmann-json. Otherwise the header-file
297 * will re-include assert.h, and reset the assert macro. */
298#define JSON_ASSERT(x) assert(x)
299
300#if defined(MAX_PATH)
301 /* It's already defined, no need to override */
302#elif defined(PATH_MAX) && PATH_MAX > 0
303 /* Use the value from PATH_MAX, if it exists */
304# define MAX_PATH PATH_MAX
305#else
306 /* If all else fails, hardcode something :( */
307# define MAX_PATH 260
308#endif
309
310#if defined(_MSC_VER) && !defined(_DEBUG)
311# define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
312# define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
313#elif defined(__GNUC__) && !defined(_DEBUG)
314# define HELPER0(x) #x
315# define HELPER1(x) HELPER0(GCC diagnostic ignored x)
316# define HELPER2(y) HELPER1(#y)
317#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
318# define IGNORE_UNINITIALIZED_WARNING_START \
319 _Pragma("GCC diagnostic push") \
320 _Pragma(HELPER2(-Wuninitialized)) \
321 _Pragma(HELPER2(-Wmaybe-uninitialized))
322# define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
323#endif
324#endif
325
326#ifndef IGNORE_UNINITIALIZED_WARNING_START
327# define IGNORE_UNINITIALIZED_WARNING_START
328# define IGNORE_UNINITIALIZED_WARNING_STOP
329#endif
330
331#endif /* STDAFX_H */
osx_stdafx.h
OSX is different on some places.
OTTD2FS
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:357
FS2OTTD
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:340