Flow123d
global_defs.h
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id$
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  * @file global_defs.h
26  * @brief Global macros to enhance readability and debugging, general constants.
27  *
28  */
29 
30 #ifndef GLOBAL_DEFS_H
31 #define GLOBAL_DEFS_H
32 
33 //Make sure that headers necessary for following macros are included.
34 #include <stdint.h>
35 #include <string.h>
36 #include <stdlib.h>
37 
38 #include "system/exc_common.hh"
39 
40 
41 /// @brief Global constants.
42 /// @{
43 
44 /// size of input line buffer at various places
45 #define LINE_SIZE 65536
46 
47 /// @}
48 
49 
50 /// @brief Macros to enhance readability
51 /// @{
52 
53 //#define NDEF -1 ///< not defined positive integer (obsolete - ints should be initialized by value)
54 //#define NONULL(p) ((p) != NULL) /// true for non-null pointer
55 
56 
57 // set array of pointers of given size to NULL
58 #define SET_ARRAY_NULL(ptrs,n) do {int i; for(i=0;i<n;i++) *(ptrs+i)=NULL;} while (0)
59 // set array of ints, float, or doubles to 0
60 #define SET_ARRAY_ZERO(array,n) memset((array), 0, sizeof(*(array))*(n))
61 
62 // Useful for printing boolean ints
63 #define BOOL_2_STR(i) ( (i) ? "No" : "Yes" )
64 
65 /*! @brief Debugging macros.
66  *
67  * The macro ASSERT has to be used for assertion tests. An error occures if
68  * given condition is violated. Macro accepts additional variables to print.
69  *
70  * Example:
71  * @verbatim
72  * ASSERT( i<size , "Array X overflow: index %d >= alocated size %d.\n",i,size);
73  * @endverbatim
74  *
75  * The macro INPUT_CHECK should be used for assertions about user input. So
76  * they produce User Error instead of Program error.
77  *
78  * The macro DBGMSG should be used for debugging messages,
79  * so they can be removed in production version.
80  *
81  * WARN_ASSERT - can be used for consistency tests in debugging version.
82  *
83  * @{
84  */
85 #define INPUT_CHECK(i,...) do { if (!(i)) xprintf(UsrErr,__VA_ARGS__); } while (0)
86 
87 /**
88  * Actually there are following debugging switches
89  * DEBUG_MESSAGES - use various debugging messages introduced by DBGMSG
90  * DEBUG_ASSERTS - use assertion checks introduced by ASSERT
91  * DEBUG_PROFILER - use profiling introduced by START_TIMER, END_TIMER
92  * DEBUG_FUNCTION_STACK - use function stack introduced by F_ENTRY
93  *
94  * You can turn all off defining: Flow123d_NODEBUG
95  * or turn all on defining: Flow123d_DEBUG
96  *
97  * Flow123d_DEBUG overrides Flow123d_NODEBUG
98  */
99 
100 #ifdef Flow123d_NODEBUG
101 
102 #undef DEBUG_MESSAGES
103 #undef DEBUG_ASSERTS
104 #undef DEBUG_PROFILER
105 #undef DEBUG_FUNCTION_STACK
106 
107 #endif
108 
109 
110 #ifdef Flow123d_DEBUG
111 
112 #define DEBUG_MESSAGES
113 #define DEBUG_ASSERTS
114 #define DEBUG_PROFILER
115 #define DEBUG_FUNCTION_STACK
116 
117 #endif
118 
119 
120 #ifdef DEBUG_ASSERTS
121 
122 /**
123  * Just quick hack to fix some unit tests.
124  * TODO:
125  * We should make better implementation, rather minimizing
126  * usage of macros. And make robust "system" part, that
127  * is MPI aware, but not MPI dependent.
128  */
129 #ifdef DEBUG_ASSERTS_WITHOUT_MPI
130 #define MPI_Comm_rank(A, B)
131 #endif // DEBUG_ASSERTS_WITHOUT_MPI
132 
133 #define ASSERT(i,...) do {\
134  if (!(i)) {\
135  char msg[1024];\
136  sprintf( msg, __VA_ARGS__);\
137  int rank=-1;\
138  MPI_Comm_rank(MPI_COMM_WORLD, &rank);\
139  THROW( ExcAssertMsg() << EI_Message(std::string(msg)) << EI_MPI_Rank(rank) );\
140  }} while (0)
141 
142 #define WARN_ASSERT(i,...) do { if (!(i)) xprintf(Warn,__VA_ARGS__); } while (0)
143 
144 
145 #else
146 
147 #define ASSERT(...)
148 #define WARN_ASSERT(...)
149 
150 #endif
151 
152 
153 
154 #ifdef DEBUG_ASSERTS
155 
156 #define ASSERT_EQUAL( a, b) do {\
157  stringstream ss; ss << (a) << " != " << (b); \
158  ASSERT( ((a) == (b)), "Violated assert: %s == %s,\n observed: %s.\n",#a, #b, ss.str().c_str()); \
159  } while (0)
160 #else
161 
162 #define ASSERT_EQUAL( a, b)
163 
164 #endif
165 
166 
167 
168 #ifdef DEBUG_ASSERTS
169 
170 #define ASSERT_LESS( a, b) do {\
171  stringstream ss; ss << (a) << " >= " << (b); \
172  ASSERT( ((a) < (b)) , "Violated assert: %s < %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
173  } while (0)
174 
175 
176 
177 
178 #if defined(ASSERT_LE) && defined(FLOW123D_INCLUDES_GTEST)
179 #undef ASSERT_LE
180 #endif
181 
182 
183 #define ASSERT_LE( a, b) do {\
184  stringstream ss; ss << (a) << " > " << (b); \
185  ASSERT( ((a) <= (b)) , "Violated assert: %s <= %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
186  } while (0)
187 
188 #else
189 
190 #define ASSERT_LESS( a, b)
191 #define ASSERT_LE( a, b)
192 
193 #endif
194 
195 
196 
197 
198 #ifdef DEBUG_MESSAGES
199 
200 #define DBGMSG(...) do { xprintf(MsgDbg,__VA_ARGS__); fflush(NULL); } while (0)
201 
202 /// this is simple macro for debugging output of array of ints
203 /// Use this with care !!!
204 #define DBGPRINT_INT(name,size,idx) \
205  do {\
206  int i__;\
207  xprintf(Msg, "%s (int array size=%d):\n",(name),(size));\
208  for(i__=0;i__<(size);i__++) \
209  xprintf(Msg,"i: %d int: %d\n",i__,(idx)[i__]); \
210  } while (0)
211 
212 
213 
214 
215 /**
216  * Usage:
217  * DBGCOUT( << "xy" <<endl );
218  */
219 #define DBGCOUT(...) do { std::cout << " DBG (" \
220  << __FILE__ << ", " \
221  << __func__ << ", " \
222  << __LINE__ << ")" \
223  __VA_ARGS__; } while(0)
224 
225 
226 /**
227  * Usage:
228  * DBGVAR( arma::vec( "1 2 3" ) );
229  */
230 #define DBGVAR( var ) DBGCOUT( << #var << " = " << var << endl )
231 
232 #else
233 
234 #define DBGMSG(...)
235 #define DBGPRINT_INT(...)
236 #define DBGCOUT(...)
237 #define DBGVAR(var)
238 
239 #endif
240 
241 ///@}
242 
243 #endif // GLOBAL_DEFS_H