CLAM-Development  1.4.0
Assert.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _ASSERT_
23 #define _ASSERT_
24 
29 #include "Err.hxx"
30 
31 namespace CLAM
32 {
33 
41 // Microsoft VisualC++ and ancestror MSC
42 #ifdef _MSC_VER
43 #define CLAM_BREAKPOINT {__asm {int 3}}
44 
45 // MetroWorks Code Warrior
46 #elif defined (__MWERKS__)
47 #define CLAM_BREAKPOINT {_asm {int 3}}
48 
49 // GNU GCC
50 #elif defined (__GNUC__) && (defined (__i386__) || defined(__x86_64__))
51 #define CLAM_BREAKPOINT {__asm__ (" int $3 "); }
52 
53 /* g++ on powerpc linux */
54 #elif defined (__GNUC__) && defined (__powerpc__)
55 #define CLAM_BREAKPOINT {__asm__ (" .long 0x7d821008 "); }
56 
57 /* g++ on powerpc macosx */
58 #elif defined (__GNUC__) && defined (__POWERPC__)
59 #define CLAM_BREAKPOINT {__asm__ (" .long 0x7d821008 "); }
60 
61 // Insert your compiler here
62 #else
63 #warning Breakpoint code unknown for the platform. You can add it defining the CLAM_BREAKPOINT macro at file Assert.hxx.
64 #define CLAM_BREAKPOINT {}
65 #endif
66 
67 
68 #if ! defined(_DEBUG)
69 #define CLAM_USE_RELEASE_ASSERTS
70 #endif
71 
72 
74 #if defined(CLAM_USE_RELEASE_ASSERTS)
75 #define CLAM_ABORT(message) \
76  do { \
77  throw CLAM::ErrAssertionFailed( message, __FILE__, __LINE__); \
78  } while(0)
79 #else
80 #define CLAM_ABORT(message) \
81  do { \
82  if ( !CLAM::ErrAssertionFailed::breakpointInCLAMAssertEnabled ) { \
83  throw CLAM::ErrAssertionFailed( message, __FILE__, __LINE__); \
84  } else { \
85  CLAM::ExecuteAssertFailedHandler ( message, __FILE__, __LINE__); \
86  CLAM_BREAKPOINT; \
87  } \
88  } while(0)
89 #endif
90 
93 
145 #if defined(CLAM_DISABLE_CHECKS)
146 #define CLAM_BEGIN_CHECK if (0) {
147 #define CLAM_END_CHECK }
148 #define CLAM_ASSERT( expression, message )
149 #define CLAM_WARNING( expression, message )
150 #else
151 #define CLAM_BEGIN_CHECK {
152 #define CLAM_END_CHECK }
153 #define CLAM_ASSERT( expression, message ) \
154  do { \
155  if (!(expression)) { \
156  CLAM_ABORT(message); \
157  } } while (0)
158 #define CLAM_WARNING( expression, message ) \
159  do { \
160  if (!(expression)) { \
161  CLAM::ExecuteWarningHandler ( message, __FILE__, __LINE__); \
162  } } while (0)
163 #endif
164 
165 
180 #if defined(CLAM_DISABLE_CHECKS) || defined(CLAM_USE_RELEASE_ASSERTS)
181 # define CLAM_BEGIN_DEBUG_CHECK if (0) {
182 # define CLAM_END_DEBUG_CHECK }
183 # define CLAM_DEBUG_ASSERT( expression, message )
184 # define CLAM_DEBUG_WARNING( expression, message )
185 #else
186 # define CLAM_BEGIN_DEBUG_CHECK {
187 # define CLAM_END_DEBUG_CHECK }
188 # define CLAM_DEBUG_ASSERT( expression, message ) \
189  do { \
190  if (!(expression)) { \
191  CLAM_ABORT(message); \
192  } } while (0)
193 # define CLAM_DEBUG_WARNING( expression, message ) \
194  do { \
195  if (!(expression)) { \
196  CLAM::ExecuteWarningHandler ( message, __FILE__, __LINE__); \
197  } } while (0)
198 #endif
199 
200 
215 class ErrAssertionFailed : public Err {
216 public:
222 
223  ErrAssertionFailed(const char* message, const char* filename, int linenumber);
224  virtual ~ErrAssertionFailed() throw () { }
225 };
226 
233 typedef void (*AssertFailedHandlerType) (const char* message, const char* filename, int lineNumber);
244 
248 void ExecuteAssertFailedHandler(const char* message, const char* filename, int linenumber);
249 
256 typedef void (*WarningHandlerType) (const char* message, const char* filename, int lineNumber);
267 
271 void ExecuteWarningHandler(const char* message, const char* filename, int linenumber);
272 
273 
274 }
275 
276 #endif //_ASSERT_
277