Flow123d  release_1.8.2-1603-g0109a2b
global_defs.h
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file global_defs.h
15  * @brief Global macros to enhance readability and debugging, general constants.
16  */
17 
18 #ifndef GLOBAL_DEFS_H
19 #define GLOBAL_DEFS_H
20 
21 //Make sure that headers necessary for following macros are included.
22 #include <stdint.h>
23 #include <string.h>
24 #include <stdlib.h>
25 
26 #include "system/exc_common.hh"
27 #include "config.h"
28 #include "mpi.h"
29 #include "asserts.hh"
30 
31 /*! @brief Debugging macros.
32  *
33  * The macro OLD_ASSERT has to be used for assertion tests. An error occures if
34  * given condition is violated. Macro accepts additional variables to print.
35  *
36  * Example:
37  * @verbatim
38  * OLD_ASSERT( i<size , "Array X overflow: index %d >= alocated size %d.\n",i,size);
39  * @endverbatim
40  *
41  * The macro INPUT_CHECK should be used for assertions about user input. So
42  * they produce User Error instead of Program error.
43  *
44  * The macro DBGMSG should be used for debugging messages,
45  * so they can be removed in production version.
46  *
47  * OLD_WARN_ASSERT - can be used for consistency tests in debugging version.
48  *
49  * @{
50  */
51 #define INPUT_CHECK(i,...) do { if (!(i)) xprintf(UsrErr,__VA_ARGS__); } while (0)
52 
53 /**
54  * Actually there are following debugging switches
55  * FLOW123D_DEBUG_MESSAGES - use various debugging messages introduced by DBGMSG
56  * FLOW123D_DEBUG_ASSERTS - use assertion checks introduced by ASSERT
57  * FLOW123D_DEBUG_PROFILER - use profiling introduced by START_TIMER, END_TIMER
58  *
59  * You can turn all off defining: FLOW123D_NODEBUG
60  * or turn all on defining: FLOW123D_DEBUG
61  *
62  * FLOW123D_DEBUG overrides FLOW123D_NODEBUG
63  */
64 
65 
66 #ifdef FLOW123D_NODEBUG
67 
68 #undef FLOW123D_DEBUG_MESSAGES
69 #undef FLOW123D_DEBUG_ASSERTS
70 #undef FLOW123D_DEBUG_PROFILER
71 
72 #endif
73 
74 
75 #ifdef FLOW123D_DEBUG
76 
77 #define FLOW123D_DEBUG_MESSAGES
78 #define FLOW123D_DEBUG_ASSERTS
79 #define FLOW123D_DEBUG_PROFILER
80 
81 #endif
82 
83 
84 
85 #ifdef FLOW123D_DEBUG_ASSERTS
86 
87 /**
88  * Just quick hack to fix some unit tests.
89  * TODO:
90  * We should make better implementation, rather minimizing
91  * usage of macros. And make robust "system" part, that
92  * is MPI aware, but not MPI dependent.
93  */
94 //#ifdef FLOW123D_DEBUG_ASSERTS_WITHOUT_MPI
95 //#define MPI_Comm_rank(A, B)
96 //#endif // FLOW123D_DEBUG_ASSERTS_WITHOUT_MPI
97 
98 #define OLD_ASSERT(i,...) do {\
99  if (!(i)) {\
100  char msg[1024];\
101  sprintf( msg, __VA_ARGS__);\
102  int rank=-1;\
103  THROW( ExcAssertMsg() << EI_Message(std::string(msg)) << EI_MPI_Rank(rank) );\
104  }} while (0)
105 
106 #define OLD_WARN_ASSERT(i,...) do { if (!(i)) xprintf(Warn,__VA_ARGS__); } while (0)
107 
108 #define OLD_ASSERT_EQUAL( a, b) do {\
109  stringstream ss; ss << (a) << " != " << (b); \
110  OLD_ASSERT( ((a) == (b)), "Violated assert: %s == %s,\n observed: %s.\n",#a, #b, ss.str().c_str()); \
111  } while (0)
112 
113 #define OLD_ASSERT_LESS( a, b) do {\
114  stringstream ss; ss << (a) << " >= " << (b); \
115  OLD_ASSERT( ((a) < (b)) , "Violated assert: %s < %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
116  } while (0)
117 
118 #define OLD_ASSERT_LE( a, b) do {\
119  stringstream ss; ss << (a) << " > " << (b); \
120  OLD_ASSERT( ((a) <= (b)) , "Violated assert: %s <= %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
121  } while (0)
122 
123 #define OLD_ASSERT_PTR( ptr ) do {\
124  OLD_ASSERT( ((ptr) != nullptr) , "Null pointer: %s\n", #ptr ); \
125  } while (0)
126 #else
127 
128 #define OLD_ASSERT(...)
129 #define OLD_WARN_ASSERT(...)
130 #define OLD_ASSERT_EQUAL( a, b)
131 #define OLD_ASSERT_LESS( a, b)
132 #define OLD_ASSERT_LE( a, b)
133 #define OLD_ASSERT_PTR( ptr )
134 
135 #endif
136 
137 
138 /// Internal definitions of macros
139 /// Internal clever macro A
140 #define _FEAL_ASSERT_A(x) _FEAL_ASSERT_OP(x, B)
141 /// Internal clever macro B
142 #define _FEAL_ASSERT_B(x) _FEAL_ASSERT_OP(x, A)
143 /// Internal clever macro recursion
144 #define _FEAL_ASSERT_OP(x, next) \
145  _FEAL_ASSERT_A.add_value((x), #x)._FEAL_ASSERT_ ## next
146 
147 
148 /// Definition of assert for debug and release mode
149 #define FEAL_ASSERT( expr) \
150 if ( !(expr) ) \
151  feal::Assert( #expr).set_context( __FILE__, __func__, __LINE__)._FEAL_ASSERT_A
152 
153 /// Definition of assert for debug mode only
154 #ifdef FLOW123D_DEBUG_ASSERTS
155 #define FEAL_DEBUG_ASSERT( expr) \
156 if ( !(expr) ) \
157  feal::Assert( #expr).set_context( __FILE__, __func__, __LINE__)._FEAL_ASSERT_A
158 #else
159 #define FEAL_DEBUG_ASSERT( expr)
160 #endif
161 
162 
163 
164 
165 /// Internal definitions of macros
166 /// Internal clever macro A
167 #define _FEAL_ASSERT_A(x) _FEAL_ASSERT_OP(x, B)
168 /// Internal clever macro B
169 #define _FEAL_ASSERT_B(x) _FEAL_ASSERT_OP(x, A)
170 /// Internal clever macro recursion
171 #define _FEAL_ASSERT_OP(x, next) \
172  _FEAL_ASSERT_A.add_value((x), #x)._FEAL_ASSERT_ ## next
173 
174 
175 /// Definition of assert for debug and release mode
176 #define FEAL_ASSERT( expr) \
177 if ( !(expr) ) \
178  feal::Assert( #expr).set_context( __FILE__, __func__, __LINE__)._FEAL_ASSERT_A
179 
180 /// Definition of assert for debug mode only
181 #ifdef FLOW123D_DEBUG_ASSERTS
182 #define FEAL_DEBUG_ASSERT( expr) \
183 if ( !(expr) ) \
184  feal::Assert( #expr).set_context( __FILE__, __func__, __LINE__)._FEAL_ASSERT_A
185 #else
186 #define FEAL_DEBUG_ASSERT( expr) \
187 if ( !(expr) ) \
188  feal::AssertNull()._FEAL_ASSERT_A
189 #endif
190 
191 
192 
193 /// Allow use shorter versions of macro names if these names is not used with external library
194 #ifndef ASSERT
195 #define ASSERT( expr) FEAL_ASSERT( expr)
196 #endif
197 #ifndef DEBUG_ASSERT
198 #define DEBUG_ASSERT( expr) FEAL_DEBUG_ASSERT( expr)
199 #endif
200 
201 
202 
203 
204 
205 #ifdef FLOW123D_DEBUG_MESSAGES
206 
207 #define DBGMSG(...) do { xprintf(MsgDbg,__VA_ARGS__); fflush(NULL); } while (0)
208 
209 
210 /**
211  * Usage:
212  * DBGCOUT( << "xy" <<endl );
213  */
214 #define DBGCOUT(...) do { std::cout << " DBG (" \
215  << __FILE__ << ", " \
216  << __func__ << ", " \
217  << __LINE__ << ")" \
218  __VA_ARGS__; } while(0)
219 
220 
221 /**
222  * Usage:
223  * DBGVAR( arma::vec( "1 2 3" ) );
224  */
225 #define DBGVAR( var ) DBGCOUT( << #var << " = " << var << endl )
226 
227 #else
228 
229 #define DBGMSG(...)
230 #define DBGCOUT(...)
231 #define DBGVAR(var)
232 
233 #endif
234 
235 
236 
237 
238 /**
239  * These macros are necessary in classes that contain Input::Type::Abstract (PARENT macro)
240  * and in classes contain descendant of this Abstract (CHILD macro) if these descendants
241  * are initialized through methods of @p Input::Factory class.
242  *
243  * These macros are necessary for initializing of static variables in classes that contain
244  * descendants of parent Abstract.
245  */
246 #define FLOW123D_FORCE_LINK_IN_CHILD(x) int force_link_##x = 0;
247 #define FLOW123D_FORCE_LINK_IN_PARENT(x) extern int force_link_##x; void func_##x() { force_link_##x = 1; }
248 
249 
250 ///@}
251 
252 #endif // GLOBAL_DEFS_H
Definitions of ASSERTS.