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