ManaPlus
checkutils.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2012-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef UTILS_CHECKUTILS_H
23 #define UTILS_CHECKUTILS_H
24 
25 #include "logger.h"
26 LOGGER_H
27 
28 #ifdef ENABLE_ASSERTS
29 
30 #define reportFalseReal(val) \
31  (val ? true : (reportAssertStack(__FILE__, __LINE__, __func__, \
32  "Detected false value", #val), false))
33 
34 #define reportTrueReal(val) \
35  (val ? (reportAssertStack(__FILE__, __LINE__, __func__, \
36  "Detected false value", #val), true) : false)
37 
38 #define reportAlwaysReal(...) \
39  { \
40  logger->log("Assert:"); \
41  logger->assertLog( \
42  __VA_ARGS__); \
43  reportLogStack(__FILE__, __LINE__, __func__); \
44  }
45 
46 #define returnFalseVReal(val) \
47  if (!val) \
48  { \
49  reportAssertStack(__FILE__, __LINE__, __func__, \
50  "Detected false value", #val); \
51  return; \
52  }
53 
54 #define returnTrueVReal(val) \
55  if (val) \
56  { \
57  reportAssertStack(__FILE__, __LINE__, __func__, \
58  "Detected true value", #val); \
59  return; \
60  }
61 
62 #define returnFalseReal(ret, val) \
63  if (!val) \
64  { \
65  reportAssertStack(__FILE__, __LINE__, __func__, \
66  "Detected false value", #val); \
67  return ret; \
68  }
69 
70 #define returnTrueReal(ret, val) \
71  if (val) \
72  { \
73  reportAssertStack(__FILE__, __LINE__, __func__, \
74  "Detected true value", #val); \
75  return ret; \
76  }
77 
78 #define returnNullptrVReal(val) \
79  if ((val) == nullptr) \
80  { \
81  reportAssertStack(__FILE__, __LINE__, __func__, \
82  "Detected null value", #val); \
83  return; \
84  }
85 
86 #define returnNullptrReal(ret, val) \
87  if ((val) == nullptr) \
88  { \
89  reportAssertStack(__FILE__, __LINE__, __func__, \
90  "Detected null value", #val); \
91  return ret; \
92  }
93 
94 #define failFalse(val) \
95  (val ? true : (reportAssertStack(__FILE__, __LINE__, __func__, \
96  "Detected false value", #val), \
97  throw new std::exception(), false))
98 
99 #define failTrue(val) \
100  (val ? (reportAssertStack(__FILE__, __LINE__, __func__, \
101  "Detected false value", #val), \
102  throw new std::exception(), true) : false)
103 
104 #define returnFailFalseV(val) \
105  if (!val) \
106  { \
107  reportAssertStack(__FILE__, __LINE__, __func__, \
108  "Detected false value", #val); \
109  throw new std::exception(); \
110  }
111 
112 #define returnFailTrueV(val) \
113  if (val) \
114  { \
115  reportAssertStack(__FILE__, __LINE__, __func__, \
116  "Detected true value", #val); \
117  throw new std::exception(); \
118  }
119 
120 #define returnFailFalse(ret, val) \
121  if (!val) \
122  { \
123  reportAssertStack(__FILE__, __LINE__, __func__, \
124  "Detected false value", #val); \
125  throw new std::exception(); \
126  }
127 
128 #define returnFailTrue(ret, val) \
129  if (val) \
130  { \
131  reportAssertStack(__FILE__, __LINE__, __func__, \
132  "Detected true value", #val); \
133  throw new std::exception(); \
134  }
135 
136 #define returnFailNullptrV(val) \
137  if ((val) == nullptr) \
138  { \
139  reportAssertStack(__FILE__, __LINE__, __func__, \
140  "Detected null value", #val); \
141  throw new std::exception(); \
142  }
143 
144 #define returnFailNullptr(ret, val) \
145  if ((val) == nullptr) \
146  { \
147  reportAssertStack(__FILE__, __LINE__, __func__, \
148  "Detected null value", #val); \
149  throw new std::exception(); \
150  }
151 
152 #define failAlways(...) \
153  { \
154  logger->log("Assert:"); \
155  logger->assertLog( \
156  __VA_ARGS__); \
157  reportLogStack(__FILE__, __LINE__, __func__); \
158  throw new std::exception(); \
159  }
160 
161 void reportAssertStack(const char *const file,
162  const unsigned int line,
163  const char *const func,
164  const char *const name,
165  const char *const text);
166 
167 void reportLogStack(const char *const file,
168  const unsigned int line,
169  const char *const func);
170 
171 void reportStack();
172 
173 #else // ENABLE_ASSERTS
174 
175 #define reportFalseReal(val) (val)
176 #define reportTrueReal(val) (val)
177 
178 #define reportAlwaysReal(...) \
179  { \
180  logger->log("Error:"); \
181  logger->log( \
182  __VA_ARGS__); \
183  }
184 
185 #define returnFalseVReal(val) \
186  if (!(val)) \
187  return;
188 
189 #define returnTrueVReal(val) \
190  if (val) \
191  return;
192 
193 #define returnFalseReal(ret, val) \
194  if (!(val)) \
195  return ret;
196 
197 #define returnTrueReal(ret, val) \
198  if (val) \
199  return ret;
200 
201 #define returnNullptrVReal(val) \
202  if ((val) == nullptr) \
203  return;
204 
205 #define returnNullptrReal(ret, val) \
206  if ((val) == nullptr) \
207  return ret;
208 
209 #define failFalse(val) (val)
210 #define failTrue(val) (val)
211 
212 #define returnFailFalseV(val) \
213  if (!(val)) \
214  return;
215 
216 #define returnFailTrueV(val) \
217  if (val) \
218  return;
219 
220 #define returnFailFalse(ret, val) \
221  if (!(val)) \
222  return ret;
223 
224 #define returnFailTrue(ret, val) \
225  if (val) \
226  return ret;
227 
228 #define returnFailNullptrV(val) \
229  if ((val) == nullptr) \
230  return;
231 
232 #define returnFailNullptr(ret, val) \
233  if ((val) == nullptr) \
234  return ret;
235 
236 #define failAlways(...) ;
237 
238 #endif // ENABLE_ASSERTS
239 
240 #ifdef UNITTESTS
241 #define reportFalse(val) failFalse(val)
242 #define reportTrue(val) failTrue(val)
243 #define reportAlways(...) failAlways(__VA_ARGS__)
244 #define returnFalseV(val) returnFailFalseV(val)
245 #define returnTrueV(val) returnFailTrueV(val)
246 #define returnFalse(ret, val) returnFailFalse(ret, val)
247 #define returnTrue(ret, val) returnFailTrue(ret, val)
248 #define returnNullptrV(val) returnFailNullptrV(val)
249 #define returnNullptr(ret, val) returnFailNullptr(ret, val)
250 #else // UNITTESTS
251 #define reportFalse(val) reportFalseReal(val)
252 #define reportTrue(val) reportTrueReal(val)
253 #define reportAlways(...) reportAlwaysReal(__VA_ARGS__)
254 #define returnFalseV(val) returnFalseVReal(val)
255 #define returnTrueV(val) returnTrueVReal(val)
256 #define returnFalse(ret, val) returnFalseReal(ret, val)
257 #define returnTrue(ret, val) returnTrueReal(ret, val)
258 #define returnNullptrV(val) returnNullptrVReal(val)
259 #define returnNullptr(ret, val) returnNullptrReal(ret, val)
260 #endif // UNITTESTS
261 
262 #endif // UTILS_CHECKUTILS_H