| 1 |
// $Id: b_assert.h,v 1.6 2003/03/02 04:12:45 fukasawa Exp $ |
| 2 |
|
| 3 |
//============================================================================= |
| 4 |
/** |
| 5 |
* @file b_assert.h |
| 6 |
* |
| 7 |
* @author Fukasawa Mitsuo |
| 8 |
* |
| 9 |
* |
| 10 |
* Copyright (C) 1998-2004 BEE Co.,Ltd. All rights reserved. |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or |
| 13 |
* modify it under the terms of the GNU General Public License |
| 14 |
* as published by the Free Software Foundation; either version 2 |
| 15 |
* of the License, or (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 |
*/ |
| 26 |
//============================================================================= |
| 27 |
|
| 28 |
#ifndef B_ASSERT_H |
| 29 |
#define B_ASSERT_H |
| 30 |
|
| 31 |
#include <string> |
| 32 |
#include <stdexcept> |
| 33 |
using namespace std; |
| 34 |
|
| 35 |
#define _AsString(identifier) #identifier |
| 36 |
|
| 37 |
#ifdef NDEBUG |
| 38 |
#define BEEAssert(x) |
| 39 |
#define BEEPanic(x) |
| 40 |
#else |
| 41 |
#define BEEPanic(x) Panic<runtime_error>((x), __FILE__, __LINE__) |
| 42 |
#define BEEAssert(x) Assert<runtime_error>((x), __FILE__, __LINE__, #x) |
| 43 |
|
| 44 |
typedef runtime_error BEException; |
| 45 |
|
| 46 |
#endif /* _NO_DEBUG */ |
| 47 |
|
| 48 |
template<class T, class A> |
| 49 |
inline void Assert(A assertion, char * fname, int lineNum, char * astr) |
| 50 |
{ |
| 51 |
if (! assertion) |
| 52 |
{ |
| 53 |
char lbuf[16]; |
| 54 |
string s; |
| 55 |
sprintf(lbuf, "(%d): ", lineNum); |
| 56 |
s += (strrchr(fname, DIR_SEPARATOR_CHAR) + 1); |
| 57 |
s += lbuf; |
| 58 |
s += "ASSERT("; |
| 59 |
s += astr; |
| 60 |
s += ")"; |
| 61 |
throw T(s); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
template<class T> |
| 66 |
inline void Panic(char * msg, char * fname, int linenum) |
| 67 |
{ |
| 68 |
char lbuf[16]; |
| 69 |
string s = "Panic! "; |
| 70 |
sprintf(lbuf, "(%d): ", linenum); |
| 71 |
s += (strrchr(fname, DIR_SEPARATOR_CHAR) + 1); |
| 72 |
s += lbuf; |
| 73 |
s += msg; |
| 74 |
throw T(s); |
| 75 |
} |
| 76 |
|
| 77 |
#endif /* B_ASSERT_H */ |