LAL  7.5.0.1-bede9b2
LALStatusMacros.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Jolien Creighton
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 /* ---------- SEE LALStatusMacros.dox for doxygen documentation ---------- */
21 
22 #ifndef _LALSTATUSMACROS_H
23 #define _LALSTATUSMACROS_H
24 
25 #include <lal/LALConfig.h>
26 
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <lal/LALDebugLevel.h>
31 #include <lal/LALMalloc.h>
32 #include <lal/LALDatatypes.h>
33 #include <lal/LALError.h>
34 #include <lal/XLALError.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #elif 0
39 } /* so that editors will match preceding brace */
40 #endif
41 
42 #define LAL_EXLAL 16384
43 #define LAL_MSGEXLAL "Failure in an XLAL routine"
44 #define ABORTXLAL(sp) ABORT(sp,LAL_EXLAL,LAL_MSGEXLAL)
45 #define XLAL_CHECK_LAL(sp, assertion, ...) _XLAL_CHECK_IMPL_(ABORTXLAL(sp), assertion, __VA_ARGS__)
46 
47 #ifndef LAL_STATUS_MACROS_DISABLED
48 
49 #define INITSTATUS( statusptr ) \
50  do { if ( (statusptr) ) \
51  { \
52  INT4 level_ = (statusptr)->level ; \
53  INT4 statp_ = (statusptr)->statusPtr ? 1 : 0 ; \
54  memset( (statusptr), 0, sizeof( LALStatus ) ); /* possible memory leak */ \
55  (statusptr)->level = level_ > 0 ? level_ : 1 ; \
56  (statusptr)->Id = "$Id$"; \
57  (statusptr)->function = __func__; \
58  SETSTATUSFILELINE( statusptr ); \
59  (void) LALTrace( statusptr, 0 ); \
60  if ( statp_ ) \
61  ABORT( statusptr, -2, "INITSTATUS: non-null status pointer" ); \
62  else if ( xlalErrno ) \
63  ABORT( statusptr, -16, "INITSTATUS: non-zero xlalErrno" ); \
64  } \
65  else \
66  lalAbortHook( "Abort: function %s, file %s, line %d, %s\n" \
67  " Null status pointer passed to function\n", \
68  __func__, __FILE__, __LINE__, "$Id$" ); \
69  } while ( 0 )
70 
71 #define RETURN( statusptr ) \
72  do { if ( 1 ) \
73  { \
74  SETSTATUSFILELINE( statusptr ); \
75  if ( (statusptr)->statusCode ) \
76  (void) LALError( statusptr, "RETURN:" ); \
77  (void) LALTrace( statusptr, 1 ); \
78  if ( xlalErrno ) \
79  ABORT( statusptr, -32, "RETURN: untrapped XLAL error" ); \
80  return; \
81  } \
82  } while ( 0 )
83 
84 #define ATTATCHSTATUSPTR(statusptr) \
85  do { if ( !(statusptr)->statusPtr ) \
86  { \
87  (statusptr)->statusPtr = (LALStatus *)LALCalloc( 1, sizeof( LALStatus ) );\
88  if ( !(statusptr)->statusPtr ) \
89  ABORT( statusptr, -4, "ATTATCHSTATUSPTR: memory allocation error" ); \
90  (statusptr)->statusPtr->level = (statusptr)->level + 1; \
91  } \
92  else \
93  ABORT( statusptr, -2, "ATTATCHSTATUSPTR: non-null status pointer" ); \
94  } while ( 0 )
95 
96 #define DETATCHSTATUSPTR( statusptr ) \
97  do { if ( (statusptr)->statusPtr ) \
98  { \
99  FREESTATUSPTR( statusptr ); \
100  (statusptr)->statusCode = 0; \
101  (statusptr)->statusDescription = NULL; \
102  } \
103  else \
104  ABORT( statusptr, -8, "DETATCHSTATUSPTR: null status pointer" ); \
105  } while ( 0 )
106 
107 #define ABORT( statusptr, code, mesg ) \
108  do { if ( (statusptr)->statusPtr ) FREESTATUSPTR( statusptr ); \
109  SETSTATUS( statusptr, code, mesg ); \
110  if ( code ) \
111  (void) LALError( statusptr, "ABORT:" ); \
112  (void) LALTrace( statusptr, 1 ); \
113  return; \
114  } while ( 0 )
115 
116 #ifdef LAL_ASSERT_MACRO_DISABLED
117 #define ASSERT( assertion, statusptr, code, mesg )
118 #else
119 #define ASSERT( assertion, statusptr, code, mesg ) \
120  do { if ( !(assertion) ) \
121  { \
122  if ( (statusptr)->statusPtr ) \
123  FREESTATUSPTR( statusptr ); \
124  SETSTATUS( statusptr, code, mesg ); \
125  (void) LALError( statusptr, "Assertion \"" #assertion "\" failed:" ); \
126  (void) LALTrace( statusptr, 1 ); \
127  return; \
128  } \
129  } while ( 0 )
130 #endif
131 
132 #define TRY( func, statusptr ) \
133  do { if ( (func), (statusptr)->statusPtr->statusCode ) \
134  { \
135  SETSTATUS( statusptr, -1, "Recursive error" ); \
136  (void) LALError( statusptr, "Function call \"" #func "\" failed:" ); \
137  (void) LALTrace( statusptr, 1 ); \
138  return; \
139  } \
140  } while ( 0 )
141 
142 #define CHECKSTATUSPTR( statusptr ) \
143  do { if ( (statusptr)->statusPtr->statusCode ) \
144  { \
145  SETSTATUS( statusptr, -1, "Recursive error" ); \
146  (void) LALError( statusptr, "CHECKSTATUSPTR:" ); \
147  (void) LALTrace( statusptr, 1 ); \
148  return; \
149  } \
150  } while ( 0 )
151 
152 #define FREESTATUSPTR( statusptr ) \
153  do \
154  { \
155  LALStatus *next_ = (statusptr)->statusPtr->statusPtr; \
156  LALFree( (statusptr)->statusPtr ); \
157  (statusptr)->statusPtr = next_; \
158  } \
159  while ( (statusptr)->statusPtr )
160 
161 #define REPORTSTATUS( statusptr ) \
162  do \
163  { \
164  LALStatus *ptr_; \
165  for ( ptr_ = (statusptr); ptr_; ptr_ = ptr_->statusPtr ) \
166  { \
167  LALPrintError( "\nLevel %i: %s\n", ptr_->level, ptr_->Id ); \
168  if ( ptr_->statusCode ) \
169  LALPrintError( "\tStatus code %i: %s\n", ptr_->statusCode, \
170  ptr_->statusDescription ); \
171  else \
172  LALPrintError( "\tStatus code 0: Nominal\n" ); \
173  LALPrintError( "\tfunction %s, file %s, line %i\n", \
174  ptr_->function, ptr_->file, ptr_->line ); \
175  } \
176  } while ( 0 )
177 
178 #else /* LAL_STATUS_MACROS_DISABLED */
179 
180 #define INITSTATUS( statusptr ) \
181  do { if ( LALInitStatus( statusptr, __func__, "$Id$", __FILE__, __LINE__ ) ) return; } while ( 0 )
182 
183 #define RETURN( statusptr ) \
184  do { if ( LALPrepareReturn( statusptr, __FILE__, __LINE__ ), 1 ) return; } while ( 0 )
185 
186 #define ATTATCHSTATUSPTR( statusptr ) \
187  do { if ( LALAttatchStatusPtr( statusptr, __FILE__, __LINE__ ) ) return; } while ( 0 )
188 
189 #define DETATCHSTATUSPTR( statusptr ) \
190  do { if ( LALDetatchStatusPtr( statusptr, __FILE__, __LINE__ ) ) return; } while ( 0 )
191 
192 #define ABORT( statusptr, code, mesg ) \
193  do { if ( LALPrepareAbort( statusptr, code, mesg, __FILE__, __LINE__ ), 1 ) return; } while ( 0 )
194 
195 #ifdef LAL_ASSERT_MACRO_DISABLED
196 #define ASSERT( assertion, statusptr, code, mesg )
197 #else
198 #define ASSERT( assertion, statusptr, code, mesg ) \
199  do { if ( !(assertion) ) \
200  { \
201  LALPrepareAssertFail( statusptr, code, mesg, \
202  "Assertion \"" #assertion "\" failed:", \
203  __FILE__, __LINE__ ); \
204  return; \
205  } \
206  } while ( 0 )
207 #endif
208 
209 #define TRY( func, statusptr ) \
210  do \
211  { \
212  (func); \
213  if ( LALCheckStatusPtr( statusptr, "Function call \"" #func "\" failed:", \
214  __FILE__, __LINE__ ) ) \
215  return; \
216  } \
217  while ( 0 )
218 
219 #define CHECKSTATUSPTR( statusptr ) \
220  do { if ( LALCheckStatusPtr( statusptr, "CHECKSTATUSPTR:", __FILE__, __LINE__ ) ) return; } while ( 0 )
221 
222 #endif /* LAL_STATUS_MACROS_DISABLED */
223 
224 /* these just have to be macros... */
225 
226 #define BEGINFAIL( statusptr ) \
227 do { \
228  if ( !(statusptr) ) \
229  ABORT( statusptr, -8, "BEGINFAIL: null status pointer" ); \
230  if ( !( (statusptr)->statusPtr ) ) \
231  ABORT( statusptr, -8, "BEGINFAIL: null status pointer pointer" ); \
232  if ( (statusptr)->statusPtr->statusCode ) { \
233  LALStatus *statusPtrSave_ = (statusptr)->statusPtr; \
234  (statusptr)->statusPtr = NULL; \
235  ATTATCHSTATUSPTR( statusptr ); \
236  do
237 
238 #define ENDFAIL( statusptr ) \
239  while ( 0 ); \
240  DETATCHSTATUSPTR( statusptr ); \
241  (statusptr)->statusPtr = statusPtrSave_; \
242  SETSTATUS( statusptr, -1, "Recursive error" ); \
243  (void) LALError( statusptr, "ENDFAIL:" ); \
244  (void) LALTrace( statusptr, 1 ); \
245  return; \
246  } \
247 } while ( 0 )
248 
249 #define SETSTATUSFILELINE( statusptr ) \
250  ( ( void ) ( (statusptr)->file = __FILE__, (statusptr)->line = __LINE__ ) )
251 
252 #define SETSTATUS( statusptr, code, mesg ) \
253  ( SETSTATUSFILELINE( statusptr ), \
254  (statusptr)->statusDescription = (mesg), \
255  (statusptr)->statusCode = (code) )
256 
257 
258 #if 0
259 { /* so that editors will match succeeding brace */
260 #elif defined(__cplusplus)
261 }
262 #endif
263 #endif /* _LALSTATUSMACROS_H */