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