asserts.hpp

Go to the documentation of this file.
00001 /* $Id: asserts.hpp 54254 2012-05-20 09:56:10Z mordante $ */
00002 /*
00003    Copyright (C) 2008 by David White <dave@whitevine.net>
00004                  2008 - 2012 by Richard Kettering <kettering.richard@gmail.com>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 
00018 #ifndef ASSERTS_HPP_INCLUDED
00019 #define ASSERTS_HPP_INCLUDED
00020 
00021 #include <cstdlib>
00022 #include <iostream>
00023 #include <csignal>
00024 
00025 #ifdef _MSC_VER
00026 #define BREAKPOINT() __debugbreak()
00027 #define WES_HALT() do { BREAKPOINT(); exit(1); } while (false)
00028 
00029 #elif defined(__GNUG__) && (defined(__i386__) || defined(__x86_64__)) \
00030   && !defined(__native_client__)
00031 #define BREAKPOINT() asm("int3")
00032 #define WES_HALT() do { BREAKPOINT(); abort(); } while (false)
00033 
00034 #elif defined(SIGTRAP)
00035 #define BREAKPOINT() do{ ::std::raise(SIGTRAP); } while (0)
00036 #define WES_HALT() do { BREAKPOINT(); abort(); } while (false)
00037 
00038 #else
00039 #define BREAKPOINT()
00040 #define WES_HALT() abort()
00041 #endif
00042 
00043 #define ERROR_LOG(a) do { \
00044     std::cerr << __FILE__ << ":" << __LINE__ << " ASSSERTION FAILED: " << a << std::endl; \
00045     WES_HALT(); \
00046     } while (false)
00047 
00048 //for custom logging.  Example usage:
00049 //ASSERT_LOG(x != y, "x not equal to y. Value of x: " << x << ", y: " << y);
00050 #define ASSERT_LOG(a,b) if (!(a)) { ERROR_LOG(b); } else (void)0
00051 
00052 #define FATAL_ERROR ERROR_LOG("FATAL ERROR")
00053 
00054 /**
00055  * Marker for code that should be unreachable.
00056  *
00057  * This can be used to avoid compiler warnings and to detect logic errors in the code.
00058  */
00059 #define UNREACHABLE_CODE ERROR_LOG("REACHED UNREACHABLE CODE")
00060 
00061 //helper macro for the simple operator cases defined below
00062 #define ASSERT_OP(a,op,b) ASSERT_LOG((a) op (b), #a " " #op " " #b " (" << (a) << " " #op " " << (b) << ")")
00063 
00064 //various asserts of standard "equality" tests, such as "equals", "not equals", "greater than", etc.
00065 //Example usage ASSERT_GE(x, y);
00066 //on failure this will cerr "assertion failed: x >= y (value_of_x >= value_of_y)"
00067 #define ASSERT_EQ(a,b) ASSERT_OP(a,==,b)
00068 #define ASSERT_NE(a,b) ASSERT_OP(a,!=,b)
00069 #define ASSERT_GE(a,b) ASSERT_OP(a,>=,b)
00070 #define ASSERT_LE(a,b) ASSERT_OP(a,<=,b)
00071 #define ASSERT_GT(a,b) ASSERT_OP(a,>,b)
00072 #define ASSERT_LT(a,b) ASSERT_OP(a,<,b)
00073 
00074 
00075 #endif
00076 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:34 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs