22#ifndef _LALSTATUSMACROS_H
23#define _LALSTATUSMACROS_H
25#include <lal/LALConfig.h>
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>
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__)
47#ifndef LAL_STATUS_MACROS_DISABLED
49#define INITSTATUS( statusptr ) \
50 do { if ( (statusptr) ) \
52 INT4 level_ = (statusptr)->level ; \
53 INT4 statp_ = (statusptr)->statusPtr ? 1 : 0 ; \
54 memset( (statusptr), 0, sizeof( LALStatus ) ); \
55 (statusptr)->level = level_ > 0 ? level_ : 1 ; \
56 (statusptr)->Id = "$Id$"; \
57 (statusptr)->function = __func__; \
58 SETSTATUSFILELINE( statusptr ); \
59 (void) LALTrace( statusptr, 0 ); \
61 ABORT( statusptr, -2, "INITSTATUS: non-null status pointer" ); \
62 else if ( xlalErrno ) \
63 ABORT( statusptr, -16, "INITSTATUS: non-zero xlalErrno" ); \
66 lalAbortHook( "Abort: function %s, file %s, line %d, %s\n" \
67 " Null status pointer passed to function\n", \
68 __func__, __FILE__, __LINE__, "$Id$" ); \
71#define RETURN( statusptr ) \
74 SETSTATUSFILELINE( statusptr ); \
75 if ( (statusptr)->statusCode ) \
76 (void) LALError( statusptr, "RETURN:" ); \
77 (void) LALTrace( statusptr, 1 ); \
79 ABORT( statusptr, -32, "RETURN: untrapped XLAL error" ); \
84#define ATTATCHSTATUSPTR(statusptr) \
85 do { if ( !(statusptr)->statusPtr ) \
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; \
93 ABORT( statusptr, -2, "ATTATCHSTATUSPTR: non-null status pointer" ); \
96#define DETATCHSTATUSPTR( statusptr ) \
97 do { if ( (statusptr)->statusPtr ) \
99 FREESTATUSPTR( statusptr ); \
100 (statusptr)->statusCode = 0; \
101 (statusptr)->statusDescription = NULL; \
104 ABORT( statusptr, -8, "DETATCHSTATUSPTR: null status pointer" ); \
107#define ABORT( statusptr, code, mesg ) \
108 do { if ( (statusptr)->statusPtr ) FREESTATUSPTR( statusptr ); \
109 SETSTATUS( statusptr, code, mesg ); \
111 (void) LALError( statusptr, "ABORT:" ); \
112 (void) LALTrace( statusptr, 1 ); \
116#ifdef LAL_ASSERT_MACRO_DISABLED
117#define ASSERT( assertion, statusptr, code, mesg )
119#define ASSERT( assertion, statusptr, code, mesg ) \
120 do { if ( !(assertion) ) \
122 if ( (statusptr)->statusPtr ) \
123 FREESTATUSPTR( statusptr ); \
124 SETSTATUS( statusptr, code, mesg ); \
125 (void) LALError( statusptr, "Assertion \"" #assertion "\" failed:" ); \
126 (void) LALTrace( statusptr, 1 ); \
132#define TRY( func, statusptr ) \
133 do { if ( (func), (statusptr)->statusPtr->statusCode ) \
135 SETSTATUS( statusptr, -1, "Recursive error" ); \
136 (void) LALError( statusptr, "Function call \"" #func "\" failed:" ); \
137 (void) LALTrace( statusptr, 1 ); \
142#define CHECKSTATUSPTR( statusptr ) \
143 do { if ( (statusptr)->statusPtr->statusCode ) \
145 SETSTATUS( statusptr, -1, "Recursive error" ); \
146 (void) LALError( statusptr, "CHECKSTATUSPTR:" ); \
147 (void) LALTrace( statusptr, 1 ); \
152#define FREESTATUSPTR( statusptr ) \
155 LALStatus *next_ = (statusptr)->statusPtr->statusPtr; \
156 LALFree( (statusptr)->statusPtr ); \
157 (statusptr)->statusPtr = next_; \
159 while ( (statusptr)->statusPtr )
161#define REPORTSTATUS( statusptr ) \
165 for ( ptr_ = (statusptr); ptr_; ptr_ = ptr_->statusPtr ) \
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 ); \
172 LALPrintError( "\tStatus code 0: Nominal\n" ); \
173 LALPrintError( "\tfunction %s, file %s, line %i\n", \
174 ptr_->function, ptr_->file, ptr_->line ); \
180#define INITSTATUS( statusptr ) \
181 do { if ( LALInitStatus( statusptr, __func__, "$Id$", __FILE__, __LINE__ ) ) return; } while ( 0 )
183#define RETURN( statusptr ) \
184 do { if ( LALPrepareReturn( statusptr, __FILE__, __LINE__ ), 1 ) return; } while ( 0 )
186#define ATTATCHSTATUSPTR( statusptr ) \
187 do { if ( LALAttatchStatusPtr( statusptr, __FILE__, __LINE__ ) ) return; } while ( 0 )
189#define DETATCHSTATUSPTR( statusptr ) \
190 do { if ( LALDetatchStatusPtr( statusptr, __FILE__, __LINE__ ) ) return; } while ( 0 )
192#define ABORT( statusptr, code, mesg ) \
193 do { if ( LALPrepareAbort( statusptr, code, mesg, __FILE__, __LINE__ ), 1 ) return; } while ( 0 )
195#ifdef LAL_ASSERT_MACRO_DISABLED
196#define ASSERT( assertion, statusptr, code, mesg )
198#define ASSERT( assertion, statusptr, code, mesg ) \
199 do { if ( !(assertion) ) \
201 LALPrepareAssertFail( statusptr, code, mesg, \
202 "Assertion \"" #assertion "\" failed:", \
203 __FILE__, __LINE__ ); \
209#define TRY( func, statusptr ) \
213 if ( LALCheckStatusPtr( statusptr, "Function call \"" #func "\" failed:", \
214 __FILE__, __LINE__ ) ) \
219#define CHECKSTATUSPTR( statusptr ) \
220 do { if ( LALCheckStatusPtr( statusptr, "CHECKSTATUSPTR:", __FILE__, __LINE__ ) ) return; } while ( 0 )
226#define BEGINFAIL( statusptr ) \
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 ); \
238#define ENDFAIL( statusptr ) \
240 DETATCHSTATUSPTR( statusptr ); \
241 (statusptr)->statusPtr = statusPtrSave_; \
242 SETSTATUS( statusptr, -1, "Recursive error" ); \
243 (void) LALError( statusptr, "ENDFAIL:" ); \
244 (void) LALTrace( statusptr, 1 ); \
249#define SETSTATUSFILELINE( statusptr ) \
250 ( ( void ) ( (statusptr)->file = __FILE__, (statusptr)->line = __LINE__ ) )
252#define SETSTATUS( statusptr, code, mesg ) \
253 ( SETSTATUSFILELINE( statusptr ), \
254 (statusptr)->statusDescription = (mesg), \
255 (statusptr)->statusCode = (code) )
260#elif defined(__cplusplus)