antioch-0.4.0
antioch_asserts.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_ASSERTS_H
28 #define ANTIOCH_ASSERTS_H
29 
30 // Antioch
32 #include "antioch_config.h" // for ANTIOCH_HAVE_CXX11
33 
34 // C++
35 #include <iostream>
36 #include <iomanip>
37 
38 // Most of the following macros are "savagely copy and pasted from libMesh"
39 // then modified to avoid name collisions
40 
41 #define antioch_here() do { std::cerr << __FILE__ << ", line " << __LINE__ << ", compiled " << __DATE__ << " at " << __TIME__ << std::endl; } while (0)
42 
43 // The antioch_assert() macro acts like C's assert(), but throws a
44 // antioch_error() (including stack trace, etc) instead of just exiting
45 
46 // When not debugging, we don't test any asserts
47 #ifdef NDEBUG
48 #define antioch_assert(asserted) ((void) 0)
49 #define antioch_assert_msg(asserted, msg) ((void) 0)
50 #define antioch_assert_equal_to(expr1,expr2) ((void) 0)
51 #define antioch_assert_not_equal_to(expr1,expr2) ((void) 0)
52 #define antioch_assert_less(expr1,expr2) ((void) 0)
53 #define antioch_assert_greater(expr1,expr2) ((void) 0)
54 #define antioch_assert_less_equal(expr1,expr2) ((void) 0)
55 #define antioch_assert_greater_equal(expr1,expr2) ((void) 0)
56 #else
57 
58 #define antioch_assert(asserted) do { if (!(asserted)) { std::cerr << "Assertion `" #asserted "' failed." << std::endl; antioch_error(); } } while(0)
59 
60 // When using C++11, we can test asserts comparing two different types
61 // robustly
62 // #if __cplusplus > 199711L // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=1773
63 #ifdef ANTIOCH_HAVE_CXX11
64 #define antioch_assert_equal_to(expr1,expr2) 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) << std::endl; antioch_error(); } } while(0)
65 #define antioch_assert_not_equal_to(expr1,expr2) 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) << std::endl; antioch_error(); } } while(0)
66 #define antioch_assert_less(expr1,expr2) 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) << std::endl; antioch_error(); } } while(0)
67 #define antioch_assert_greater(expr1,expr2) 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) << std::endl; antioch_error(); } } while(0)
68 #define antioch_assert_less_equal(expr1,expr2) 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) << std::endl; antioch_error(); } } while(0)
69 #define antioch_assert_greater_equal(expr1,expr2) 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) << std::endl; antioch_error(); } } while(0)
70 
71 // When using C++98, we let the compiler pick the type conversion and
72 // hope for the best.
73 #else
74 #define antioch_assert_equal_to(expr1,expr2) do { if (!(expr1 == expr2)) { std::cerr << "Assertion `" #expr1 " == " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << std::endl; antioch_error(); } } while(0)
75 #define antioch_assert_not_equal_to(expr1,expr2) do { if (!(expr1 != expr2)) { std::cerr << "Assertion `" #expr1 " != " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << std::endl; antioch_error(); } } while(0)
76 #define antioch_assert_less(expr1,expr2) do { if (!(expr1 < expr2)) { std::cerr << "Assertion `" #expr1 " < " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << std::endl; antioch_error(); } } while(0)
77 #define antioch_assert_greater(expr1,expr2) do { if (!(expr1 > expr2)) { std::cerr << "Assertion `" #expr1 " > " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << std::endl; antioch_error(); } } while(0)
78 #define antioch_assert_less_equal(expr1,expr2) do { if (!(expr1 <= expr2)) { std::cerr << "Assertion `" #expr1 " <= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << std::endl; antioch_error(); } } while(0)
79 #define antioch_assert_greater_equal(expr1,expr2) do { if (!(expr1 >= expr2)) { std::cerr << "Assertion `" #expr1 " >= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << std::endl; antioch_error(); } } while(0)
80 
81 #endif // C++11
82 
83 #endif // NDEBUG
84 
85 
86 // The antioch_do_once macro helps us avoid redundant repeated
87 // repetitions of the same warning messages
88 #undef antioch_do_once
89 #define antioch_do_once(do_this) \
90  do { \
91  static bool did_this_already = false; \
92  if (!did_this_already) { \
93  did_this_already = true; \
94  do_this; \
95  } } while (0)
96 
97 
98 // Using cout for less redundancy in parallel
99 #define antioch_warning(message) \
100  antioch_do_once(std::cout << message \
101  << __FILE__ << ", line " << __LINE__ << ", compiled " << __DATE__ << " at " << __TIME__ << " ***" << std::endl;)
102 
103 
104 #define antioch_error() do { antioch_here(); ANTIOCH_THROW(Antioch::LogicError()); } while(0)
105 #define antioch_not_implemented() do { antioch_here(); ANTIOCH_THROW(Antioch::NotImplemented()); } while(0)
106 #define antioch_file_error(filename) do { antioch_here(); ANTIOCH_THROW(Antioch::FileError(filename)); } while(0)
107 #define antioch_unit_error(description) do { antioch_here(); ANTIOCH_THROW(Antioch::UnitError(description)); } while(0)
108 #define antioch_parsing_error(description) do { antioch_here(); ANTIOCH_THROW(Antioch::ParsingError(description)); } while(0)
109 
110 #define antioch_parameter_required(parameter,defaultpar) \
111  antioch_warning("\n*** Warning, The parameter " << parameter << " is not provided\n" << "default parameter is " << defaultpar << std::endl)
112 
113 #define antioch_unit_required(parameter,defaultunit) \
114  antioch_warning("\n*** Warning, The parameter " << parameter << " is not given a unit\n" << "default unit given is " << defaultunit << std::endl)
115 
116 // The antioch_deprecated macro warns that you are using obsoleted code
117 #define antioch_deprecated() \
118  antioch_warning( "\n*** Warning, This code is deprecated, and likely to be removed in future library versions!\n")
119 
120 
121 // Just outputing to std::cerr
122 #define antioch_msg(errmsg) do {std::cerr << errmsg << std::endl;} while(0)
123 #define antioch_error_msg(errmsg) do {antioch_msg(errmsg); antioch_error();} while(0)
124 #define antioch_not_implemented_msg(errmsg) do {antioch_msg(errmsg); antioch_not_implemented();} while(0)
125 
126 // Encapsulate guarding static_assert until we require C++11
127 #ifdef ANTIOCH_HAVE_CXX_STATIC_ASSERT
128 #define antioch_static_assert(cond,errmsg) static_assert(cond,errmsg)
129 #else
130 #define antioch_static_assert(cond,errmsg) ((void) 0)
131 #endif
132 
133 
134 // Encapsulate guarding static_assert until we require C++11
135 // This one include a fallback to a runtime error.
136 #ifdef ANTIOCH_HAVE_CXX_STATIC_ASSERT
137 #define antioch_static_assert_runtime_fallback(cond,errmsg) static_assert( cond, errmsg )
138 #else
139 #define antioch_static_assert_runtime_fallback(cond,errmsg) \
140  if( !cond ) \
141  antioch_msg_error(errmsg)
142 #endif
143 
144 #endif // ANTIOCH_ASSERTS_H

Generated on Thu Jul 7 2016 11:09:46 for antioch-0.4.0 by  doxygen 1.8.8