yat
0.8.3pre
|
00001 #ifndef _theplu_yat_utility_yat_assert_ 00002 #define _theplu_yat_utility_yat_assert_ 00003 // $Id: yat_assert.h 2384 2010-12-22 14:03:36Z peter $ 00004 00005 /* 00006 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 00007 Copyright (C) 2009, 2010 Peter Johansson 00008 00009 This file is part of the yat library, http://dev.thep.lu.se/yat 00010 00011 The yat library is free software; you can redistribute it and/or 00012 modify it under the terms of the GNU General Public License as 00013 published by the Free Software Foundation; either version 3 of the 00014 License, or (at your option) any later version. 00015 00016 The yat library is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with yat. If not, see <http://www.gnu.org/licenses/>. 00023 */ 00024 00025 #include "Exception.h" 00026 00027 #include <sstream> 00028 #include <stdexcept> 00029 #include <string> 00030 00031 namespace theplu { 00032 namespace yat { 00033 namespace utility { 00034 00043 template<class X> inline void yat_assert(bool assertion, std::string msg) 00044 #ifdef YAT_DEBUG 00045 { if (!assertion) throw X(std::string("yat_assert:")+msg); } 00046 #else 00047 { } 00048 #endif 00049 00050 }}} 00051 00052 00053 #ifdef YAT_DEBUG 00054 // Peter, this is a bit clumsy, but I wanna keep the stringstream 00055 // invisible outside macro, or multiple calls to macro would result in 00056 // multiple declaration of the stringstream. Also the macro is 00057 // supposed to be called with a trailing `;', and that's why we need 00058 // to end with something that allows that. 00059 #define YAT_ASSERT(expr) \ 00060 if (!(expr)) { \ 00061 std::stringstream yat_msg_; \ 00062 yat_msg_ << __FILE__ << ":" << __LINE__ << " failed assertion `" \ 00063 << #expr << "'"; \ 00064 theplu::yat::utility::yat_assert<theplu::yat::utility::runtime_error>(expr, \ 00065 yat_msg_.str()); \ 00066 } \ 00067 else theplu::yat::utility::yat_assert<std::runtime_error>(expr, "") 00068 #else 00069 // This could be anything empty, but why not use the empty yat_assert 00070 #define YAT_ASSERT(expr) theplu::yat::utility::yat_assert<std::runtime_error>(expr, "") 00071 #endif 00072 /* 00073 */ 00074 #endif