queso-0.53.0
asserts.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 //
26 // nick (nicholas.malaya) providing a direct port of the antioch assertions
27 // to queso (see: https://github.com/libantioch/antioch/blob/master/src/utilities/include/antioch/antioch_asserts.h)
28 //
29 // adding MPI-friendly exits
30 //
31 
32 #ifndef QUESO_ASSERTS_H
33 #define QUESO_ASSERTS_H
34 
35 #include "exceptions.h"
36 #include "queso/config_queso.h" // for QUESO_HAVE_CXX11
37 
38 // C++
39 #include <iostream>
40 #include <iomanip>
41 
42 #define queso_here() do { std::cerr << __FILE__ << ", line " << __LINE__ << ", compiled " << __DATE__ << " at " << __TIME__ << std::endl; } while (0)
43 
44 // queso_error and kin throw exceptions to indicate various possible
45 // errors
46 
47 #define queso_error_msg(msg) do { queso_here(); std::cerr << msg << std::endl; QUESO_THROW(QUESO::LogicError()); } while(0)
48 
49 #define queso_not_implemented_msg(msg) do { queso_here(); std::cerr << msg << std::endl; QUESO_THROW(QUESO::NotImplemented()); } while(0)
50 
51 #define queso_file_error_msg(filename, msg) do { queso_here(); std::cerr << msg << std::endl; QUESO_THROW(QUESO::FileError(filename)); } while(0)
52 
53 #define queso_error() \
54  queso_error_msg("")
55 
56 #define queso_not_implemented() \
57  queso_not_implemented_msg("")
58 
59 #define queso_file_error(filename) \
60  queso_file_error_msg(filename, "")
61 
62 // The queso_assert() macro acts like C's assert(), but throws a
63 // queso_error() (enabling exception handling, stack trace, etc)
64 // instead of just exiting.
65 
66 // The queso_require() macro does the same, but remains active even
67 // when NDEBUG is not defined
68 
69 #define queso_require_msg(asserted, msg) do { if (!(asserted)) { std::cerr << "Assertion `" #asserted "' failed.\n" << msg << std::endl; queso_error(); } } while(0)
70 
71 // When using C++11, we can test asserts comparing two different types
72 // robustly
73 // #if __cplusplus > 199711L // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=1773
74 #ifdef QUESO_HAVE_CXX11
75 #define queso_require_equal_to_msg(expr1,expr2,msg) do { typedef decltype(expr1) type1; typedef decltype(expr2) type2; if (!((expr1 == static_cast<type1>(expr2)) && static_cast<type2>(expr1) == expr2)) { std::cerr << "Assertion `" #expr1 " == " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
76 #define queso_require_not_equal_to_msg(expr1,expr2,msg) do { typedef decltype(expr1) type1; typedef decltype(expr2) type2; if (!((expr1 != static_cast<type1>(expr2)) && (static_cast<type2>(expr1) != expr2))) { std::cerr << "Assertion `" #expr1 " != " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
77 #define queso_require_less_msg(expr1,expr2,msg) do { typedef decltype(expr1) type1; typedef decltype(expr2) type2; if (!((static_cast<type2>(expr1) < expr2) && (expr1 < static_cast<type1>(expr2)))) { std::cerr << "Assertion `" #expr1 " < " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
78 #define queso_require_greater_msg(expr1,expr2,msg) do { typedef decltype(expr1) type1; typedef decltype(expr2) type2; if (!((static_cast<type2>(expr1) > expr2) && (expr1 > static_cast<type1>(expr2)))) { std::cerr << "Assertion `" #expr1 " > " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
79 #define queso_require_less_equal_msg(expr1,expr2,msg) do { typedef decltype(expr1) type1; typedef decltype(expr2) type2; if (!((static_cast<type2>(expr1) <= expr2) && (expr1 <= static_cast<type1>(expr2)))) { std::cerr << "Assertion `" #expr1 " <= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
80 #define queso_require_greater_equal_msg(expr1,expr2,msg) do { typedef decltype(expr1) type1; typedef decltype(expr2) type2; if (!((static_cast<type2>(expr1) >= expr2) && (expr1 >= static_cast<type1>(expr2)))) { std::cerr << "Assertion `" #expr1 " >= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
81 
82 // When using C++98, we let the compiler pick the type conversion and
83 // hope for the best.
84 #else
85 #define queso_require_equal_to_msg(expr1,expr2,msg) do { if (!(expr1 == expr2)) { std::cerr << "Assertion `" #expr1 " == " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
86 #define queso_require_not_equal_to_msg(expr1,expr2,msg) do { if (!(expr1 != expr2)) { std::cerr << "Assertion `" #expr1 " != " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
87 #define queso_require_less_msg(expr1,expr2,msg) do { if (!(expr1 < expr2)) { std::cerr << "Assertion `" #expr1 " < " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
88 #define queso_require_greater_msg(expr1,expr2,msg) do { if (!(expr1 > expr2)) { std::cerr << "Assertion `" #expr1 " > " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
89 #define queso_require_less_equal_msg(expr1,expr2,msg) do { if (!(expr1 <= expr2)) { std::cerr << "Assertion `" #expr1 " <= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
90 #define queso_require_greater_equal_msg(expr1,expr2,msg) do { if (!(expr1 >= expr2)) { std::cerr << "Assertion `" #expr1 " >= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl; queso_error(); } } while(0)
91 
92 #endif // C++11
93 
94 // When not debugging, we don't test any asserts
95 #ifdef NDEBUG
96 
97 #define queso_assert_msg(asserted,msg) ((void) 0)
98 #define queso_assert_equal_to_msg(expr1,expr2,msg) ((void) 0)
99 #define queso_assert_not_equal_to_msg(expr1,expr2,msg) ((void) 0)
100 #define queso_assert_less_msg(expr1,expr2,msg) ((void) 0)
101 #define queso_assert_greater_msg(expr1,expr2,msg) ((void) 0)
102 #define queso_assert_less_equal_msg(expr1,expr2,msg) ((void) 0)
103 #define queso_assert_greater_equal_msg(expr1,expr2,msg) ((void) 0)
104 
105 #else
106 
107 #define queso_assert_msg(asserted,msg) \
108  queso_require_msg(asserted,msg)
109 #define queso_assert_equal_to_msg(expr1,expr2,msg) \
110  queso_require_equal_to_msg(expr1,expr2,msg)
111 #define queso_assert_not_equal_to_msg(expr1,expr2,msg) \
112  queso_require_not_equal_to_msg(expr1,expr2,msg)
113 #define queso_assert_less_msg(expr1,expr2,msg) \
114  queso_require_less_msg(expr1,expr2,msg)
115 #define queso_assert_greater_msg(expr1,expr2,msg) \
116  queso_require_greater_msg(expr1,expr2,msg)
117 #define queso_assert_less_equal_msg(expr1,expr2,msg) \
118  queso_require_less_equal_msg(expr1,expr2,msg)
119 #define queso_assert_greater_equal_msg(expr1,expr2,msg) \
120  queso_require_greater_equal_msg(expr1,expr2,msg)
121 
122 #endif // NDEBUG
123 
124 #define queso_require(asserted) \
125  queso_require_msg(asserted, "")
126 
127 #define queso_require_equal_to(expr1,expr2) \
128  queso_require_equal_to_msg(expr1,expr2,"")
129 
130 #define queso_require_not_equal_to(expr1,expr2) \
131  queso_require_not_equal_to_msg(expr1,expr2,"") \
132 
133 #define queso_require_less(expr1,expr2) \
134  queso_require_less_msg(expr1,expr2,"")
135 
136 #define queso_require_greater(expr1,expr2) \
137  queso_require_greater_msg(expr1,expr2,"")
138 
139 #define queso_require_less_equal(expr1,expr2) \
140  queso_require_less_equal_msg(expr1,expr2,"")
141 
142 #define queso_require_greater_equal(expr1,expr2) \
143  queso_require_greater_equal_msg(expr1,expr2,"")
144 
145 
146 #define queso_assert(asserted) \
147  queso_assert_msg(asserted, "")
148 
149 #define queso_assert_equal_to(expr1,expr2) \
150  queso_assert_equal_to_msg(expr1,expr2,"")
151 
152 #define queso_assert_not_equal_to(expr1,expr2) \
153  queso_assert_not_equal_to_msg(expr1,expr2,"") \
154 
155 #define queso_assert_less(expr1,expr2) \
156  queso_assert_less_msg(expr1,expr2,"")
157 
158 #define queso_assert_greater(expr1,expr2) \
159  queso_assert_greater_msg(expr1,expr2,"")
160 
161 #define queso_assert_less_equal(expr1,expr2) \
162  queso_assert_less_equal_msg(expr1,expr2,"")
163 
164 #define queso_assert_greater_equal(expr1,expr2) \
165  queso_assert_greater_equal_msg(expr1,expr2,"")
166 
167 
168 
169 #endif // QUESO_ASSERTS_H

Generated on Thu Jun 11 2015 13:52:31 for queso-0.53.0 by  doxygen 1.8.5