xmscore  1.0
XmError.h
Go to the documentation of this file.
1 #pragma once
2 //------------------------------------------------------------------------------
8 //------------------------------------------------------------------------------
9 
10 //----- Included files ---------------------------------------------------------
11 
12 // 3. Standard Library Headers
13 
14 // 4. External Library Headers
15 #include <boost/preprocessor/facilities/overload.hpp>
16 #include <boost/preprocessor/cat.hpp>
17 #include <boost/preprocessor/facilities/empty.hpp>
18 
19 // 5. Shared Headers
20 #include <xmscore/misc/XmLog.h>
21 
22 // 6. Non-shared Headers
23 
24 //----- Functions --------------------------------------------------------------
25 
26 namespace xms
27 {
28 bool& xmAsserting();
29 extern int XM0, XM1; // replacements for 0 and 1 which do not cause warning C4127: conditional
30  // expression is constant
31 
33 // Some code copied from Mozilla. Code is under Mozilla Public License, v. 2.0
34 // which can be obtained at http://mozilla.org/MPL/2.0/.
36 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 enum xmresult { XR_SUCCESS = 0, XR_FAILURE = 1 };
40 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
43 #define XM_LIKELY(x) (!!(x))
44 #define XM_UNLIKELY(x) (!!(x))
45 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
50 inline unsigned int XM_FAILED_impl(xmresult _xmresult)
51 {
52  // return static_cast<uint32_t>(_xmresult) & 0x80000000;
53  return static_cast<unsigned int>(_xmresult) & 0x00000001;
54 }
55 #define XM_FAILED(_xmresult) ((bool)XM_UNLIKELY(XM_FAILED_impl(_xmresult)))
56 #define XM_SUCCEEDED(_xmresult) ((bool)XM_LIKELY(!XM_FAILED_impl(_xmresult)))
57 //------------------------------------------------------------------------------
58 //------------------------------------------------------------------------------
60 #ifdef _DEBUG
61 #define XM_ASSERT(x) \
62  \
63 { \
64  if (xms::xmAsserting()) \
65  assert(x); \
66  \
67 }
68 #else
69 #define XM_ASSERT(x) ((void)0)
70 #endif
71 //------------------------------------------------------------------------------
81 //------------------------------------------------------------------------------
82 #define XM_ENSURE_SUCCESS_5(x, ret, lvl, msg, ast) \
83  do \
84  { \
85  xms::xmresult __rv = x; /* Don't evaluate |x| more than once */ \
86  if (XM_FAILED(__rv)) \
87  { \
88  XM_LOG(lvl, msg); \
89  if (ast) \
90  { \
91  XM_ASSERT(!#x); \
92  } \
93  /*TS_WARN("XM_ENSURE_SUCCESS(" #x ", " #ret ") failed"); */ \
94  return ret; \
95  } \
96  } while (xms::XM0)
97 //------------------------------------------------------------------------------
105 //------------------------------------------------------------------------------
106 #define XM_ENSURE_SUCCESS_3(x, ret, ast) \
107  XM_ENSURE_SUCCESS_5(x, ret, xmlog::debug, "XM_ENSURE_SUCCESS(" #x ", " #ret ") failed", ast)
108 //------------------------------------------------------------------------------
111 //------------------------------------------------------------------------------
112 #define XM_ENSURE_SUCCESS(...) \
113  BOOST_PP_CAT( \
114  BOOST_PP_OVERLOAD(XM_ENSURE_SUCCESS_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
115  BOOST_PP_EMPTY())
116 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 #define XM_ENSURE_SUCCESS_NO_ASSERT(...) \
120  BOOST_PP_CAT( \
121  BOOST_PP_OVERLOAD(XM_ENSURE_SUCCESS_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
122  BOOST_PP_EMPTY())
123 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
134 #define XM_ENSURE_SUCCESS_T_5(x, ret, lvl, msg, ast) \
135  do \
136  { \
137  xms::xmresult __rv = x; /* Don't evaluate |x| more than once */ \
138  if (XM_FAILED(__rv)) \
139  { \
140  XM_LOG(lvl, msg); \
141  if (ast) \
142  { \
143  XM_ASSERT(!#x); \
144  } \
145  /*TS_WARN("XM_ENSURE_SUCCESS(" #x ", " #ret ") failed"); */ \
146  throw(ret); \
147  } \
148  } while (xms::XM0)
149 //------------------------------------------------------------------------------
157 //------------------------------------------------------------------------------
158 #define XM_ENSURE_SUCCESS_T_3(x, ret, ast) \
159  XM_ENSURE_SUCCESS_T_5(x, ret, xmlog::debug, "XM_ENSURE_SUCCESS_T(" #x ", " #ret ") failed", ast)
160 //------------------------------------------------------------------------------
163 //------------------------------------------------------------------------------
164 #define XM_ENSURE_SUCCESS_T(...) \
165  BOOST_PP_CAT( \
166  BOOST_PP_OVERLOAD(XM_ENSURE_SUCCESS_T_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
167  BOOST_PP_EMPTY())
168 //------------------------------------------------------------------------------
170 //------------------------------------------------------------------------------
171 #define XM_ENSURE_SUCCESS_T_NO_ASSERT(...) \
172  BOOST_PP_CAT( \
173  BOOST_PP_OVERLOAD(XM_ENSURE_SUCCESS_T_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
174  BOOST_PP_EMPTY())
175 //------------------------------------------------------------------------------
184 //------------------------------------------------------------------------------
185 #define XM_ENSURE_SUCCESS_VOID_4(x, lvl, msg, ast) \
186  do \
187  { \
188  xms::xmresult __rv = x; \
189  if (XM_FAILED(__rv)) \
190  { \
191  XM_LOG(lvl, msg); \
192  if (ast) \
193  { \
194  XM_ASSERT(!#x); \
195  } \
196  /*TS_WARN("XM_ENSURE_SUCCESS(" #x ") failed"); */ \
197  return; \
198  } \
199  } while (xms::XM0)
200 //------------------------------------------------------------------------------
207 //------------------------------------------------------------------------------
208 #define XM_ENSURE_SUCCESS_VOID_2(x, ast) \
209  XM_ENSURE_SUCCESS_VOID_4(x, xmlog::debug, "XM_ENSURE_SUCCESS_VOID(" #x ") failed", ast)
210 //------------------------------------------------------------------------------
214 //------------------------------------------------------------------------------
215 #define XM_ENSURE_SUCCESS_VOID(...) \
216  BOOST_PP_CAT( \
217  BOOST_PP_OVERLOAD(XM_ENSURE_SUCCESS_VOID_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
218  BOOST_PP_EMPTY())
219 //------------------------------------------------------------------------------
222 //------------------------------------------------------------------------------
223 #define XM_ENSURE_SUCCESS_VOID_NO_ASSERT(...) \
224  BOOST_PP_CAT( \
225  BOOST_PP_OVERLOAD(XM_ENSURE_SUCCESS_VOID_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
226  BOOST_PP_EMPTY())
227 //------------------------------------------------------------------------------
237 //------------------------------------------------------------------------------
238 #define XM_ENSURE_TRUE_5(x, ret, lvl, msg, ast) \
239  do \
240  { \
241  if (XM_UNLIKELY(!(x))) \
242  { \
243  XM_LOG(lvl, msg); \
244  if (ast) \
245  { \
246  XM_ASSERT(!#x); \
247  } \
248  /*TS_WARN("XM_ENSURE_TRUE(" #x ") failed"); */ \
249  return ret; \
250  } \
251  } while (xms::XM0)
252 //------------------------------------------------------------------------------
260 //------------------------------------------------------------------------------
261 #define XM_ENSURE_TRUE_3(x, ret, ast) \
262  XM_ENSURE_TRUE_5(x, ret, xmlog::debug, "XM_ENSURE_TRUE(" #x ", " #ret ") failed", ast)
263 //------------------------------------------------------------------------------
270 //------------------------------------------------------------------------------
271 #define XM_ENSURE_TRUE_2(x, ast) XM_ENSURE_TRUE_VOID_2(x, ast)
272 //------------------------------------------------------------------------------
275 //------------------------------------------------------------------------------
276 #define XM_ENSURE_TRUE(...) \
277  BOOST_PP_CAT(BOOST_PP_OVERLOAD(XM_ENSURE_TRUE_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
278  BOOST_PP_EMPTY())
279 //------------------------------------------------------------------------------
281 //------------------------------------------------------------------------------
282 #define XM_ENSURE_TRUE_NO_ASSERT(...) \
283  BOOST_PP_CAT(BOOST_PP_OVERLOAD(XM_ENSURE_TRUE_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
284  BOOST_PP_EMPTY())
285 //------------------------------------------------------------------------------
295 //------------------------------------------------------------------------------
296 #define XM_ENSURE_TRUE_T_5(x, ret, lvl, msg, ast) \
297  do \
298  { \
299  if (XM_UNLIKELY(!(x))) \
300  { \
301  XM_LOG(lvl, msg); \
302  if (ast) \
303  { \
304  XM_ASSERT(!#x); \
305  } \
306  /*TS_WARN("XM_ENSURE_TRUE(" #x ") failed"); */ \
307  throw(ret); \
308  } \
309  } while (xms::XM0)
310 //------------------------------------------------------------------------------
318 //------------------------------------------------------------------------------
319 #define XM_ENSURE_TRUE_T_3(x, ret, ast) \
320  XM_ENSURE_TRUE_T_5(x, ret, xmlog::debug, "XM_ENSURE_TRUE_T(" #x ", " #ret ") failed", ast)
321 //------------------------------------------------------------------------------
324 //------------------------------------------------------------------------------
325 #define XM_ENSURE_TRUE_T(...) \
326  BOOST_PP_CAT(BOOST_PP_OVERLOAD(XM_ENSURE_TRUE_T_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
327  BOOST_PP_EMPTY())
328 //------------------------------------------------------------------------------
330 //------------------------------------------------------------------------------
331 #define XM_ENSURE_TRUE_T_NO_ASSERT(...) \
332  BOOST_PP_CAT(BOOST_PP_OVERLOAD(XM_ENSURE_TRUE_T_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
333  BOOST_PP_EMPTY())
334 //------------------------------------------------------------------------------
343 //------------------------------------------------------------------------------
344 #define XM_ENSURE_TRUE_VOID_4(x, lvl, msg, ast) \
345  do \
346  { \
347  if (XM_UNLIKELY(!(x))) \
348  { \
349  XM_LOG(lvl, msg); \
350  if (ast) \
351  { \
352  XM_ASSERT(!#x); \
353  } \
354  /*TS_WARN("XM_ENSURE_TRUE(" #x ") failed"); */ \
355  return; \
356  } \
357  } while (xms::XM0)
358 //------------------------------------------------------------------------------
365 //------------------------------------------------------------------------------
366 #define XM_ENSURE_TRUE_VOID_2(x, ast) \
367  XM_ENSURE_TRUE_VOID_4(x, xmlog::debug, "XM_ENSURE_TRUE_VOID(" #x ") failed", ast)
368 //------------------------------------------------------------------------------
371 //------------------------------------------------------------------------------
372 #define XM_ENSURE_TRUE_VOID(...) \
373  BOOST_PP_CAT( \
374  BOOST_PP_OVERLOAD(XM_ENSURE_TRUE_VOID_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
375  BOOST_PP_EMPTY())
376 //------------------------------------------------------------------------------
378 //------------------------------------------------------------------------------
379 #define XM_ENSURE_TRUE_VOID_NO_ASSERT(...) \
380  BOOST_PP_CAT( \
381  BOOST_PP_OVERLOAD(XM_ENSURE_TRUE_VOID_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
382  BOOST_PP_EMPTY())
383 //------------------------------------------------------------------------------
393 //------------------------------------------------------------------------------
394 #define XM_ENSURE_FALSE_5(x, ret, lvl, msg, ast) XM_ENSURE_TRUE_5(!(x), ret, lvl, msg, ast)
395 //------------------------------------------------------------------------------
403 //------------------------------------------------------------------------------
404 #define XM_ENSURE_FALSE_3(x, ret, ast) \
405  XM_ENSURE_FALSE_5(x, ret, xmlog::debug, "XM_ENSURE_FALSE(" #x ", " #ret ") failed", ast)
406 //------------------------------------------------------------------------------
409 //------------------------------------------------------------------------------
410 #define XM_ENSURE_FALSE(...) \
411  BOOST_PP_CAT(BOOST_PP_OVERLOAD(XM_ENSURE_FALSE_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
412  BOOST_PP_EMPTY())
413 //------------------------------------------------------------------------------
415 //------------------------------------------------------------------------------
416 #define XM_ENSURE_FALSE_NO_ASSERT(...) \
417  BOOST_PP_CAT(BOOST_PP_OVERLOAD(XM_ENSURE_FALSE_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
418  BOOST_PP_EMPTY())
419 //------------------------------------------------------------------------------
429 //------------------------------------------------------------------------------
430 #define XM_ENSURE_FALSE_T_5(x, ret, lvl, msg, ast) XM_ENSURE_TRUE_T(!(x), ret, lvl, msg, ast)
431 //------------------------------------------------------------------------------
439 //------------------------------------------------------------------------------
440 #define XM_ENSURE_FALSE_T_3(x, ret, ast) \
441  XM_ENSURE_FALSE_T_5(x, ret, xmlog::debug, "XM_ENSURE_FALSE_T(" #x ", " #ret ") failed", ast)
442 //------------------------------------------------------------------------------
445 //------------------------------------------------------------------------------
446 #define XM_ENSURE_FALSE_T(...) \
447  BOOST_PP_CAT( \
448  BOOST_PP_OVERLOAD(XM_ENSURE_FALSE_T_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
449  BOOST_PP_EMPTY())
450 //------------------------------------------------------------------------------
452 //------------------------------------------------------------------------------
453 #define XM_ENSURE_FALSE_T_NO_ASSERT(...) \
454  BOOST_PP_CAT( \
455  BOOST_PP_OVERLOAD(XM_ENSURE_FALSE_T_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
456  BOOST_PP_EMPTY())
457 //------------------------------------------------------------------------------
466 //------------------------------------------------------------------------------
467 #define XM_ENSURE_FALSE_VOID_4(x, lvl, msg, ast) XM_ENSURE_TRUE_VOID_4(!(x), lvl, msg, ast)
468 //------------------------------------------------------------------------------
475 //------------------------------------------------------------------------------
476 #define XM_ENSURE_FALSE_VOID_2(x, ast) \
477  XM_ENSURE_FALSE_VOID_4(x, xmlog::debug, "XM_ENSURE_FALSE_VOID(" #x ") failed", ast)
478 //------------------------------------------------------------------------------
481 //------------------------------------------------------------------------------
482 #define XM_ENSURE_FALSE_VOID(...) \
483  BOOST_PP_CAT( \
484  BOOST_PP_OVERLOAD(XM_ENSURE_FALSE_VOID_, __VA_ARGS__, xms::XM1)(__VA_ARGS__, xms::XM1), \
485  BOOST_PP_EMPTY())
486 //------------------------------------------------------------------------------
488 //------------------------------------------------------------------------------
489 #define XM_ENSURE_FALSE_VOID_NO_ASSERT(...) \
490  BOOST_PP_CAT( \
491  BOOST_PP_OVERLOAD(XM_ENSURE_FALSE_VOID_, __VA_ARGS__, xms::XM0)(__VA_ARGS__, xms::XM0), \
492  BOOST_PP_EMPTY())
493 //------------------------------------------------------------------------------
497 //------------------------------------------------------------------------------
498 #define XM_RETURN_XMRESULT(x) \
499  { \
500  return (XM_LIKELY(x) ? XR_SUCCESS : XR_FAILURE); \
501  }
502 } // namespace xms
bool & xmAsserting()
Stores whether we want XM_ASSERT to do something or not.
Definition: XmError.cpp:32
xmresult
Like Mozilla&#39;s nsresult.
Definition: XmError.h:39
unsigned int XM_FAILED_impl(xmresult _xmresult)
Like Mozilla&#39;s NS_FAILED in their nsError.h file.
Definition: XmError.h:50
Routines for creating and writing to logs and stacking errors.
int XM0
avoid warning
Definition: XmError.cpp:25
int XM1
avoid warning
Definition: XmError.cpp:26