Codechange: Store custom station layouts in a map instead of nested vectors. (#12898)
[openttd-github.git] / src / stdafx.h
blobb006c8c47156eb3f907c4e96027c5fc0321ad7ce
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 */
8 /** @file stdafx.h Definition of base types and functions in a cross-platform compatible way. */
10 #ifndef STDAFX_H
11 #define STDAFX_H
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
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
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__ */
39 #if defined(__HAIKU__)
40 # include <SupportDefs.h>
41 # include <unistd.h>
42 # define _DEFAULT_SOURCE
43 # define _GNU_SOURCE
44 #endif
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>
79 #if defined(UNIX) || defined(__MINGW32__)
80 # include <sys/types.h>
81 #endif
83 /* Stuff for GCC */
84 #if defined(__GNUC__) || (defined(__clang__) && !defined(_MSC_VER))
85 # define CDECL
86 #endif /* __GNUC__ || __clang__ */
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
94 #if defined(_WIN32)
95 # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
96 #endif
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
105 /* Stuff for MSVC */
106 #if defined(_MSC_VER)
107 # pragma once
108 # define NOMINMAX // Disable min/max macros in windows.h.
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
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 ...
123 # define CDECL _cdecl
125 # if defined(_WIN32) && !defined(_WIN64)
126 # if !defined(_W64)
127 # define _W64
128 # endif
130 typedef _W64 int INT_PTR, *PINT_PTR;
131 typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
132 # endif /* _WIN32 && !_WIN64 */
134 # if defined(_WIN64)
135 # define fseek _fseeki64
136 # endif /* _WIN64 */
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
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
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
159 /* MSVC doesn't have these :( */
160 # define S_ISDIR(mode) (mode & S_IFDIR)
161 # define S_ISREG(mode) (mode & S_IFREG)
163 #endif /* defined(_MSC_VER) */
165 #if !defined(STRGEN) && !defined(SETTINGSGEN)
166 # if defined(_WIN32)
167 char *getcwd(char *buf, size_t size);
168 # include <io.h>
169 # include <tchar.h>
171 # define fopen(file, mode) _wfopen(OTTD2FS(file).c_str(), _T(mode))
173 std::string FS2OTTD(const std::wstring &name);
174 std::wstring OTTD2FS(const std::string &name);
175 # elif defined(WITH_ICONV)
176 # define fopen(file, mode) fopen(OTTD2FS(file).c_str(), mode)
177 std::string FS2OTTD(const std::string &name);
178 std::string OTTD2FS(const std::string &name);
179 # else
180 // no override of fopen() since no transformation is required of the filename
181 template <typename T> std::string FS2OTTD(T name) { return name; }
182 template <typename T> std::string OTTD2FS(T name) { return name; }
183 # endif /* _WIN32 or WITH_ICONV */
184 #endif /* STRGEN || SETTINGSGEN */
186 #if defined(_WIN32)
187 # define PATHSEP "\\"
188 # define PATHSEPCHAR '\\'
189 #else
190 # define PATHSEP "/"
191 # define PATHSEPCHAR '/'
192 #endif
194 #if defined(_MSC_VER)
195 # define PACK_N(type_dec, n) __pragma(pack(push, n)) type_dec; __pragma(pack(pop))
196 #elif defined(__MINGW32__)
197 # define PRAGMA(x) _Pragma(#x)
198 # define PACK_N(type_dec, n) PRAGMA(pack(push, n)) type_dec; PRAGMA(pack(pop))
199 #else
200 # define PACK_N(type_dec, n) type_dec __attribute__((__packed__, aligned(n)))
201 #endif
202 #define PACK(type_dec) PACK_N(type_dec, 1)
205 * When making a (pure) debug build, the compiler will by default disable
206 * inlining of functions. This has a detremental effect on the performance of
207 * debug builds, especially when more and more trivial (wrapper) functions get
208 * added to the code base.
209 * Take for example the savegame called "Wentbourne", when running this game
210 * for 100 ticks with the null video driver a number of fairly trivial
211 * functions show up on top. The most common one is the implicit conversion
212 * operator of TileIndex to unsigned int, which takes up over 5% of the total
213 * run time and functionally does absolutely nothing. The remaining functions
214 * for the top 5 are GB, GetTileType, Map::Size and IsTileType to a total of
215 * about 12.5% of the game's total run time.
216 * It is possible to still force inlining in the most commonly used compilers,
217 * but that is at the cost of some problems with debugging due to the forced
218 * inlining. However, the performance benefit can be enormous; when forcing
219 * inlining for the previously mentioned top 5, the debug build ran about 15%
220 * quicker.
221 * The following debug_inline annotation may be added to functions comply
222 * with the following preconditions:
223 * 1: the function takes more than 0.5% of a profiled debug runtime
224 * 2: the function does not modify the game state
225 * 3: the function does not contain selection or iteration statements,
226 * i.e. no if, switch, for, do, while, etcetera.
227 * 4: the function is one line of code, excluding assertions.
228 * 5: the function is defined in a header file.
229 * The debug_inline annotation must be placed in front of the function, i.e.
230 * before the optional static or constexpr modifier.
232 #if !defined(_DEBUG) || defined(NO_DEBUG_INLINE)
234 * Do not force inlining when not in debug. This way we do not work against
235 * any carefully designed compiler optimizations.
237 #define debug_inline inline
238 #elif defined(__clang__) || defined(__GNUC__)
239 #define debug_inline [[gnu::always_inline]] inline
240 #else
242 * MSVC explicitly disables inlining, even forced inlining, in debug builds
243 * so __forceinline makes no difference compared to inline. Other unknown
244 * compilers can also just fallback to a normal inline.
246 #define debug_inline inline
247 #endif
249 /* This is already defined in unix, but not in QNX Neutrino (6.x) or Cygwin. */
250 #if (!defined(UNIX) && !defined(__HAIKU__)) || defined(__QNXNTO__) || defined(__CYGWIN__)
251 typedef unsigned int uint;
252 #endif
254 #if !defined(WITH_PERSONAL_DIR)
255 # define PERSONAL_DIR ""
256 #endif
258 /* Define the the platforms that use XDG */
259 #if defined(WITH_PERSONAL_DIR) && defined(UNIX) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
260 # define USE_XDG
261 #endif
263 /* Check if the types have the bitsizes like we are using them */
264 static_assert(sizeof(uint64_t) == 8);
265 static_assert(sizeof(uint32_t) == 4);
266 static_assert(sizeof(uint16_t) == 2);
267 static_assert(sizeof(uint8_t) == 1);
268 static_assert(SIZE_MAX >= UINT32_MAX);
270 #ifndef M_PI_2
271 #define M_PI_2 1.57079632679489661923
272 #define M_PI 3.14159265358979323846
273 #endif /* M_PI_2 */
275 template <typename T, size_t N>
276 char (&ArraySizeHelper(T (&array)[N]))[N];
279 * Return the length of an fixed size array.
280 * Unlike sizeof this function returns the number of elements
281 * of the given type.
283 * @param x The pointer to the first element of the array
284 * @return The number of elements
286 #define lengthof(array) (sizeof(ArraySizeHelper(array)))
289 * Gets the size of a variable within a class.
290 * @param base The class the variable is in.
291 * @param variable The variable to get the size of.
292 * @return the size of the variable
294 #define cpp_sizeof(base, variable) (sizeof(std::declval<base>().variable))
297 /* take care of some name clashes on MacOS */
298 #if defined(__APPLE__)
299 # define GetString OTTD_GetString
300 # define DrawString OTTD_DrawString
301 # define CloseConnection OTTD_CloseConnection
302 #endif /* __APPLE__ */
304 #if defined(__GNUC__) || defined(__clang__)
305 # define GNU_TARGET(x) [[gnu::target(x)]]
306 #else
307 # define GNU_TARGET(x)
308 #endif /* __GNUC__ || __clang__ */
310 /* For the FMT library we only want to use the headers, not link to some library. */
311 #define FMT_HEADER_ONLY
313 [[noreturn]] void NOT_REACHED(const std::source_location location = std::source_location::current());
314 [[noreturn]] void AssertFailedError(const char *expression, const std::source_location location = std::source_location::current());
316 /* For non-debug builds with assertions enabled use the special assertion handler. */
317 #if defined(NDEBUG) && defined(WITH_ASSERT)
318 # undef assert
319 # define assert(expression) do { if (!(expression)) [[unlikely]] AssertFailedError(#expression); } while (false)
320 #endif
322 /* Define JSON_ASSERT, which is used by nlohmann-json. Otherwise the header-file
323 * will re-include assert.h, and reset the assert macro. */
324 #define JSON_ASSERT(x) assert(x)
326 #if defined(MAX_PATH)
327 /* It's already defined, no need to override */
328 #elif defined(PATH_MAX) && PATH_MAX > 0
329 /* Use the value from PATH_MAX, if it exists */
330 # define MAX_PATH PATH_MAX
331 #else
332 /* If all else fails, hardcode something :( */
333 # define MAX_PATH 260
334 #endif
337 * Version of the standard free that accepts const pointers.
338 * @param ptr The data to free.
340 inline void free(const void *ptr)
342 free(const_cast<void *>(ptr));
346 * The largest value that can be entered in a variable
347 * @param type the type of the variable
349 #define MAX_UVALUE(type) (static_cast<type>(~static_cast<type>(0)))
351 #if defined(_MSC_VER) && !defined(_DEBUG)
352 # define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
353 # define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
354 #elif defined(__GNUC__) && !defined(_DEBUG)
355 # define HELPER0(x) #x
356 # define HELPER1(x) HELPER0(GCC diagnostic ignored x)
357 # define HELPER2(y) HELPER1(#y)
358 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
359 # define IGNORE_UNINITIALIZED_WARNING_START \
360 _Pragma("GCC diagnostic push") \
361 _Pragma(HELPER2(-Wuninitialized)) \
362 _Pragma(HELPER2(-Wmaybe-uninitialized))
363 # define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
364 #endif
365 #endif
367 #ifndef IGNORE_UNINITIALIZED_WARNING_START
368 # define IGNORE_UNINITIALIZED_WARNING_START
369 # define IGNORE_UNINITIALIZED_WARNING_STOP
370 #endif
372 #endif /* STDAFX_H */