|
3 | 3 | #include "openvic-simulation/utility/Logger.hpp" // IWYU pragma: keep for macros |
4 | 4 | #include "openvic-simulation/utility/Utility.hpp" |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Try using `ERR_FAIL_COND_MSG`. |
| 8 | + * Only use this macro if more complex error detection or recovery is required. |
| 9 | + * |
| 10 | + * Prints `m_msg`, and the current function returns. |
| 11 | + */ |
| 12 | +#define OV_ERR_FAIL_MSG(m_msg) \ |
| 13 | + if (true) { \ |
| 14 | + ::OpenVic::Logger::error("Method/function failed. ", m_msg); \ |
| 15 | + return; \ |
| 16 | + } else \ |
| 17 | + ((void)0) |
| 18 | + |
| 19 | +/** |
| 20 | + * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`. |
| 21 | + * Only use this macro if more complex error detection or recovery is required, and |
| 22 | + * there is no sensible error message. |
| 23 | + * |
| 24 | + * The current function returns `m_retval`. |
| 25 | + */ |
| 26 | +#define OV_ERR_FAIL_V(m_retval) \ |
| 27 | + if (true) { \ |
| 28 | + ::OpenVic::Logger::error("Method/function failed. Returning: " _OV_STR(m_retval)); \ |
| 29 | + return m_retval; \ |
| 30 | + } else \ |
| 31 | + ((void)0) |
| 32 | + |
| 33 | +/** |
| 34 | + * Try using `ERR_FAIL_COND_V_MSG`. |
| 35 | + * Only use this macro if more complex error detection or recovery is required. |
| 36 | + * |
| 37 | + * Prints `m_msg`, and the current function returns `m_retval`. |
| 38 | + */ |
| 39 | +#define OV_ERR_FAIL_V_MSG(m_retval, m_msg) \ |
| 40 | + if (true) { \ |
| 41 | + ::OpenVic::Logger::error("Method/function failed. Returning: " _OV_STR(m_retval), " ", m_msg); \ |
| 42 | + return m_retval; \ |
| 43 | + } else \ |
| 44 | + ((void)0) |
| 45 | + |
6 | 46 | /** |
7 | 47 | * Try using `ERR_FAIL_COND_MSG`. |
8 | 48 | * Only use this macro if there is no sensible error message. |
9 | | - * If checking for null use ERR_FAIL_NULL_MSG instead. |
| 49 | + * If checking for null use OV_ERR_FAIL_NULL_MSG instead. |
10 | 50 | * If checking index bounds use ERR_FAIL_INDEX_MSG instead. |
11 | 51 | * |
12 | 52 | * Ensures `m_cond` is false. |
|
23 | 63 | * Ensures `m_cond` is false. |
24 | 64 | * If `m_cond` is true, prints `m_msg` and the current function returns. |
25 | 65 | * |
26 | | - * If checking for null use ERR_FAIL_NULL_MSG instead. |
| 66 | + * If checking for null use OV_ERR_FAIL_NULL_MSG instead. |
27 | 67 | * If checking index bounds use ERR_FAIL_INDEX_MSG instead. |
28 | 68 | */ |
29 | 69 | #define OV_ERR_FAIL_COND_MSG(m_cond, m_msg) \ |
|
36 | 76 | /** |
37 | 77 | * Try using `ERR_FAIL_COND_V_MSG`. |
38 | 78 | * Only use this macro if there is no sensible error message. |
39 | | - * If checking for null use ERR_FAIL_NULL_V_MSG instead. |
| 79 | + * If checking for null use OV_ERR_FAIL_NULL_V_MSG instead. |
40 | 80 | * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead. |
41 | 81 | * |
42 | 82 | * Ensures `m_cond` is false. |
43 | 83 | * If `m_cond` is true, the current function returns `m_retval`. |
44 | 84 | */ |
45 | 85 | #define OV_ERR_FAIL_COND_V(m_cond, m_retval) \ |
46 | 86 | if (OV_unlikely(m_cond)) { \ |
47 | | - ::OpenVic::Logger::error( \ |
48 | | - "Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval) \ |
49 | | - ); \ |
| 87 | + ::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval)); \ |
50 | 88 | return m_retval; \ |
51 | 89 | } else \ |
52 | 90 | ((void)0) |
|
55 | 93 | * Ensures `m_cond` is false. |
56 | 94 | * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`. |
57 | 95 | * |
58 | | - * If checking for null use ERR_FAIL_NULL_V_MSG instead. |
| 96 | + * If checking for null use OV_ERR_FAIL_NULL_V_MSG instead. |
59 | 97 | * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead. |
60 | 98 | */ |
61 | 99 | #define OV_ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \ |
62 | 100 | if (OV_unlikely(m_cond)) { \ |
| 101 | + ::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval) " ", m_msg); \ |
| 102 | + return m_retval; \ |
| 103 | + } else \ |
| 104 | + ((void)0) |
| 105 | + |
| 106 | +/** |
| 107 | + * Try using `ERR_FAIL_INDEX_MSG`. |
| 108 | + * Only use this macro if there is no sensible error message. |
| 109 | + * |
| 110 | + * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0. |
| 111 | + * If not, the current function returns. |
| 112 | + */ |
| 113 | +#define OV_ERR_FAIL_INDEX(m_index, m_size) \ |
| 114 | + if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ |
| 115 | + ::OpenVic::Logger::error( \ |
| 116 | + "Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, ")." \ |
| 117 | + ); \ |
| 118 | + return; \ |
| 119 | + } else \ |
| 120 | + ((void)0) |
| 121 | + |
| 122 | +/** |
| 123 | + * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0. |
| 124 | + * If not, prints `m_msg` and the current function returns. |
| 125 | + */ |
| 126 | +#define OV_ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \ |
| 127 | + if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ |
63 | 128 | ::OpenVic::Logger::error( \ |
64 | | - "Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval) " ", m_msg \ |
| 129 | + "Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, "). ", m_msg \ |
65 | 130 | ); \ |
| 131 | + return; \ |
| 132 | + } else \ |
| 133 | + ((void)0) |
| 134 | + |
| 135 | +/** |
| 136 | + * Try using `ERR_FAIL_INDEX_V_MSG`. |
| 137 | + * Only use this macro if there is no sensible error message. |
| 138 | + * |
| 139 | + * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0. |
| 140 | + * If not, the current function returns `m_retval`. |
| 141 | + */ |
| 142 | +#define OV_ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \ |
| 143 | + if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ |
| 144 | + ::OpenVic::Logger::error( \ |
| 145 | + "Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, \ |
| 146 | + "). Returning: " _OV_STR(m_retval) \ |
| 147 | + ); \ |
| 148 | + return m_retval; \ |
| 149 | + } else \ |
| 150 | + ((void)0) |
| 151 | + |
| 152 | +/** |
| 153 | + * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0. |
| 154 | + * If not, prints `m_msg` and the current function returns `m_retval`. |
| 155 | + */ |
| 156 | +#define OV_ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \ |
| 157 | + if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ |
| 158 | + ::OpenVic::Logger::error( \ |
| 159 | + "Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, \ |
| 160 | + "). Returning: " _OV_STR(m_retval) " ", m_msg \ |
| 161 | + ); \ |
| 162 | + return m_retval; \ |
| 163 | + } else \ |
| 164 | + ((void)0) |
| 165 | + |
| 166 | +#define OV_ERR_CONTINUE(m_cond) \ |
| 167 | + if (OV_unlikely(m_cond)) { \ |
| 168 | + ::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Continuing."); \ |
| 169 | + continue; \ |
| 170 | + } else \ |
| 171 | + ((void)0) |
| 172 | + |
| 173 | +#define OV_ERR_BREAK(m_cond) \ |
| 174 | + if (OV_unlikely(m_cond)) { \ |
| 175 | + ::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Breaking."); \ |
| 176 | + break; \ |
| 177 | + } else \ |
| 178 | + ((void)0) |
| 179 | + |
| 180 | +/** |
| 181 | + * Try using `ERR_FAIL_NULL_MSG`. |
| 182 | + * Only use this macro if there is no sensible error message. |
| 183 | + * |
| 184 | + * Ensures a pointer `m_param` is not null. |
| 185 | + * If it is null, the current function returns. |
| 186 | + */ |
| 187 | +#define OV_ERR_FAIL_NULL(m_param) \ |
| 188 | + if (OV_unlikely(m_param == nullptr)) { \ |
| 189 | + ::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null."); \ |
| 190 | + return; \ |
| 191 | + } else \ |
| 192 | + ((void)0) |
| 193 | + |
| 194 | +/** |
| 195 | + * Ensures a pointer `m_param` is not null. |
| 196 | + * If it is null, prints `m_msg` and the current function returns. |
| 197 | + */ |
| 198 | +#define OV_ERR_FAIL_NULL_MSG(m_param, m_msg) \ |
| 199 | + if (OV_unlikely(m_param == nullptr)) { \ |
| 200 | + ::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null.", m_msg); \ |
| 201 | + return; \ |
| 202 | + } else \ |
| 203 | + ((void)0) |
| 204 | + |
| 205 | +/** |
| 206 | + * Try using `ERR_FAIL_NULL_V_MSG`. |
| 207 | + * Only use this macro if there is no sensible error message. |
| 208 | + * |
| 209 | + * Ensures a pointer `m_param` is not null. |
| 210 | + * If it is null, the current function returns `m_retval`. |
| 211 | + */ |
| 212 | +#define OV_ERR_FAIL_NULL_V(m_param, m_retval) \ |
| 213 | + if (OV_unlikely(m_param == nullptr)) { \ |
| 214 | + ::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null."); \ |
| 215 | + return m_retval; \ |
| 216 | + } else \ |
| 217 | + ((void)0) |
| 218 | + |
| 219 | +/** |
| 220 | + * Ensures a pointer `m_param` is not null. |
| 221 | + * If it is null, prints `m_msg` and the current function returns `m_retval`. |
| 222 | + */ |
| 223 | +#define OV_ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \ |
| 224 | + if (OV_unlikely(m_param == nullptr)) { \ |
| 225 | + ::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null.", m_msg); \ |
66 | 226 | return m_retval; \ |
67 | 227 | } else \ |
68 | 228 | ((void)0) |
0 commit comments