1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // ZORBA_ASSERT (and the deprecated Assert) survive in release mode
18 // and raise errors via the Zorba error API
19 #pragma once
20 #ifndef ZORBA_ASSERT_H
21 #define ZORBA_ASSERT_H
22 
23 #include <sstream>
24 
25 #include "util/cxx_util.h"
26 
27 namespace zorba {
28 
29 /**
30  * Helper function for the ZORBA_ASSERT() macro.  This is called only if an
31  * assertion fails.
32  *
33  * @param condition The string representation of the condition that failed.
34  * @param file The C++ source-code file name where the assertion failed.
35  * @param line The C++ source-code line number where the assertion failed.
36  * @param msg An optional message that is output if the assertion fails.
37  * @throws ZXQP0002_ASSERT_FAILED
38  */
39 void assertion_failed( char const *condition,
40                        char const *file,
41                        int line,
42                        char const *msg = nullptr );
43 
44 /**
45  * Zorba version of the standard assert(3) macro.
46  * Is checked in RELEASE mode as well. And outputs a special
47  * error code with the failing condition.
48  */
49 #define ZORBA_ASSERT(COND)                                  \
50   do {                                                      \
51     if ( !(COND) ) {                                        \
52       zorba::assertion_failed( #COND, __FILE__, __LINE__ ); \
53       throw 0; /* never gets here but suppresses warning */ \
54     }                                                       \
55   } while (0)
56 
57 /**
58  * Zorba version of the standard assert(3) macro.
59  * Is checked in RELEASE mode as well. And outputs a special
60  * error code with the failing condition as well as potentially
61  * useful information for further debugging.
62  */
63 #define ZORBA_ASSERT_WITH_MSG(COND,MSG)                                        \
64   do {                                                                         \
65     if ( !(COND) ) {                                                           \
66       std::ostringstream oss;                                                  \
67       oss << MSG;                                                              \
68       zorba::assertion_failed( #COND, __FILE__, __LINE__, oss.str().c_str() ); \
69       throw 0; /* never gets here but suppresses warning */                    \
70     }                                                                          \
71   } while (0)
72 
73 } // namespace zorba
74 #endif /* ZORBA_ASSERT_H */
75 /* vim:set et sw=2 ts=2: */
76