The Battle for Wesnoth  1.19.7+dev
global.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #ifdef _MSC_VER
19 #endif //_MSC_VER
20 
21 #ifdef NDEBUG
22 /*
23  * Wesnoth uses asserts to avoid undefined behaviour. For example, to make sure
24  * pointers are not nullptr before dereferencing them, or collections are not empty
25  * before accessing their elements. Therefore Wesnoth should not be compiled
26  * with assertions disabled.
27  */
28 #error "Compilation with NDEBUG defined isn't supported, Wesnoth depends on asserts."
29 #endif
30 
31 #if defined(__clang__)
32 #endif
33 
34 #if defined(__GNUC__) && !defined(__clang__)
35 #endif
36 
37 /*
38  * GCC-13 and GCC-14 warn about functions that return a reference and whose arguments also include a
39  * reference to a temporary, because they assume that the returned reference may point into the
40  * argument. This causes false positives for functions that take a std::map-like object as a
41  * separate argument (or as their "this" pointer), where the temporary being passed in is only used
42  * as a key to find an object in the map, and the returned reference points to an object owned by
43  * the map. Similarly, it's a false positive for data owned by a singleton.
44  *
45  * GCC-14 supports supressing the warnings with [[gnu::no_dangling]]. Clang complains about unknown
46  * attributes in the gnu:: namespace, so has to have the #if, and the #if means we need the #ifdef.
47  */
48 #ifdef __has_cpp_attribute
49 
50 #if __has_cpp_attribute(gnu::no_dangling)
51 #define NOT_DANGLING [[gnu::no_dangling]]
52 #endif
53 
54 #if __has_cpp_attribute(likely)
55 #define LIKELY [[likely]]
56 #endif
57 
58 #if __has_cpp_attribute(unlikely)
59 #define UNLIKELY [[unlikely]]
60 #endif
61 
62 #endif // __has_cpp_attribute
63 
64 #ifndef NOT_DANGLING
65 #define NOT_DANGLING
66 #endif
67 
68 #ifndef LIKELY
69 #define LIKELY
70 #endif
71 
72 #ifndef UNLIKELY
73 #define UNLIKELY
74 #endif
75 
76 #ifdef __cpp_aggregate_paren_init
77 #define AGGREGATE_EMPLACE(...) emplace_back(__VA_ARGS__)
78 #else
79 #define AGGREGATE_EMPLACE(...) push_back({ __VA_ARGS__ })
80 #endif