Flow123d  release_3.0.0-968-gc359144
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 "logger.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 DBGCOUT 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 DBGCOUT
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 #include "asserts.hh"
85 
86 
87 
88 #ifdef FLOW123D_DEBUG_ASSERTS
89 
90 /**
91  * Just quick hack to fix some unit tests.
92  * TODO:
93  * We should make better implementation, rather minimizing
94  * usage of macros. And make robust "system" part, that
95  * is MPI aware, but not MPI dependent.
96  */
97 //#ifdef FLOW123D_DEBUG_ASSERTS_WITHOUT_MPI
98 //#define MPI_Comm_rank(A, B)
99 //#endif // FLOW123D_DEBUG_ASSERTS_WITHOUT_MPI
100 
101 #define OLD_ASSERT(i,...) do {\
102  if (!(i)) {\
103  char msg[1024];\
104  sprintf( msg, __VA_ARGS__);\
105  int rank=-1;\
106  THROW( ExcAssertMsg() << EI_Message(std::string(msg)) << EI_MPI_Rank(rank) );\
107  }} while (0)
108 
109 //#define OLD_WARN_ASSERT(i,...) do { if (!(i)) xprintf(Warn,__VA_ARGS__); } while (0)
110 
111 #define OLD_ASSERT_EQUAL( a, b) do {\
112  stringstream ss; ss << (a) << " != " << (b); \
113  OLD_ASSERT( ((a) == (b)), "Violated assert: %s == %s,\n observed: %s.\n",#a, #b, ss.str().c_str()); \
114  } while (0)
115 
116 #define OLD_ASSERT_LESS( a, b) do {\
117  stringstream ss; ss << (a) << " >= " << (b); \
118  OLD_ASSERT( ((a) < (b)) , "Violated assert: %s < %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
119  } while (0)
120 
121 #define OLD_ASSERT_LE( a, b) do {\
122  stringstream ss; ss << (a) << " > " << (b); \
123  OLD_ASSERT( ((a) <= (b)) , "Violated assert: %s <= %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
124  } while (0)
125 
126 #define OLD_ASSERT_PTR( ptr ) do {\
127  OLD_ASSERT( ((ptr) != nullptr) , "Null pointer: %s\n", #ptr ); \
128  } while (0)
129 #else
130 
131 #define OLD_ASSERT(...)
132 #define OLD_WARN_ASSERT(...)
133 #define OLD_ASSERT_EQUAL( a, b)
134 #define OLD_ASSERT_LESS( a, b)
135 #define OLD_ASSERT_LE( a, b)
136 #define OLD_ASSERT_PTR( ptr )
137 
138 #endif
139 
140 
141 
142 
143 #ifdef FLOW123D_DEBUG_MESSAGES
144 
145 /**
146  * Usage:
147  * DBGCOUT( << "xy" <<endl );
148  */
149 #define DBGCOUT(...) do { std::cout << " DBG (" \
150  << __FILE__ << ", " \
151  << __func__ << ", " \
152  << __LINE__ << ")" \
153  __VA_ARGS__; } while(0)
154 
155 
156 /**
157  * Usage:
158  * DBGVAR( arma::vec( "1 2 3" ) );
159  */
160 #define DBGVAR( var ) DBGCOUT( << #var << " = " << var << endl )
161 
162 #else
163 
164 #define DBGCOUT(...)
165 #define DBGVAR(var)
166 
167 #endif
168 
169 
170 
171 
172 /**
173  * These macros are necessary in classes that contain Input::Type::Abstract (PARENT macro)
174  * and in classes contain descendant of this Abstract (CHILD macro) if these descendants
175  * are initialized through methods of @p Input::Factory class.
176  *
177  * These macros are necessary for initializing of static variables in classes that contain
178  * descendants of parent Abstract.
179  */
180 #define FLOW123D_FORCE_LINK_IN_CHILD(x) int force_link_##x = 0;
181 #define _TOKENPASTE(x, y) func_ ## x ## y // helper macro
182 #define _TOKENPASTE2(x, y) _TOKENPASTE(x, y) // helper macro
183 #define FLOW123D_FORCE_LINK_IN_PARENT(x) extern int force_link_##x; void _TOKENPASTE2(x, __LINE__)(void) { force_link_##x = 1; }
184 
185 
186 ///@}
187 
188 #endif // GLOBAL_DEFS_H
Definitions of ASSERTS.