yat/utility/yat_assert.h

Code
Comments
Other
Rev Date Author Line
916 30 Sep 07 peter 1 #ifndef _theplu_yat_utility_yat_assert_
916 30 Sep 07 peter 2 #define _theplu_yat_utility_yat_assert_
916 30 Sep 07 peter 3 // $Id$
916 30 Sep 07 peter 4
916 30 Sep 07 peter 5 /*
4359 23 Aug 23 peter 6   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 7   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 8   Copyright (C) 2009, 2010, 2013, 2014 Peter Johansson
916 30 Sep 07 peter 9
1437 25 Aug 08 peter 10   This file is part of the yat library, http://dev.thep.lu.se/yat
916 30 Sep 07 peter 11
916 30 Sep 07 peter 12   The yat library is free software; you can redistribute it and/or
916 30 Sep 07 peter 13   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 14   published by the Free Software Foundation; either version 3 of the
916 30 Sep 07 peter 15   License, or (at your option) any later version.
916 30 Sep 07 peter 16
916 30 Sep 07 peter 17   The yat library is distributed in the hope that it will be useful,
916 30 Sep 07 peter 18   but WITHOUT ANY WARRANTY; without even the implied warranty of
916 30 Sep 07 peter 19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
916 30 Sep 07 peter 20   General Public License for more details.
916 30 Sep 07 peter 21
916 30 Sep 07 peter 22   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 23   along with yat. If not, see <http://www.gnu.org/licenses/>.
916 30 Sep 07 peter 24 */
916 30 Sep 07 peter 25
2210 05 Mar 10 peter 26 #include "Exception.h"
2210 05 Mar 10 peter 27
1875 19 Mar 09 peter 28 #include <sstream>
1875 19 Mar 09 peter 29 #include <stdexcept>
936 05 Oct 07 peter 30 #include <string>
936 05 Oct 07 peter 31
936 05 Oct 07 peter 32 namespace theplu {
936 05 Oct 07 peter 33 namespace yat {
936 05 Oct 07 peter 34 namespace utility {
936 05 Oct 07 peter 35
3188 25 Mar 14 peter 36   /// \cond IGNORE_DOXYGEN
3188 25 Mar 14 peter 37
966 11 Oct 07 peter 38   /**
1197 03 Mar 08 peter 39      \internal
1197 03 Mar 08 peter 40
966 11 Oct 07 peter 41      \brief yat_assert is similar to assert in std.
966 11 Oct 07 peter 42
2313 17 Aug 10 peter 43      If YAT_DEBUG is enabled and \a assertion is false, an exception
966 11 Oct 07 peter 44      X is thrown using constructor X(\a msg ).
966 11 Oct 07 peter 45    */
4200 19 Aug 22 peter 46   template<class X> inline void yat_assert(bool assertion, std::string msg)
916 30 Sep 07 peter 47 #ifdef YAT_DEBUG
1100 18 Feb 08 peter 48   { if (!assertion) throw X(std::string("yat_assert:")+msg); }
916 30 Sep 07 peter 49 #else
936 05 Oct 07 peter 50   { }
916 30 Sep 07 peter 51 #endif
916 30 Sep 07 peter 52
3188 25 Mar 14 peter 53   /// \endcond
3188 25 Mar 14 peter 54
936 05 Oct 07 peter 55 }}}
1875 19 Mar 09 peter 56
1875 19 Mar 09 peter 57
1875 19 Mar 09 peter 58 #ifdef YAT_DEBUG
1875 19 Mar 09 peter 59 // Peter, this is a bit clumsy, but I wanna keep the stringstream
1875 19 Mar 09 peter 60 // invisible outside macro, or multiple calls to macro would result in
1875 19 Mar 09 peter 61 // multiple declaration of the stringstream. Also the macro is
2964 22 Jan 13 peter 62 // supposed to be called with a trailing ';', and that's why we need
1875 19 Mar 09 peter 63 // to end with something that allows that.
1875 19 Mar 09 peter 64 #define YAT_ASSERT(expr) \
2075 02 Oct 09 peter 65   if (!(expr)) {                                                \
1875 19 Mar 09 peter 66     std::stringstream yat_msg_;                                  \
2964 22 Jan 13 peter 67     yat_msg_ << __FILE__ << ":" << __LINE__ << " failed assertion '" \
1875 19 Mar 09 peter 68              << #expr << "'";                                            \
2210 05 Mar 10 peter 69     theplu::yat::utility::yat_assert<theplu::yat::utility::runtime_error>(expr, \
2210 05 Mar 10 peter 70                                                     yat_msg_.str());  \
1875 19 Mar 09 peter 71   } \
1875 19 Mar 09 peter 72   else theplu::yat::utility::yat_assert<std::runtime_error>(expr, "")
1875 19 Mar 09 peter 73 #else
1875 19 Mar 09 peter 74 // This could be anything empty, but why not use the empty yat_assert
1875 19 Mar 09 peter 75 #define YAT_ASSERT(expr) theplu::yat::utility::yat_assert<std::runtime_error>(expr, "")
1875 19 Mar 09 peter 76 #endif
936 05 Oct 07 peter 77   /*
936 05 Oct 07 peter 78   */
916 30 Sep 07 peter 79 #endif