source: trunk/src/lol/base/assert.h @ 2265

Last change on this file since 2265 was 2265, checked in by sam, 7 years ago

core: implement a cool ASSERT() macro.

File size: 2.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if !defined __LOL_BASE_ASSERT_H__
12#define __LOL_BASE_ASSERT_H__
13
14#include <cstdlib>
15
16namespace lol
17{
18
19static inline void Abort()
20{
21#if defined __CELLOS_LV2__
22    *(uint32_t *)NULL = 0xdead;
23#else
24    std::abort();
25#endif
26}
27
28#define LOL_CALL(macro, args) macro args
29#define LOL_EVAL(a) a
30#define LOL_1ST(a, ...) a
31
32#define LOL_NUM2(a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, \
33                 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \
34                 a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \
35                 a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, \
36                 a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \
37                 a51, a52, a53, a54, a55, a56, a57, a58, a59, a60, \
38                 a61, a62, a63, ...) a63
39#define LOL_NUM(...) \
40    LOL_EVAL(LOL_NUM2(__VA_ARGS__, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \
41                                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \
42                                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \
43                                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \
44                                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \
45                                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \
46                                   2, 1, TOO_MANY_ARGUMENTS))
47
48/* Three levels of dispatch are needed because of Visual Studio's bizarre
49 * handling of __VA_ARGS__ inside macro calls */
50#define LOL_CAT3(a, b) a##b
51#define LOL_CAT2(a, b) LOL_CAT3(a,b)
52#define LOL_CAT(a, b) LOL_CAT2(a,b)
53
54#define LOL_ERROR_1(t) \
55    Log::Error("assertion failure: " #t "\n")
56
57#define LOL_ERROR_2(t, s) \
58    Log::Error("assertion failure: %s\n", s)
59
60#define LOL_ERROR_3(t, s, ...) \
61    Log::Error("assertion failure: " s "\n", __VA_ARGS__)
62
63#if FINAL_RELEASE
64#   define ASSERT(...) (void)sizeof(LOL_CALL(LOL_1ST, (__VA_ARGS__)))
65#else
66#   define ASSERT(...) \
67        if (!(LOL_CALL(LOL_1ST, (__VA_ARGS__)))) \
68        { \
69            LOL_CALL(LOL_CAT(LOL_ERROR_, LOL_CALL(LOL_NUM, (__VA_ARGS__))), \
70                     (__VA_ARGS__)); \
71            Abort(); \
72        }
73#endif
74
75} /* namespace lol */
76
77#endif // __LOL_BASE_ASSERT_H__
78
Note: See TracBrowser for help on using the repository browser.