00001 #ifndef _theplu_yat_utility_yat_assert_ 00002 #define _theplu_yat_utility_yat_assert_ 00003 // $Id: yat_assert.h 2119 2009-12-12 23:11:43Z peter $ 00004 00005 /* 00006 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 00007 Copyright (C) 2009 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 <sstream> 00026 #include <stdexcept> 00027 #include <string> 00028 00029 namespace theplu { 00030 namespace yat { 00031 namespace utility { 00032 00041 template<class X> inline void yat_assert(bool assertion, std::string msg) 00042 #ifdef YAT_DEBUG 00043 { if (!assertion) throw X(std::string("yat_assert:")+msg); } 00044 #else 00045 { } 00046 #endif 00047 00048 }}} 00049 00050 00051 #ifdef YAT_DEBUG 00052 // Peter, this is a bit clumsy, but I wanna keep the stringstream 00053 // invisible outside macro, or multiple calls to macro would result in 00054 // multiple declaration of the stringstream. Also the macro is 00055 // supposed to be called with a trailing `;', and that's why we need 00056 // to end with something that allows that. 00057 #define YAT_ASSERT(expr) \ 00058 if (!(expr)) { \ 00059 std::stringstream yat_msg_; \ 00060 yat_msg_ << __FILE__ << ":" << __LINE__ << " failed assertion `" \ 00061 << #expr << "'"; \ 00062 theplu::yat::utility::yat_assert<std::runtime_error>(expr, \ 00063 yat_msg_.str()); \ 00064 } \ 00065 else theplu::yat::utility::yat_assert<std::runtime_error>(expr, "") 00066 #else 00067 // This could be anything empty, but why not use the empty yat_assert 00068 #define YAT_ASSERT(expr) theplu::yat::utility::yat_assert<std::runtime_error>(expr, "") 00069 #endif 00070 /* 00071 */ 00072 #endif