The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
asserts.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 by David White <[email protected]>
3  2008 - 2015 by Richard Kettering <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 
17 #ifndef ASSERTS_HPP_INCLUDED
18 #define ASSERTS_HPP_INCLUDED
19 
20 #include <cstdlib>
21 #include <iostream>
22 #include <csignal>
23 
24 #ifdef _MSC_VER
25 #define BREAKPOINT() __debugbreak()
26 #define WES_HALT() do { BREAKPOINT(); exit(1); } while (false)
27 
28 #elif defined(__GNUG__) && (defined(__i386__) || defined(__x86_64__)) \
29  && !defined(__native_client__)
30 #define BREAKPOINT() asm("int3")
31 #define WES_HALT() do { BREAKPOINT(); abort(); } while (false)
32 
33 #elif defined(SIGTRAP)
34 #define BREAKPOINT() do{ ::std::raise(SIGTRAP); } while (0)
35 #define WES_HALT() do { BREAKPOINT(); abort(); } while (false)
36 
37 #else
38 #define BREAKPOINT()
39 #define WES_HALT() abort()
40 #endif
41 
42 #define ERROR_LOG(a) do { \
43  std::cerr << __FILE__ << ":" << __LINE__ << " ASSSERTION FAILED: " << a << std::endl; \
44  WES_HALT(); \
45  } while (false)
46 
47 //for custom logging. Example usage:
48 //ASSERT_LOG(x != y, "x not equal to y. Value of x: " << x << ", y: " << y);
49 #define ASSERT_LOG(a,b) if (!(a)) { ERROR_LOG(b); } else (void)0
50 
51 #define FATAL_ERROR ERROR_LOG("FATAL ERROR")
52 
53 /**
54  * Marker for code that should be unreachable.
55  *
56  * This can be used to avoid compiler warnings and to detect logic errors in the code.
57  */
58 #define UNREACHABLE_CODE ERROR_LOG("REACHED UNREACHABLE CODE")
59 
60 //helper macro for the simple operator cases defined below
61 #define ASSERT_OP(a,op,b) ASSERT_LOG((a) op (b), #a " " #op " " #b " (" << (a) << " " #op " " << (b) << ")")
62 
63 //various asserts of standard "equality" tests, such as "equals", "not equals", "greater than", etc.
64 //Example usage ASSERT_GE(x, y);
65 //on failure this will cerr "assertion failed: x >= y (value_of_x >= value_of_y)"
66 #define ASSERT_EQ(a,b) ASSERT_OP(a,==,b)
67 #define ASSERT_NE(a,b) ASSERT_OP(a,!=,b)
68 #define ASSERT_GE(a,b) ASSERT_OP(a,>=,b)
69 #define ASSERT_LE(a,b) ASSERT_OP(a,<=,b)
70 #define ASSERT_GT(a,b) ASSERT_OP(a,>,b)
71 #define ASSERT_LT(a,b) ASSERT_OP(a,<,b)
72 
73 
74 #endif
75