-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_macros.hpp
357 lines (308 loc) · 19.5 KB
/
error_macros.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*********************************************************************************/
/* */
/* Copyright (C) 2024 Seif Kandil (k0T0z) */
/* */
/* This file is a part of the ENIGMA Development Environment. */
/* */
/* */
/* ENIGMA 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 3 of the license or any later version. */
/* */
/* This application and its source code is distributed AS-IS, WITHOUT ANY */
/* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS */
/* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more */
/* details. */
/* */
/* You should have recieved a copy of the GNU General Public License along */
/* with this code. If not, see <http://www.gnu.org/licenses/> */
/* */
/* ENIGMA is an environment designed to create games and other programs with a */
/* high-level, fully compilable language. Developers of ENIGMA or anything */
/* associated with ENIGMA are in no way responsible for its users or */
/* applications created by its users, or damages caused by the environment */
/* or programs made in the environment. */
/* */
/*********************************************************************************/
// This is a general purpose error handling macros header file so feel free to use it
// in your projects.
#ifndef ERROR_MACROS_HPP
#define ERROR_MACROS_HPP
#ifdef SHADER_GEN_DEBUG
#include <iostream>
#endif // SHADER_GEN_DEBUG
#include <cstdint>
#include <string>
namespace shadergen_logger {
inline static void print_error(const char* function, const char* file, const int& line, const char* error,
const std::string& message) noexcept {
#ifdef SHADER_GEN_DEBUG
std::cerr << "ERROR: " << error << "\n";
std::cerr << " at: " << function << " (" << file << ":" << line << ")" << "\n";
std::cerr << " " << message << "\n";
#endif // SHADER_GEN_DEBUG
}
inline static void print_index_error(const char* function, const char* file, const int& line, const int64_t& index,
const int64_t& size, const std::string& message,
const bool& fatal = false) noexcept {
#ifdef SHADER_GEN_DEBUG
std::cerr << (fatal ? "FATAL: " : "ERROR: ") << "Index " << index << " is out of bounds (" << size << ")." << "\n";
std::cerr << " at: " << function << " (" << file << ":" << line << ")" << "\n";
std::cerr << " " << message << "\n";
#endif // SHADER_GEN_DEBUG
}
inline static void print_warning(const char* function, const char* file, const int& line, const char* warning,
const std::string& message) noexcept {
#ifdef SHADER_GEN_DEBUG
std::cerr << "WARNING: " << warning << "\n";
std::cerr << " at: " << function << " (" << file << ":" << line << ")" << "\n";
std::cerr << " " << message << "\n";
#endif // SHADER_GEN_DEBUG
}
inline static void print_debug(const char* function, const char* file, const int& line,
const std::string& message) noexcept {
#ifdef SHADER_GEN_DEBUG
std::cerr << "DEBUG at: " << function << " (" << file << ":" << line << ")" << "\n";
std::cerr << " " << message << "\n";
#endif // SHADER_GEN_DEBUG
}
inline static void flush_stdout() noexcept {
#ifdef SHADER_GEN_DEBUG
fflush(stdout);
#endif // SHADER_GEN_DEBUG
}
} // namespace shadergen_logger
// https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html
#ifndef STRNGIZE
#define STRNGIZE(S) #S
#define XSTRNGIZE(S) STRNGIZE(S)
#endif // STRNGIZE
// Don't use GENERATE_TRAP() directly, should only be used be the macros below.
#ifdef _MSC_VER
#define GENERATE_TRAP() __debugbreak()
#else
#define GENERATE_TRAP() __builtin_trap()
#endif
/**
* Error Handling Macros
*
* @brief These macros prioritize application stability over strict error enforcement,
* operating opposite to assert(). While assert() halts execution on failure,
* these macros attempt to handle errors gracefully.
*
* @c WHY? To maintain program stability and avoid crashes, handle non-fatal errors
* and invalid data safely, return fallback values to allow continued
* execution, and provide non-silent variants for detailed debugging info.
*
* @note Uses @c ((void)0) to enforce semicolon usage after macro calls
* @note Wraps conditions in if statements to prevent expansion issues
* Example: if (<CONDITION>) SILENT_FAIL_AND_RETURN(); // Works correctly without
* braces
*
* @note @c SILENT_ macros are for non-critical errors and invalid data
* @note You can add a message to any macro with no @c SILENT_ prefix
* @note You can add a return value to any macro with @c NON_VOID suffix
* @note @c CRITICAL_ macros will halt execution with a trap on failure. A critical
* error is a fatal error that should never occur in a stable application.
*/
#define SILENT_VALIDATE_INDEX(I, SIZE) \
if ((I < 0) || (I >= SIZE)) { \
return; \
} else \
((void)0)
#define SILENT_VALIDATE_INDEX_NON_VOID(I, SIZE, RETURN_VAL) \
if ((I < 0) || (I >= SIZE)) { \
return RETURN_VAL; \
} else \
((void)0)
#define VALIDATE_INDEX(I, SIZE, DETAILS) \
if ((I < 0) || (I >= SIZE)) { \
shadergen_logger::print_index_error(__FUNCTION__, __FILE__, __LINE__, I, SIZE, DETAILS); \
return; \
} else \
((void)0)
#define VALIDATE_INDEX_NON_VOID(I, SIZE, RETURN_VAL, DETAILS) \
if ((I < 0) || (I >= SIZE)) { \
shadergen_logger::print_index_error(__FUNCTION__, __FILE__, __LINE__, I, SIZE, DETAILS); \
return RETURN_VAL; \
} else \
((void)0)
#define VALIDATE_CRITICAL_INDEX(I, SIZE, DETAILS) \
if ((I < 0) || (I >= SIZE)) { \
shadergen_logger::print_index_error(__FUNCTION__, __FILE__, __LINE__, I, SIZE, DETAILS, true); \
shadergen_logger::flush_stdout(); \
GENERATE_TRAP(); \
} else \
((void)0)
//////////////////////////////////
//////////////////////////////////
//////////////////////////////////
#define SILENT_VALIDATE_UNSIGNED_INDEX(I, SIZE) \
if (I >= SIZE) { \
return; \
} else \
((void)0)
#define SILENT_VALIDATE_UNSIGNED_INDEX_NON_VOID(I, SIZE, RETURN_VAL) \
if (I >= SIZE) { \
return RETURN_VAL; \
} else \
((void)0)
#define VALIDATE_UNSIGNED_INDEX(I, SIZE, DETAILS) \
if (I >= SIZE) { \
shadergen_logger::print_index_error(__FUNCTION__, __FILE__, __LINE__, I, SIZE, DETAILS); \
return; \
} else \
((void)0)
#define VALIDATE_UNSIGNED_INDEX_NON_VOID(I, SIZE, RETURN_VAL, DETAILS) \
if (I >= SIZE) { \
shadergen_logger::print_index_error(__FUNCTION__, __FILE__, __LINE__, I, SIZE, DETAILS); \
return RETURN_VAL; \
} else \
((void)0)
#define VALIDATE_CRITICAL_UNSIGNED_INDEX(I, SIZE, DETAILS) \
if (I >= SIZE) { \
shadergen_logger::print_index_error(__FUNCTION__, __FILE__, __LINE__, I, SIZE, DETAILS, true); \
shadergen_logger::flush_stdout(); \
GENERATE_TRAP(); \
} else \
((void)0)
//////////////////////////////////
//////////////////////////////////
//////////////////////////////////
#define SILENT_CHECK_PARAM_NULLPTR(P) \
if (P == nullptr) { \
return; \
} else \
((void)0)
#define SILENT_CHECK_PARAM_NULLPTR_NON_VOID(P, RETURN_VAL) \
if (P == nullptr) { \
return RETURN_VAL; \
} else \
((void)0)
#define CHECK_PARAM_NULLPTR(P, DETAILS) \
if (P == nullptr) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, "Parameter \"" STRNGIZE(P) "\" is nullptr.", \
DETAILS); \
return; \
} else \
((void)0)
#define CHECK_PARAM_NULLPTR_NON_VOID(P, RETURN_VAL, DETAILS) \
if (P == nullptr) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, "Parameter \"" STRNGIZE(P) "\" is nullptr.", \
DETAILS); \
return RETURN_VAL; \
} else \
((void)0)
//////////////////////////////////
//////////////////////////////////
//////////////////////////////////
#define SILENT_CHECK_CONDITION_TRUE(C) \
if (C) { \
return; \
} else \
((void)0)
#define SILENT_CHECK_CONDITION_TRUE_NON_VOID(C, RETURN_VAL) \
if (C) { \
return RETURN_VAL; \
} else \
((void)0)
#define CHECK_CONDITION_TRUE(C, DETAILS) \
if (C) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, "Condition \"" STRNGIZE(C) "\" is true.", \
DETAILS); \
return; \
} else \
((void)0)
#define CHECK_CONDITION_TRUE_NON_VOID(C, RETURN_VAL, DETAILS) \
if (C) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, \
"Condition \"" STRNGIZE(C) "\" is true. Returning: " STRNGIZE(RETURN_VAL), DETAILS); \
return RETURN_VAL; \
} else \
((void)0)
#define CHECK_CRITICAL_CONDITION_TRUE(C, DETAILS) \
if (C) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, "FATAL: Condition \"" STRNGIZE(C) "\" is true.", \
DETAILS); \
shadergen_logger::flush_stdout(); \
GENERATE_TRAP(); \
} else \
((void)0)
//////////////////////////////////
//////////////////////////////////
//////////////////////////////////
#define SILENT_CONTINUE_IF_TRUE(C) \
if (C) { \
continue; \
} else \
((void)0)
#define CONTINUE_IF_TRUE(C, DETAILS) \
if (C) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, \
"Condition \"" STRNGIZE(C) "\" is true. Continuing.", DETAILS); \
continue; \
} else \
((void)0)
#define SILENT_BREAK_IF_TRUE(C) \
if (C) { \
break; \
} else \
((void)0)
#define BREAK_IF_TRUE(C, DETAILS) \
if (C) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, \
"Condition \"" STRNGIZE(C) "\" is true. Breaking.", DETAILS); \
break; \
} else \
((void)0)
//////////////////////////////////
//
// Generic error macros.
//
//////////////////////////////////
#define SILENT_FAIL_AND_RETURN() \
if (true) { \
return; \
} else \
((void)0)
#define SILENT_FAIL_AND_RETURN_NON_VOID(RETURN_VAL) \
if (true) { \
return RETURN_VAL; \
} else \
((void)0)
#define FAIL_AND_RETURN(DETAILS) \
if (true) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, "Method/function failed.", DETAILS); \
return; \
} else \
((void)0)
#define FAIL_AND_RETURN_NON_VOID(RETURN_VAL, DETAILS) \
if (true) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, \
"Method/function failed. Returning: " STRNGIZE(RETURN_VAL), DETAILS); \
return RETURN_VAL; \
} else \
((void)0)
#define ERROR_PRINT(DETAILS) \
if (true) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, "", DETAILS); \
} else \
((void)0)
#define WARN_PRINT(DETAILS) \
if (true) { \
shadergen_logger::print_warning(__FUNCTION__, __FILE__, __LINE__, "", DETAILS); \
} else \
((void)0)
#define DEBUG_PRINT(DETAILS) \
if (true) { \
shadergen_logger::print_debug(__FUNCTION__, __FILE__, __LINE__, DETAILS); \
} else \
((void)0)
#define CRASH_NOW(DETAILS) \
if (true) { \
shadergen_logger::print_error(__FUNCTION__, __FILE__, __LINE__, "FATAL: Method/function failed.", DETAILS); \
shadergen_logger::flush_stdout(); \
GENERATE_TRAP(); \
} else \
((void)0)
#endif // ERROR_MACROS_HPP