]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Misc/CustomAssert.h
update changelog, target experimental
[flightcrew.git] / src / FlightCrew / Misc / CustomAssert.h
1 /*\r
2 * Copyright (c) 2008, Power of Two Games LLC\r
3 *               2010, Strahinja Markovic\r
4 *\r
5 * Redistribution and use in source and binary forms, with or without\r
6 * modification, are permitted provided that the following conditions are met:\r
7 *     * Redistributions of source code must retain the above copyright\r
8 *       notice, this list of conditions and the following disclaimer.\r
9 *     * Redistributions in binary form must reproduce the above copyright\r
10 *       notice, this list of conditions and the following disclaimer in the\r
11 *       documentation and/or other materials provided with the distribution.\r
12 *     * Neither the name of Power of Two Games LLC nor the\r
13 *       names of its contributors may be used to endorse or promote products\r
14 *       derived from this software without specific prior written permission.\r
15 *\r
16 * THIS SOFTWARE IS PROVIDED BY POWER OF TWO GAMES LLC ``AS IS'' AND ANY\r
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
19 * DISCLAIMED. IN NO EVENT SHALL POWER OF TWO GAMES LLC BE LIABLE FOR ANY\r
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 */\r
27 \r
28 #ifndef CUSTOM_ASSERT_H\r
29 #define CUSTOM_ASSERT_H\r
30 \r
31 namespace assert_ns { namespace Assert\r
32 {\r
33         enum FailBehavior\r
34         {\r
35                 Halt,\r
36                 Continue,\r
37         };\r
38 \r
39         typedef FailBehavior (*Handler)(const char* condition, \r
40                                                                         const char* msg, \r
41                                                                         const char* file, \r
42                                                                         int line);\r
43 \r
44         Handler GetHandler();\r
45         void SetHandler(Handler newHandler);\r
46 \r
47         FailBehavior ReportFailure(const char* condition, \r
48                                                            const char* file, \r
49                                                            int line, \r
50                                                            const char* msg, ...);\r
51 }}\r
52 \r
53 #if defined(_MSC_VER)\r
54 #   define X_HALT() __debugbreak()\r
55 #elif defined(__GNUC__)\r
56 #   define X_HALT() __builtin_trap()\r
57 #else\r
58 #    define X_HALT() exit(__LINE__)\r
59 #endif\r
60 \r
61 #define X_UNUSED(x) do { (void)sizeof(x); } while(0)\r
62 \r
63 #ifndef NDEBUG\r
64         #define X_ASSERT(cond) \\r
65                 do \\r
66                 { \\r
67                         if (!(cond)) \\r
68                         { \\r
69                                 if (assert_ns::Assert::ReportFailure(#cond, __FILE__, __LINE__, 0) == \\r
70                                         assert_ns::Assert::Halt) \\r
71                                         X_HALT(); \\r
72                         } \\r
73                 } while(0)\r
74 \r
75         #define X_ASSERT_MSG(cond, msg, ...) \\r
76                 do \\r
77                 { \\r
78                         if (!(cond)) \\r
79                         { \\r
80                                 if (assert_ns::Assert::ReportFailure(#cond, __FILE__, __LINE__, (msg), __VA_ARGS__) == \\r
81                                         assert_ns::Assert::Halt) \\r
82                                         X_HALT(); \\r
83                         } \\r
84                 } while(0)\r
85 \r
86         #define X_ASSERT_FAIL(msg, ...) \\r
87                 do \\r
88                 { \\r
89                         if (assert_ns::Assert::ReportFailure(0, __FILE__, __LINE__, (msg), __VA_ARGS__) == \\r
90                                 assert_ns::Assert::Halt) \\r
91                         X_HALT(); \\r
92                 } while(0)\r
93 \r
94         #define X_VERIFY(cond) X_ASSERT(cond)\r
95         #define X_VERIFY_MSG(cond, msg, ...) X_ASSERT_MSG(cond, msg, ##__VA_ARGS__)\r
96 #else\r
97         #define X_ASSERT(condition) \\r
98                 do { X_UNUSED(condition); } while(0)\r
99         #define X_ASSERT_MSG(condition, msg, ...) \\r
100                 do { X_UNUSED(condition); X_UNUSED(msg); } while(0)\r
101         #define X_ASSERT_FAIL(msg, ...) \\r
102                 do { X_UNUSED(msg); } while(0)\r
103         #define X_VERIFY(cond) (void)(cond)\r
104         #define X_VERIFY_MSG(cond, msg, ...) \\r
105                 do { (void)(cond); X_UNUSED(msg); } while(0)\r
106 #endif\r
107 \r
108 #define X_STATIC_ASSERT(x) \\r
109         typedef char StaticAssert[(x) ? 1 : -1];\r
110 \r
111 #endif // CUSTOM_ASSERT_H\r